Using the API with PHP

gazugafan's Avatar

gazugafan

25 Apr, 2013 07:59 PM

It seems that we can no longer create tickets through the API. From what I can tell, it looks like the POST to the API is just returning "1". Nothing has changed on our end, but maybe there was a recent change to the API or it's somehow down at the moment?

  1. Support Staff 1 Posted by Tiger Team on 25 Apr, 2013 08:15 PM

    Tiger Team's Avatar

    The only thing that's different is that we recently changed load balancers (SSL and IP address changed) but the old setup is still running and should work. Are you using a library to access the API? Can you reproduce with, say, just cURL?

  2. 2 Posted by gazugafan on 25 Apr, 2013 08:32 PM

    gazugafan's Avatar

    We're using curl via PHP. The code is basically the same as found here...
    http://help.lighthouseapp.com/discussions/api-developers/39-api-php...

    I haven't tried using just curl via command line yet... I've never tried that, actually, so trying to craft a curl command that replicates what we're doing in php might take a bit for me to wrap my head around. Before I get into that, could you take a quick look at the php example code and see if there's anything obvious to you that could cause this?

  3. 3 Posted by Colin Gray on 25 Apr, 2013 09:29 PM

    Colin Gray's Avatar

    We're seeing the same problem using the same sample code from:

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

    This has worked fine for us for months, but is now returning an empty result and not creating the ticket.

    Using curl from the command line, I have been able to create a ticket, so it's not apparent to me what has changed.

    Any advice?

  4. Support Staff 4 Posted by Tiger Team on 25 Apr, 2013 10:37 PM

    Tiger Team's Avatar

    If it suddenly broke, it's likely our fault..! I will take a look. Are you including any yaml in your XML?

  5. Support Staff 5 Posted by Tiger Team on 25 Apr, 2013 10:56 PM

    Tiger Team's Avatar

    Dont burn too much time-- I should be able to figure it out way faster. It seems to be restricted to PHP clients only.

  6. Support Staff 6 Posted by Tiger Team on 25 Apr, 2013 10:57 PM

    Tiger Team's Avatar

    However if you are able to capture the raw http post for me that'd be suuuper helpful

  7. 7 Posted by Colin Gray on 25 Apr, 2013 10:58 PM

    Colin Gray's Avatar

    Thanks for looking into this. We're not using any YAML. Let us know what you find...

  8. 8 Posted by Courtenay on 25 Apr, 2013 11:30 PM

    Courtenay's Avatar

    Can you give me the project id? You're posting to /tickets.xml, right?

  9. 9 Posted by Colin Gray on 25 Apr, 2013 11:34 PM

    Colin Gray's Avatar

    I'm posting to: /projects/90316/tickets.xml

    If you can give me an email address, I can send you the php curl request dump.

  10. 10 Posted by Courtenay on 25 Apr, 2013 11:58 PM

    Courtenay's Avatar

    OK, I've found the cause of it. The workaround for now is for you to hardcode the lighthouse domain and IP address so you're hitting the old load balancer - the new one (ELB) has issues with php's curl (!!)

    So you'd put this in your hosts file:

    184.73.201.0 your.lighthouseapp.com
    

    replacing 'your' with your account name.

    I am still trying to find the right incantation to your php script to get it working.

  11. 11 Posted by Courtenay on 26 Apr, 2013 12:31 AM

    Courtenay's Avatar

    ok, turns out the original php script was crappy. Change your script to look like this:

    $header = "X-Lighthouse-Token: " . $token . "\r\n";
    $header .= "Content-Type: application/xml\r\n";
    
    curl_setopt( $curl, CURLOPT_HTTPHEADER, array( $header ) );
    curl_setopt( $curl, CURLOPT_POSTFIELDS, $xml );
    

    removing the parts about content-length, $header .= $xml and so on.

    Thus the whole request (based on that sample anyway) is something like

    $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" .
                        "<ticket>" .
                            "<title>" . $title . "</title>" .
                            "<body>" . $description . "</body>" .
                        "</ticket>";
    
    // Setup the right headers for content-type etc.
    $header = "X-LighthouseToken: " . $token . "\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    
    curl_setopt( $curl, CURLOPT_HTTPHEADER, array( $header ) );
    curl_setopt( $curl, CURLOPT_POSTFIELDS, $xml );
    
    // Execute the request and get the result
    $result = curl_exec( $curl );
    
  12. 12 Posted by Colin Gray on 26 Apr, 2013 05:10 PM

    Colin Gray's Avatar

    Still no luck; I had tried an approach similar to this yesterday, and I constantly get back an error stating "Title can't be blank". I suspect I'm missing something obvious. Here's my test code:

    
        $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" .
                            "<ticket>" .
                                "<title>myTitle</title>" .
                                "<body>myDescription</body>" .
                            "</ticket>";
    
        // Setup the right headers for content-type etc.
        $header = "X-LighthouseToken: " . $token . "\r\n";
        $header .= "Content-Type: application/x-www-form-urlencoded\r\n\r\n";
    
        curl_setopt( $curl, CURLOPT_HTTPHEADER, array( $header ) );
        curl_setopt( $curl, CURLOPT_POSTFIELDS, $xml );
    
        // Execute the request and get the result
        $result = curl_exec( $curl );
    
        curl_close($curl);
    

    Also, does your last suggestion mean we don't need the hosts file change (I've tried it both ways with no success)?

  13. 13 Posted by Julien on 26 Apr, 2013 06:02 PM

    Julien's Avatar

    Hi Colin,

    Here is a simpler, more readable version that works:

    <?php
    
    $account  = "XXX";
    $project  = "XXX";
    $username = "XXX";
    $password = "XXX";
    
    $url = "http://$account.lighthouseapp.com/projects/$project/tickets.xml";
    $title = "title";
    $description = "description";
    $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" .
      "<ticket>" .
        "<title>$title</title>" .
        "<body>$description</body>" .
      "</ticket>";
    
    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL, $url );
    curl_setopt( $curl, CURLOPT_USERPWD, "$username:$password" );
    curl_setopt( $curl, CURLOPT_HTTPHEADER, array( "Content-Type: application/xml" ) );
    curl_setopt( $curl, CURLOPT_POST, 1 );
    curl_setopt( $curl, CURLOPT_POSTFIELDS, $xml );
    curl_exec( $curl );
    curl_close( $curl );
    
    ?>
    

    A few notes though:

    • I would recommend using an API token instead of user/password. simply add X-LighthouseToken: TOKEN to the header array, and remove the CURLOPT_USERPWD line.

    • I would also recomment using an XML library to generate the XML rather than handcrafting it, which is error prone.

    Let me know if you have questions.

  14. 14 Posted by gazugafan on 26 Apr, 2013 07:47 PM

    gazugafan's Avatar

    This new PHP code works for me!

  15. 15 Posted by Colin Gray on 26 Apr, 2013 11:19 PM

    Colin Gray's Avatar

    I can confirm that the new code works for us. I'd recommend placing this in your knowledge base, as well as making reference to it in the threads that have solutions that no longer work.

    Thanks Julien and Courtenay!

  16. 16 Posted by Colin Gray on 27 Apr, 2013 12:43 AM

    Colin Gray's Avatar

    This worked for us as well. I'd recommend putting this sample in the KB and referencing it from the other thread whose approach no longer works.

    Thank you Julien and Courtenay for all your help!

  17. 17 Posted by Julien on 29 Apr, 2013 05:03 PM

    Julien's Avatar

    Hi Colin,

    Courtenay pasted the correct solution in the other thread as a follow up.

    Glad things are working now.

    Cheers!

  18. Julien closed this discussion on 29 Apr, 2013 05: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