API PHP Example

martijndekuijper's Avatar

martijndekuijper

24 Sep, 2008 09:24 AM

Hi,

I was struggling with getting the Lighthouse API to work on PHP, but I finally managed. One thing I was missing was a simple working(!) example, so I'll just share my example (for adding a ticket) with you in order to get you started (maybe it's not that elegant, but it works ;) );

$l_sUrl = "http://{your_app_name}.lighthouseapp.com/projects/{project-id}/tickets.xml";

$l_oCurl = curl_init();
    
curl_setopt( $l_oCurl,CURLOPT_URL, $l_sUrl );
curl_setopt( $l_oCurl, CURLOPT_USERPWD, "username:password" );

$l_sTitle = "title_here";
$l_sDescription = "description here";

$l_sXml =   "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".
                        "<ticket>".
                            "<title>".$l_sTitle."</title>".
                            "<body>".$l_sDescription."</body>".
                        "</ticket>";

$l_sHeader = "X-LighthouseToken: {token_here}\r\n";
$l_sHeader .= "Content-type: application/xml\r\n";
$l_sHeader .= "Content-length: ".strlen( $l_sXml ) . "\r\n\n"; // Important! Two linebreaks.
$l_sHeader .= $l_sXml;

curl_setopt( $l_oCurl, CURLOPT_HTTPHEADER, array( $l_sHeader ) );

$l_oResult = curl_exec( $l_oCurl );
curl_close($l_oCurl);
  1. Support Staff 1 Posted by Tiger Team on 24 Sep, 2008 04:32 PM

    Tiger Team's Avatar

    Will, this should go into the KB.

  2. 2 Posted by Rick on 24 Sep, 2008 04:49 PM

    Rick's Avatar
    $l_sUrl = "http://{your_app_name}.lighthouseapp.com/projects/{project-id}/tickets.xml";
    
    $l_oCurl = curl_init();
    
    curl_setopt( $l_oCurl,CURLOPT_URL, $l_sUrl ); curl_setopt( $l_oCurl, CURLOPT_USERPWD, "username:password" );
    
    $l_sTitle = "title_here"; $l_sDescription = "description here";
    
    $l_sXml = "<?xml version="1.0" encoding="UTF-8"?>".
                        "<ticket>".
                            "<title>".$l_sTitle."</title>".
                            "<body>".$l_sDescription."</body>".
                        "</ticket>";
    
    $l_sHeader = "X-LighthouseToken: {token_here}\r\n"; $l_sHeader .= "Content-type: application/xml\r\n"; $l_sHeader .= "Content-length: ".strlen( $l_sXml ) . "\r\n\n"; // Important! Two linebreaks. $l_sHeader .= $l_sXml;
    
    curl_setopt( $l_oCurl, CURLOPT_HTTPHEADER, array( $l_sHeader ) );
    
    $l_oResult = curl_exec( $l_oCurl ); curl_close($l_oCurl);
    
  3. 3 Posted by Rick on 24 Sep, 2008 04:50 PM

    Rick's Avatar

    I'd suggest you html encode the title and description fields. If you use any raw html entities like <, it will break the XML.

  4. 4 Posted by System on 03 Dec, 2008 09:22 PM

    System's Avatar

    This discussion was assigned to Will, on ticket 22.

  5. 5 Posted by bryan on 21 Apr, 2009 03:06 PM

    bryan's Avatar

    thanks for getting me started. I found one thing missing for creating a ticket:

    curl_setopt($l_oCurl, CURLOPT_POST, 1);

    (Need to send the XML request as POST to create a ticket)

  6. 6 Posted by Will Duncan on 21 Apr, 2009 08:13 PM

    Will Duncan's Avatar
    <?php
    // Account and user settings
    $account = "yourapp";
    $project_id = "12345";
    $user = "[email blocked]";
    $password = "yourpass123";
    $token = "XXXXXXX";
    
    // Assemble the account url
    $url = "http://" . $account . ".lighthouseapp.com/projects/" . $project_id . "/tickets.xml";
    
    // Setup the cURL object
    $curl = curl_init();
    curl_setopt($l_oCurl, CURLOPT_POST, 1);
    curl_setopt( $curl, CURLOPT_URL, $url );
    curl_setopt( $curl, CURLOPT_USERPWD, ($user . ":" . $password) );
    
    // Create the XML to post
    $title = "Create a new ticket";
    $description = "A new lighthouse ticket";
    
    $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/xml\r\n";
    $header .= "Content-length: " . strlen( $xml ) . "\r\n\n";  // Important! Two linebreaks.
    $header .= $xml;
    
    curl_setopt( $curl, CURLOPT_HTTPHEADER, array( $header ) );
    
    // Execute the request and get the result
    $result = curl_exec( $curl );
    curl_close($curl);
    ?>
    
  7. brandi closed this discussion on 29 Jun, 2012 10:10 PM.

  8. Tiger Team re-opened this discussion on 26 Apr, 2013 12:33 AM

  9. Support Staff 7 Posted by Tiger Team on 26 Apr, 2013 12:33 AM

    Tiger Team's Avatar

    anyone following this, here is a better post for the business end of things that actually generates a valid http request o_O

    <?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 );
    
    ?>
    
  10. Tiger Team closed this discussion on 26 Apr, 2013 12:34 AM.

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