Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

osTicket v1.10 (stable) and Maintenance Release v1.9.15 are now available! Go get it now

PHP Form example to hit API?

Just looking for an example of someone that's implemented a custom php page to create new tickets via the new api engine. I imagine SOMEONE has done this... I've looked through the documentation but don't see any working examples.

anyways, if someone could be so kind to post up their code; I think it would help out tons of folks in similar situations.

Comments

  • This should get you close. You might not need to part about the attachments, but it's include as an example.

    More documentation is available here


    #!/usr/bin/php -q
    <?php
    #
    # Configuration: Enter the url and key. That is it.
    # url => URL to api/task/cron e.g http://yourdomain.com/support/api/tasks/cron
    # key => API's Key (see admin panel on how to generate a key)
    #

    $config = array(
    'url'=>'http://domain.com/api/tickets.json',
    'key'=>'<api key>'
    );

    # Fill in the data for the new ticket, this will likely come from $_POST.

    $data = array(
    'name' => 'greezybacon',
    'email' => 'mailbox@host.com',
    'subject' => 'Test API message',
    'message' => 'This is a test of the osTicket API',
    'ip' => $_SERVER['REMOTE_ADDR'],
    'attachments' => array(),
    );

    #pre-checks
    function_exists('curl_version') or die('CURL support required');
    function_exists('json_encode') or die('JSON support required');

    $data['attachments'][] =
    array('filename.pdf' =>
    'data:application/pdf;base64,' .
    base64_encode(file_get_contents('/path/to/filename.pdf')));

    #set timeout
    set_time_limit(30);

    #curl post
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $config['url']);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.7');
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-API-Key: '.$config['key']));
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result=curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($code != 201)
    throw new Exception('Unable to create ticket: '.$result);

    $ticket_id = (int) $result;

    # Continue onward here if necessary. $ticket_id has the ID number of the
    # newly-created ticket

    ?>


    Cheers,

    PS - Hopefully one day, osTicket will have a downloadable API library ...
  • thanks, going to play around with it... If anyone has a full form example as well, feel free to post :)
  • just trying to get the example to fire off is throwing an error 500 in apache and looking at the apache logs; showing it's trying to redirect to a place that doesn't exist;

    is this code compatible with the latest 1.7.1 ?

    Almost wonder if because our address we're using is a http://helpdesk.somewhere.com/hid/api if that's throwning things off... it keeps redirecting me over to http://www.somewhere.com instead.... very strange.

    going to investigate the .htaccess file more I guess.
  • just curious, does anyone have this working? been trying to wrap my head around it; and am still getting errors...

    (see above)
  • so anyways, seems that httpd.conf defaults to not allow the .htaccess file to do rewrites by default.

    anyways, fixed that part... was having an issue with the system saying invalid api, but that seems to be working now.

    Now on to building out some forms to actually post data... Test tickets made it into the system.

    (if anyone has any php forms they've built; love to see them!)
  • Thank so much
Sign In or Register to comment.