[rt-users] Creating a ticket with attachment in PHP

Hugo Escobar hescobar at afslc.com
Wed May 23 16:11:29 EDT 2012


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.com> 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.org
> > 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>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.bestpractical.com/pipermail/rt-users/attachments/20120523/975c9983/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: linkedin-24x24.png
Type: image/png
Size: 875 bytes
Desc: not available
URL: <http://lists.bestpractical.com/pipermail/rt-users/attachments/20120523/975c9983/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: AFS_logo.png
Type: image/png
Size: 3183 bytes
Desc: not available
URL: <http://lists.bestpractical.com/pipermail/rt-users/attachments/20120523/975c9983/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: facebook-24x24.png
Type: image/png
Size: 814 bytes
Desc: not available
URL: <http://lists.bestpractical.com/pipermail/rt-users/attachments/20120523/975c9983/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: linkedin-24x24.png
Type: image/png
Size: 875 bytes
Desc: not available
URL: <http://lists.bestpractical.com/pipermail/rt-users/attachments/20120523/975c9983/attachment-0003.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: facebook-24x24.png
Type: image/png
Size: 814 bytes
Desc: not available
URL: <http://lists.bestpractical.com/pipermail/rt-users/attachments/20120523/975c9983/attachment-0004.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: AFS_logo.png
Type: image/png
Size: 3183 bytes
Desc: not available
URL: <http://lists.bestpractical.com/pipermail/rt-users/attachments/20120523/975c9983/attachment-0005.png>


More information about the rt-users mailing list