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

Kevin Falcone falcone at bestpractical.com
Mon Apr 23 10:29:27 EDT 2012


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 195 bytes
Desc: not available
URL: <http://lists.bestpractical.com/pipermail/rt-users/attachments/20120423/2d1d1df9/attachment.sig>


More information about the rt-users mailing list