Struggling with simple API transaction in PHP

jared (at netrivet)'s Avatar

jared (at netrivet)

17 Aug, 2010 01:22 AM

So, I'm digging into the API a bit, using PHP. I've based the code pasted below somewhat on this post:

http://help.lighthouseapp.com/discussions/api-developers/39-api-php...

All I'm trying to do is resolve a ticket through the API. I get what I think is a generic 500 error "Sorry, you've stumbled on an error" every time I submit the request.

I've double-checked the project ID, ticket number, it's all correct. I can load the .xml address in a browser and it prompts me for user pass then outputs the xml, so I know the address URL I've assembled is correct.

My questions are two:

1) after looking at the below code, can you help me figure out what I did wrong?

2) is there any way to get a more meaningful error response from the API rather than just a 500 error page?

define( 'LIGHTHOUSE_URL', 'https://myproject.lighthouseapp.com/' );
define( 'LIGHTHOUSE_USER', '[email blocked]' );
define( 'LIGHTHOUSE_PASS', 'mypassword' );
define( 'LIGHTHOUSE_API_TOKEN', 'tokenxxxxxxxxxxxxxxxxxxxx' );

$project_id = '12345';
$ticket_id = '2';

$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" .
            "<ticket>" .
                "<state>resolved</state>" .
            "</ticket>";

$header = "X-LighthouseToken: " . LIGHTHOUSE_API_TOKEN . "\r\n";
$header .= "Content-type: application/xml\r\n";
$header .= "Content-length: " . strlen( $xml ) . "\r\n\n";  // Important! Two linebreaks.
$header .= $xml;

$ch = curl_init();
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_USERPWD, LIGHTHOUSE_USER . ':' . LIGHTHOUSE_PASS );
curl_setopt( $ch, CURLOPT_URL, LIGHTHOUSE_URL . "projects/{$project_id}/tickets/{$ticket_id}.xml" );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( $header ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

$curl_result = curl_exec( $ch );
curl_close( $ch );

echo $curl_result;
  1. 1 Posted by jared (at netri... on 17 Aug, 2010 02:02 PM

    jared (at netrivet)'s Avatar

    I think I finally figured out the answer to my own question. I had to do it as CURLOPT_PUT instead and create an actual temporary XML file and specify the putfile and put file size.

    In case it helps anyone else out, here's some working PHP code to update a ticket, that can be modified to suit your needs:

    define( 'LIGHTHOUSE_URL', 'https://myproject.lighthouseapp.com/' );
    define( 'LIGHTHOUSE_USER', '[email blocked]' );
    define( 'LIGHTHOUSE_PASS', 'mypassword' );
    define( 'LIGHTHOUSE_API_TOKEN', 'tokenxxxxxxxxxxxxxxxxxxxx' );
    
    $project_id = '12345';
    $ticket_id = '2';
    
    $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" .
                "<ticket>" .
                    "<state>resolved</state>" .
                "</ticket>";
    
    $putFile = tmpFile();
    fwrite( $putFile, $xml );
    
    $header = "X-LighthouseToken: " . LIGHTHOUSE_API_TOKEN . "\r\n";
    $header .= "Content-type: application/xml\r\n";
    $header .= "Content-length: " . strlen( $xml ) . "\r\n\n";  // Important! Two linebreaks.
    $header .= $xml;
    
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_PUT, true );
    curl_setopt( $ch, CURLOPT_INFILE, $putFile );
    curl_setopt( $ch, CURLOPT_INFILESIZE, strlen( $xml ) );
    curl_setopt( $ch, CURLOPT_USERPWD, LIGHTHOUSE_USER . ':' . LIGHTHOUSE_PASS );
    curl_setopt( $ch, CURLOPT_URL, LIGHTHOUSE_URL . "projects/{$project_id}/tickets/{$ticket_id}.xml" );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array( $header ) );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    
    $curl_result = curl_exec( $ch );
    curl_close( $ch );
  2. jared (at netrivet) closed this discussion on 17 Aug, 2010 02:03 PM.

Discussions are closed to public comments.
If you need help with Lighthouse please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac