Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Request Tracker: Users

Creating a ticket with attachment in PHP

 

 

Request Tracker users RSS feed   Index | Next | Previous | View Threaded


ghilt at shadowprojects

Apr 18, 2012, 2:57 AM

Post #1 of 8 (1528 views)
Permalink
Creating a ticket with attachment in PHP

Hello,

I'm using RT 3.8.1 and REST API in PHP.

I'm creating a ticket this way :
$request = new HttpRequest( $conf['rt']['url'] .
'/REST/1.0/ticket/new?user=' . $conf['rt']['user'] . '&pass=' .
$conf['rt']['password'], HTTP_METH_POST );
$rt_queue =
$conf['rt']['languages'][$conf['rt']['fallback_language']];
$post_data = array( 'content' => "Queue: " . $rt_queue .
"\nRequestor: " . $_SESSION['customer']['login'] . "\nCF-OS: " .
$_POST['os'] . "\nSubject: " . $_POST['subject'] . "\nOwner: " .
$_SESSION['customer']['login'] . "\nText: " . str_replace( "\n", "\n ",
$_POST['message'] ) . "\n" );
$request->addPostFields( $post_data );
$request->send();

It works well.

Now i'm trying to add an attachment.

I tried to add "attachment_1: my file content" to my post data but it
doesn't work.

What's the way to attach a file to a ticket using PHP ?

Regards,

--
Guillaume Hilt


falcone at bestpractical

Apr 19, 2012, 5:54 AM

Post #2 of 8 (1472 views)
Permalink
Re: Creating a ticket with attachment in PHP [In reply to]

On Wed, Apr 18, 2012 at 11:57:20AM +0200, Guillaume Hilt wrote:
> I'm using RT 3.8.1 and REST API in PHP.
>
> I'm creating a ticket this way :
> $request = new HttpRequest( $conf['rt']['url'] .
> '/REST/1.0/ticket/new?user=' . $conf['rt']['user'] . '&pass=' .
> $conf['rt']['password'], HTTP_METH_POST );
> $rt_queue =
> $conf['rt']['languages'][$conf['rt']['fallback_language']];
> $post_data = array( 'content' => "Queue: " . $rt_queue .
> "\nRequestor: " . $_SESSION['customer']['login'] . "\nCF-OS: " .
> $_POST['os'] . "\nSubject: " . $_POST['subject'] . "\nOwner: " .
> $_SESSION['customer']['login'] . "\nText: " . str_replace( "\n", "\n
> ", $_POST['message'] ) . "\n" );
> $request->addPostFields( $post_data );
> $request->send();
>
> It works well.
>
> Now i'm trying to add an attachment.
>
> I tried to add "attachment_1: my file content" to my post data but
> it doesn't work.
>
> What's the way to attach a file to a ticket using PHP ?

If you search the mailing list archives, you'll find that you can't
create a ticket with an attachment via the REST api, only
reply/comment with an attachment. I believe there is already a ticket
open about it.

-kevin


ghilt at shadowprojects

Apr 21, 2012, 1:13 AM

Post #3 of 8 (1457 views)
Permalink
Re: Creating a ticket with attachment in PHP [In reply to]

Thanks.
I didn't know there was a way to search into the archive. It's really
useful.

Guillaume Hilt


Le 19/04/2012 14:54, Kevin Falcone a crit :
> On Wed, Apr 18, 2012 at 11:57:20AM +0200, Guillaume Hilt wrote:
>> I'm using RT 3.8.1 and REST API in PHP.
>>
>> I'm creating a ticket this way :
>> $request = new HttpRequest( $conf['rt']['url'] .
>> '/REST/1.0/ticket/new?user=' . $conf['rt']['user'] .'&pass=' .
>> $conf['rt']['password'], HTTP_METH_POST );
>> $rt_queue =
>> $conf['rt']['languages'][$conf['rt']['fallback_language']];
>> $post_data = array( 'content' => "Queue: " . $rt_queue .
>> "\nRequestor: " . $_SESSION['customer']['login'] . "\nCF-OS: " .
>> $_POST['os'] . "\nSubject: " . $_POST['subject'] . "\nOwner: " .
>> $_SESSION['customer']['login'] . "\nText: " . str_replace( "\n", "\n
>> ", $_POST['message'] ) . "\n" );
>> $request->addPostFields( $post_data );
>> $request->send();
>>
>> It works well.
>>
>> Now i'm trying to add an attachment.
>>
>> I tried to add "attachment_1: my file content" to my post data but
>> it doesn't work.
>>
>> What's the way to attach a file to a ticket using PHP ?
> If you search the mailing list archives, you'll find that you can't
> create a ticket with an attachment via the REST api, only
> reply/comment with an attachment. I believe there is already a ticket
> open about it.
>
> -kevin


ghilt at shadowprojects

Apr 23, 2012, 3:09 AM

Post #4 of 8 (1444 views)
Permalink
Re: Creating a ticket with attachment in PHP [In reply to]

Ok, so i'm trying to comment an existing ticket with an attachment :

<?php

$request = new HttpRequest( $conf['rt']['url'] .
'/REST/1.0/ticket/' . $ticket_id . '/comment?user=' .
$conf['rt']['user'] . '&pass=' . $conf['rt']['password'], HTTP_METH_POST );

// First way : attachment is directly in the request content
$post_data = array( 'content' => "id: " . $ticket_id . "\nAction:
comment\nText: Fichier joint\nAttachment: " .
$_FILES['uploadedfile']['name'] . "\nattachment_1: " .
file_get_contents( $_FILES['uploadedfile']['tmp_name'] ) . "\n" );

//Second way : i'm adding the attachment using postfile
$post_data = array( 'content' => "id: " . $ticket_id . "\nAction:
comment\nText: Fichier joint\n" );
$request->addPostFields( $post_data );

$request->addPostFile( $_FILES['uploadedfile']['name'],
file_get_contents( $_FILES['uploadedfile']['tmp_name'] ) );
$result = $request->send();

?>

Either way, it doesn't work.

First way gives me a syntax error :

HttpMessage Object
(
[type:protected] => 2
[body:protected] => RT/3.8.10 409 Syntax Error

# Syntax error.

id: 48
Action: comment
Text: Fichier joint
Attachment: ftp_networth.jpg
attachment_1: \xFF\xD8\xFF\xE0 JFIF  ` ` \xFF\xE1\xE8Exif MM *  ; 

#K-\xB4\xF3\xE5<\xC5d\xC90\xAA\x9D\xCA\xDF+60pGKY^!\xD1c֡\xB5\xFF J\xB9\xB2\xBB\xB4\x9B\xED\xB7v\xDB


Second way gives me this error :
PHP Fatal error: Uncaught exception 'HttpInvalidParamException' with
message 'Empty or too short HTTP message

What am i doing wrong ?

I searched in this list archive and google but I didn't find any example
on how to send an attachment with PHP and the API.

Regards,

Guillaume Hilt


Le 21/04/2012 10:13, Guillaume Hilt a crit :
> Thanks.
> I didn't know there was a way to search into the archive. It's really
> useful.
>
> Guillaume Hilt
>
>
> Le 19/04/2012 14:54, Kevin Falcone a crit :
>> On Wed, Apr 18, 2012 at 11:57:20AM +0200, Guillaume Hilt wrote:
>>> I'm using RT 3.8.1 and REST API in PHP.
>>>
>>> I'm creating a ticket this way :
>>> $request = new HttpRequest( $conf['rt']['url'] .
>>> '/REST/1.0/ticket/new?user=' . $conf['rt']['user'] .'&pass=' .
>>> $conf['rt']['password'], HTTP_METH_POST );
>>> $rt_queue =
>>> $conf['rt']['languages'][$conf['rt']['fallback_language']];
>>> $post_data = array( 'content' => "Queue: " . $rt_queue .
>>> "\nRequestor: " . $_SESSION['customer']['login'] . "\nCF-OS: " .
>>> $_POST['os'] . "\nSubject: " . $_POST['subject'] . "\nOwner: " .
>>> $_SESSION['customer']['login'] . "\nText: " . str_replace( "\n", "\n
>>> ", $_POST['message'] ) . "\n" );
>>> $request->addPostFields( $post_data );
>>> $request->send();
>>>
>>> It works well.
>>>
>>> Now i'm trying to add an attachment.
>>>
>>> I tried to add "attachment_1: my file content" to my post data but
>>> it doesn't work.
>>>
>>> What's the way to attach a file to a ticket using PHP ?
>> If you search the mailing list archives, you'll find that you can't
>> create a ticket with an attachment via the REST api, only
>> reply/comment with an attachment. I believe there is already a ticket
>> open about it.
>>
>> -kevin
>


falcone at bestpractical

Apr 23, 2012, 7:29 AM

Post #5 of 8 (1412 views)
Permalink
Re: Creating a ticket with attachment in PHP [In reply to]

On Mon, Apr 23, 2012 at 12:09:20PM +0200, Guillaume Hilt wrote:
> Ok, so i'm trying to comment an existing ticket with an attachment :
>
> <?php
>
> $request = new HttpRequest( $conf['rt']['url'] .
> '/REST/1.0/ticket/' . $ticket_id . '/comment?user=' .
> $conf['rt']['user'] . '&pass=' . $conf['rt']['password'],
> HTTP_METH_POST );
>
> // First way : attachment is directly in the request content
> $post_data = array( 'content' => "id: " . $ticket_id .
> "\nAction: comment\nText: Fichier joint\nAttachment: " .
> $_FILES['uploadedfile']['name'] . "\nattachment_1: " .
> file_get_contents( $_FILES['uploadedfile']['tmp_name'] ) . "\n" );
>
> //Second way : i'm adding the attachment using postfile
> $post_data = array( 'content' => "id: " . $ticket_id .
> "\nAction: comment\nText: Fichier joint\n" );
> $request->addPostFields( $post_data );
>
> $request->addPostFile( $_FILES['uploadedfile']['name'],
> file_get_contents( $_FILES['uploadedfile']['tmp_name'] ) );
> $result = $request->send();
>
> ?>
>
> Either way, it doesn't work.
>
> First way gives me a syntax error :
>
> HttpMessage Object
> (
> [type:protected] => 2
> [body:protected] => RT/3.8.10 409 Syntax Error
>
> # Syntax error.
>
> id: 48
> Action: comment
> Text: Fichier joint
> Attachment: ftp_networth.jpg
> attachment_1: \xFF\xD8\xFF\xE0 JFIF  ` ` \xFF\xE1\xE8Exif MM *  ; 
>
> #K-\xB4\xF3\xE5<\xC5d\xC90\xAA\x9D\xCA\xDF+60pGKY^!\xD1c֡\xB5\xFF J\xB9\xB2\xBB\xB4\x9B\xED\xB7v\xDB
>
>
> Second way gives me this error :
> PHP Fatal error: Uncaught exception 'HttpInvalidParamException'
> with message 'Empty or too short HTTP message
>
> What am i doing wrong ?
>
> I searched in this list archive and google but I didn't find any
> example on how to send an attachment with PHP and the API.

There are tests for this, so I know it works, I suggest using the
bin/rt command line client in debug mode to see what it sends over the
wire. Otherwise, numerous people have used PHP/Python/Ruby/Java to
talk to the REST api so perhaps someone will volunteer some sample
code.

-kevin


ghilt at shadowprojects

Apr 23, 2012, 7:51 AM

Post #6 of 8 (1417 views)
Permalink
Re: Creating a ticket with attachment in PHP [In reply to]

I hope so because I didn't find anything.

I'm still digging and now I'm sending this kind of request to RT :

HttpRequest Object
(
[options:HttpRequest:private] =>
[postFields:HttpRequest:private] => Array
(
[content] => id: 56
Action: comment
Text: Fichier joint
Attachment: ftp_networth.jpg

--uploadfichiersupport--
[attachment_1] => filename: ftp_networth.jpg
Content-Type: image/jpeg

.... (file content)

Request Content-Type is set to "multipart/form-data;
boundary=uploadfichiersupport".

I'm trying to find my way using the VB.net example.

Guillaume Hilt


Le 23/04/2012 16:29, Kevin Falcone a crit :
> On Mon, Apr 23, 2012 at 12:09:20PM +0200, Guillaume Hilt wrote:
>> Ok, so i'm trying to comment an existing ticket with an attachment :
>>
>> <?php
>>
>> $request = new HttpRequest( $conf['rt']['url'] .
>> '/REST/1.0/ticket/' . $ticket_id . '/comment?user=' .
>> $conf['rt']['user'] . '&pass=' . $conf['rt']['password'],
>> HTTP_METH_POST );
>>
>> // First way : attachment is directly in the request content
>> $post_data = array( 'content' => "id: " . $ticket_id .
>> "\nAction: comment\nText: Fichier joint\nAttachment: " .
>> $_FILES['uploadedfile']['name'] . "\nattachment_1: " .
>> file_get_contents( $_FILES['uploadedfile']['tmp_name'] ) . "\n" );
>>
>> //Second way : i'm adding the attachment using postfile
>> $post_data = array( 'content' => "id: " . $ticket_id .
>> "\nAction: comment\nText: Fichier joint\n" );
>> $request->addPostFields( $post_data );
>>
>> $request->addPostFile( $_FILES['uploadedfile']['name'],
>> file_get_contents( $_FILES['uploadedfile']['tmp_name'] ) );
>> $result = $request->send();
>>
>> ?>
>>
>> Either way, it doesn't work.
>>
>> First way gives me a syntax error :
>>
>> HttpMessage Object
>> (
>> [type:protected] => 2
>> [body:protected] => RT/3.8.10 409 Syntax Error
>>
>> # Syntax error.
>>
>> id: 48
>> Action: comment
>> Text: Fichier joint
>> Attachment: ftp_networth.jpg
>> attachment_1: \xFF\xD8\xFF\xE0 JFIF  ` ` \xFF\xE1\xE8Exif MM *  ; 
>>
>> #K-\xB4\xF3\xE5<\xC5d\xC90\xAA\x9D\xCA\xDF+60pGKY^!\xD1c֡\xB5\xFF J\xB9\xB2\xBB\xB4\x9B\xED\xB7v\xDB
>>
>>
>> Second way gives me this error :
>> PHP Fatal error: Uncaught exception 'HttpInvalidParamException'
>> with message 'Empty or too short HTTP message
>>
>> What am i doing wrong ?
>>
>> I searched in this list archive and google but I didn't find any
>> example on how to send an attachment with PHP and the API.
> There are tests for this, so I know it works, I suggest using the
> bin/rt command line client in debug mode to see what it sends over the
> wire. Otherwise, numerous people have used PHP/Python/Ruby/Java to
> talk to the REST api so perhaps someone will volunteer some sample
> code.
>
> -kevin


hescobar at afslc

May 22, 2012, 2:33 PM

Post #7 of 8 (1315 views)
Permalink
Re: Creating a ticket with attachment in PHP [In reply to]

Hi there, I have this same problem, i need to add attachments to existing
tickets via REST from php.

I wrote this small piece of code:

=-=-=-=-=-= code =-=-=-=-=-=-=-=
$url = "
http://ticket/rt4/REST/1.0/ticket/514/comment?user=$username&pass=$password
";

$attachment_file = getcwd().'/somejpeg.jpg';
$attachment_content = file_get_contents($attachment_file);

$file_name = 'attachment_1; filename="somejpeg.jpg"' . "\n" .
'Content-Type: image/jpeg';

$post_data=array($file_name=>$attachment_content, "content"=>"Ticket:
514\nAction: comment\nText: aaahhhhhhh\nAttachment: somejpeg.jpg\n");

$curl_handle=curl_init();
curl_setopt ($curl_handle, CURLOPT_URL,$url);
curl_setopt ($curl_handle, CURLOPT_ENCODING, "deflate");
curl_setopt ($curl_handle, CURLOPT_COOKIEJAR, getcwd().'/cookies.txt');
curl_setopt ($curl_handle, CURLOPT_POST, 2);
curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $post_data);
curl_setopt ($curl_handle, CURLOPT_HEADER, 1);
curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($curl_handle, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
$response = curl_exec($curl_handle);
echo $response;
curl_close($curl_handle);
=-=-=-=-=-= end code -=-=-=-=-=-=-

In order to have a reference, I submitted the file via rt from the command
line with complete success.
I wanted to look at what 'rt' sends so i used tcpflow to reconstruct the
content, see below

=-=-=-=-=-= request sent by 'rt' from cli =-=-=-=-=-==
POST /rt4/REST/1.0/ticket/514/comment HTTP/1.1
TE: deflate,gzip;q=0.3
Connection: TE, close
Host: ticket
User-Agent: RT/3.0b
Content-Length: 41964
Content-Type: multipart/form-data; boundary=xYzZY
Cookie: RT_SID_ticket.80=c593fea8c27488b8b8764196a4b257d3

--xYzZY
Content-Disposition: form-data; name="attachment_1"; filename="somejpeg.jpg"
Content-Type: image/jpeg

(binary content)
--xYzZY
Content-Disposition: form-data; name="content"

Ticket: 514
Action: comment
Cc:
Bcc:
Attachment: somejpeg.jpg
TimeWorked:
Text: going crazy

--xYzZY--
=-=-=-=-=-=-=-= end of content sent by 'rt' =-=-=-==-=-=


=-=-=-=-=-=-=-= content sent via php/curl -=-=-=-=-=-=-
POST /rt4/REST/1.0/ticket/514/comment?user=someuser&pass=somepassword
HTTP/1.1
Host: ticket
Accept: */*
Accept-Encoding: deflate
Content-Length: 42046
Content-Type: multipart/form-data;
boundary=----------------------------5e8f1e2cb2d5

------------------------------5e8f1e2cb2d5
Content-Disposition: form-data; name="attachment_1; filename="somejpeg.jpg"
Content-Type: image/jpeg"

(binary content)
------------------------------5e8f1e2cb2d5
Content-Disposition: form-data; name="content"

Ticket: 514
Action: comment
Text: aaahhhhhhh
Attachment: somejpeg.jpg

------------------------------5e8f1e2cb2d5--
=-=-=-=-=-=-= end of content sent via php/curl =-=-=-=-=-=


I think that php is unable to construct messages of type
multipart/form-data correctly.
By comparing the line just after the boundary (for the binary part) you'll
note that they are not equal.
I couldn't find a way to replicate what rt generates with php.
I tried using a very 'unnatural' key for the file name in $post_data but it
doesn't work. All i get is the
infamous:

RT/4.0.2 400 Bad Request

# No attachment for somejpeg.jpg.


Any comments?

Thanks,
Hugo


On Mon, Apr 23, 2012 at 10:51 AM, Guillaume Hilt
<ghilt [at] shadowprojects>wrote:

> I hope so because I didn't find anything.
>
> I'm still digging and now I'm sending this kind of request to RT :
>
> HttpRequest Object
> (
> [options:HttpRequest:private] =>
> [postFields:HttpRequest:**private] => Array
> (
> [content] => id: 56
>
> Action: comment
> Text: Fichier joint
> Attachment: ftp_networth.jpg
>
> --uploadfichiersupport--
> [attachment_1] => filename: ftp_networth.jpg
> Content-Type: image/jpeg
>
> .... (file content)
>
> Request Content-Type is set to "multipart/form-data;
> boundary=uploadfichiersupport"**.
>
> I'm trying to find my way using the VB.net example.
>
> Guillaume Hilt
>
>
> Le 23/04/2012 16:29, Kevin Falcone a crit :
>
> On Mon, Apr 23, 2012 at 12:09:20PM +0200, Guillaume Hilt wrote:
>>
>>> Ok, so i'm trying to comment an existing ticket with an attachment :
>>>
>>> <?php
>>>
>>> $request = new HttpRequest( $conf['rt']['url'] .
>>> '/REST/1.0/ticket/' . $ticket_id . '/comment?user=' .
>>> $conf['rt']['user'] . '&pass=' . $conf['rt']['password'],
>>> HTTP_METH_POST );
>>>
>>> // First way : attachment is directly in the request content
>>> $post_data = array( 'content' => "id: " . $ticket_id .
>>> "\nAction: comment\nText: Fichier joint\nAttachment: " .
>>> $_FILES['uploadedfile']['name'**] . "\nattachment_1: " .
>>> file_get_contents( $_FILES['uploadedfile']['tmp_**name'] ) . "\n" );
>>>
>>> //Second way : i'm adding the attachment using postfile
>>> $post_data = array( 'content' => "id: " . $ticket_id .
>>> "\nAction: comment\nText: Fichier joint\n" );
>>> $request->addPostFields( $post_data );
>>>
>>> $request->addPostFile( $_FILES['uploadedfile']['name'**],
>>> file_get_contents( $_FILES['uploadedfile']['tmp_**name'] ) );
>>> $result = $request->send();
>>>
>>> ?>
>>>
>>> Either way, it doesn't work.
>>>
>>> First way gives me a syntax error :
>>>
>>> HttpMessage Object
>>> (
>>> [type:protected] => 2
>>> [body:protected] => RT/3.8.10 409 Syntax Error
>>>
>>> # Syntax error.
>>>
>>> id: 48
>>> Action: comment
>>> Text: Fichier joint
>>> Attachment: ftp_networth.jpg
>>> attachment_1: \xFF\xD8\xFF\xE0 JFIF ` ` \xFF\xE1 \xE8Exif MM *
>>> ;
>>> #K -\xB4\xF3\xE5<\xC5d\xC90\xAA\**x9D\xCA\xDF+60pGKY^!\xD1c֡\**xB5\xFF
>>> J\xB9\xB2\xBB\xB4\x9B\xED \xB7v\xDB
>>>
>>>
>>> Second way gives me this error :
>>> PHP Fatal error: Uncaught exception 'HttpInvalidParamException'
>>> with message 'Empty or too short HTTP message
>>>
>>> What am i doing wrong ?
>>>
>>> I searched in this list archive and google but I didn't find any
>>> example on how to send an attachment with PHP and the API.
>>>
>> There are tests for this, so I know it works, I suggest using the
>> bin/rt command line client in debug mode to see what it sends over the
>> wire. Otherwise, numerous people have used PHP/Python/Ruby/Java to
>> talk to the REST api so perhaps someone will volunteer some sample
>> code.
>>
>> -kevin
>>
>


--
Hugo Escobar


[image: AFS_logo.png]

Follow us on Facebook and Linked-In

[image: facebook-24x24.png]<http://www.facebook.com/pages/Miami-FL/ASSOCIATION-FINANCIAL/64952991864>
[image: linkedin-24x24.png] <http://www.linkedin.com/companies/1006276>
Attachments: facebook-24x24.png (0.79 KB)
  linkedin-24x24.png (0.85 KB)
  AFS_logo.png (3.11 KB)


hescobar at afslc

May 23, 2012, 1:11 PM

Post #8 of 8 (1303 views)
Permalink
Re: Creating a ticket with attachment in PHP [In reply to]

Hi,

I found a solution ... for those using PHP to add attachments to existing
tickets
the pear package 'HTTP_Request2' builds the multipart/mime-data correctly.
Here I show a few lines of code that worked perfectly:

<?php
require_once 'HTTP/Request2.php';

$username = 'user';
$password = 'somepassword';
$url = "
http://ticket/rt4/REST/1.0/ticket/514/comment?user=$username&pass=$password
";
$attachment_file = getcwd().'/somejpeg.jpg';
$request = new HTTP_Request2($url);
$request->setMethod(HTTP_Request2::METHOD_POST)
->addPostParameter("content","Ticket: 514\nAction: comment\nText:
using HTTP_Request2\nAttachment: somejpeg.jpg\n")

->addUpload('attachment_1',$attachment_file,$attachment_file,'image/jpeg');
$request->send();
?>

On Tue, May 22, 2012 at 5:33 PM, Hugo Escobar <hescobar [at] afslc> wrote:

> Hi there, I have this same problem, i need to add attachments to existing
> tickets via REST from php.
>
> I wrote this small piece of code:
>
> =-=-=-=-=-= code =-=-=-=-=-=-=-=
> $url = "
> http://ticket/rt4/REST/1.0/ticket/514/comment?user=$username&pass=$password
> ";
>
> $attachment_file = getcwd().'/somejpeg.jpg';
> $attachment_content = file_get_contents($attachment_file);
>
> $file_name = 'attachment_1; filename="somejpeg.jpg"' . "\n" .
> 'Content-Type: image/jpeg';
>
> $post_data=array($file_name=>$attachment_content, "content"=>"Ticket:
> 514\nAction: comment\nText: aaahhhhhhh\nAttachment: somejpeg.jpg\n");
>
> $curl_handle=curl_init();
> curl_setopt ($curl_handle, CURLOPT_URL,$url);
> curl_setopt ($curl_handle, CURLOPT_ENCODING, "deflate");
> curl_setopt ($curl_handle, CURLOPT_COOKIEJAR, getcwd().'/cookies.txt');
> curl_setopt ($curl_handle, CURLOPT_POST, 2);
> curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $post_data);
> curl_setopt ($curl_handle, CURLOPT_HEADER, 1);
> curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, true);
> curl_setopt ($curl_handle, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
> $response = curl_exec($curl_handle);
> echo $response;
> curl_close($curl_handle);
> =-=-=-=-=-= end code -=-=-=-=-=-=-
>
> In order to have a reference, I submitted the file via rt from the command
> line with complete success.
> I wanted to look at what 'rt' sends so i used tcpflow to reconstruct the
> content, see below
>
> =-=-=-=-=-= request sent by 'rt' from cli =-=-=-=-=-==
> POST /rt4/REST/1.0/ticket/514/comment HTTP/1.1
> TE: deflate,gzip;q=0.3
> Connection: TE, close
> Host: ticket
> User-Agent: RT/3.0b
> Content-Length: 41964
> Content-Type: multipart/form-data; boundary=xYzZY
> Cookie: RT_SID_ticket.80=c593fea8c27488b8b8764196a4b257d3
>
> --xYzZY
> Content-Disposition: form-data; name="attachment_1";
> filename="somejpeg.jpg"
> Content-Type: image/jpeg
>
> (binary content)
> --xYzZY
> Content-Disposition: form-data; name="content"
>
> Ticket: 514
> Action: comment
> Cc:
> Bcc:
> Attachment: somejpeg.jpg
> TimeWorked:
> Text: going crazy
>
> --xYzZY--
> =-=-=-=-=-=-=-= end of content sent by 'rt' =-=-=-==-=-=
>
>
> =-=-=-=-=-=-=-= content sent via php/curl -=-=-=-=-=-=-
> POST /rt4/REST/1.0/ticket/514/comment?user=someuser&pass=somepassword
> HTTP/1.1
> Host: ticket
> Accept: */*
> Accept-Encoding: deflate
> Content-Length: 42046
> Content-Type: multipart/form-data;
> boundary=----------------------------5e8f1e2cb2d5
>
> ------------------------------5e8f1e2cb2d5
> Content-Disposition: form-data; name="attachment_1; filename="somejpeg.jpg"
> Content-Type: image/jpeg"
>
> (binary content)
> ------------------------------5e8f1e2cb2d5
> Content-Disposition: form-data; name="content"
>
> Ticket: 514
> Action: comment
> Text: aaahhhhhhh
> Attachment: somejpeg.jpg
>
> ------------------------------5e8f1e2cb2d5--
> =-=-=-=-=-=-= end of content sent via php/curl =-=-=-=-=-=
>
>
> I think that php is unable to construct messages of type
> multipart/form-data correctly.
> By comparing the line just after the boundary (for the binary part) you'll
> note that they are not equal.
> I couldn't find a way to replicate what rt generates with php.
> I tried using a very 'unnatural' key for the file name in $post_data but
> it doesn't work. All i get is the
> infamous:
>
> RT/4.0.2 400 Bad Request
>
> # No attachment for somejpeg.jpg.
>
>
> Any comments?
>
> Thanks,
> Hugo
>
>
>
> On Mon, Apr 23, 2012 at 10:51 AM, Guillaume Hilt <ghilt [at] shadowprojects
> > wrote:
>
>> I hope so because I didn't find anything.
>>
>> I'm still digging and now I'm sending this kind of request to RT :
>>
>> HttpRequest Object
>> (
>> [options:HttpRequest:private] =>
>> [postFields:HttpRequest:**private] => Array
>> (
>> [content] => id: 56
>>
>> Action: comment
>> Text: Fichier joint
>> Attachment: ftp_networth.jpg
>>
>> --uploadfichiersupport--
>> [attachment_1] => filename: ftp_networth.jpg
>> Content-Type: image/jpeg
>>
>> .... (file content)
>>
>> Request Content-Type is set to "multipart/form-data;
>> boundary=uploadfichiersupport"**.
>>
>> I'm trying to find my way using the VB.net example.
>>
>> Guillaume Hilt
>>
>>
>> Le 23/04/2012 16:29, Kevin Falcone a crit :
>>
>> On Mon, Apr 23, 2012 at 12:09:20PM +0200, Guillaume Hilt wrote:
>>>
>>>> Ok, so i'm trying to comment an existing ticket with an attachment :
>>>>
>>>> <?php
>>>>
>>>> $request = new HttpRequest( $conf['rt']['url'] .
>>>> '/REST/1.0/ticket/' . $ticket_id . '/comment?user=' .
>>>> $conf['rt']['user'] . '&pass=' . $conf['rt']['password'],
>>>> HTTP_METH_POST );
>>>>
>>>> // First way : attachment is directly in the request content
>>>> $post_data = array( 'content' => "id: " . $ticket_id .
>>>> "\nAction: comment\nText: Fichier joint\nAttachment: " .
>>>> $_FILES['uploadedfile']['name'**] . "\nattachment_1: " .
>>>> file_get_contents( $_FILES['uploadedfile']['tmp_**name'] ) . "\n" );
>>>>
>>>> //Second way : i'm adding the attachment using postfile
>>>> $post_data = array( 'content' => "id: " . $ticket_id .
>>>> "\nAction: comment\nText: Fichier joint\n" );
>>>> $request->addPostFields( $post_data );
>>>>
>>>> $request->addPostFile( $_FILES['uploadedfile']['name'**],
>>>> file_get_contents( $_FILES['uploadedfile']['tmp_**name'] ) );
>>>> $result = $request->send();
>>>>
>>>> ?>
>>>>
>>>> Either way, it doesn't work.
>>>>
>>>> First way gives me a syntax error :
>>>>
>>>> HttpMessage Object
>>>> (
>>>> [type:protected] => 2
>>>> [body:protected] => RT/3.8.10 409 Syntax Error
>>>>
>>>> # Syntax error.
>>>>
>>>> id: 48
>>>> Action: comment
>>>> Text: Fichier joint
>>>> Attachment: ftp_networth.jpg
>>>> attachment_1: \xFF\xD8\xFF\xE0 JFIF ` ` \xFF\xE1 \xE8Exif MM *
>>>> ;
>>>> #K -\xB4\xF3\xE5<\xC5d\xC90\xAA\**x9D\xCA\xDF+60pGKY^!\xD1c֡\**xB5\xFF
>>>> J\xB9\xB2\xBB\xB4\x9B\xED \xB7v\xDB
>>>>
>>>>
>>>> Second way gives me this error :
>>>> PHP Fatal error: Uncaught exception 'HttpInvalidParamException'
>>>> with message 'Empty or too short HTTP message
>>>>
>>>> What am i doing wrong ?
>>>>
>>>> I searched in this list archive and google but I didn't find any
>>>> example on how to send an attachment with PHP and the API.
>>>>
>>> There are tests for this, so I know it works, I suggest using the
>>> bin/rt command line client in debug mode to see what it sends over the
>>> wire. Otherwise, numerous people have used PHP/Python/Ruby/Java to
>>> talk to the REST api so perhaps someone will volunteer some sample
>>> code.
>>>
>>> -kevin
>>>
>>
>
>
> --
> Hugo Escobar
>
>
> [image: AFS_logo.png]
>
> Follow us on Facebook and Linked-In
>
> [image: facebook-24x24.png]<http://www.facebook.com/pages/Miami-FL/ASSOCIATION-FINANCIAL/64952991864>
> [image: linkedin-24x24.png] <http://www.linkedin.com/companies/1006276>
>
>


--
Hugo Escobar


[image: AFS_logo.png]

Follow us on Facebook and Linked-In

[image: facebook-24x24.png]<http://www.facebook.com/pages/Miami-FL/ASSOCIATION-FINANCIAL/64952991864>
[image: linkedin-24x24.png] <http://www.linkedin.com/companies/1006276>
Attachments: linkedin-24x24.png (0.85 KB)
  AFS_logo.png (3.11 KB)
  facebook-24x24.png (0.79 KB)
  linkedin-24x24.png (0.85 KB)
  facebook-24x24.png (0.79 KB)
  AFS_logo.png (3.11 KB)

Request Tracker users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.