Creating a ticket via the osTicket API using PHP & Crontab (cron job)

I feel that there is very little documentation and examples around of how to do this, and it bugged me for more than 10 minutes, so I thought I’d share my findings.

The documentation can currently be found here: https://github.com/osTicket/osTicket-1.7/blob/develop/setup/doc/api.md and here: https://github.com/osTicket/osTicket-1.7/blob/develop/setup/doc/api/tickets.md

A sample script can be found here: https://github.com/osTicket/osTicket-1.7/blob/develop/setup/scripts/rcron.php (which is what I based the following example off).

But I like to be somewhat lazy and copy and paste a script, test to see it works and then go about tweaking it.

#!/usr/bin/php -q
 URL to api/tickets.json e.g http://yourdomain.com/support/api/tickets.json
# key => API's Key (see admin panel on how to generate a key)
#

$config = array(
'url'=>'http://yourdomain.com/support/api/tickets.json',
'key'=>'[long api key goes here]'
);

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

#set timeout
set_time_limit(30);

#Sample data for the ticket
# See https://github.com/osTicket/osTicket-1.7/blob/develop/setup/doc/api/tickets.md for full list of variables and options that you can pass.
$data = array("alert" => "true",
       "autorespond" => "true",
       "source" => "API",
       "name" => "Angry User",
       "email" => "[email protected]",
       "subject" => "Testing API 3",
       "message" => "MESSAGE HERE"
);
#Convert the above array into json to POST to the API with curl below.
$data_string = json_encode($data);

#curl post
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config['url']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.7');
curl_setopt($ch, CURLOPT_HEADER, TRUE);
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);
curl_close($ch);

if(preg_match('/HTTP\/.* ([0-9]+) .*/', $result, $status) && $status[1] == 200)
exit(0);

echo $result;
exit(1);
?>

 

Then follow your hosts instructions for adding a Cron Job.
Mine looks something like this:

php /home/abennett/public_html/sample-api.php

6 thoughts on “Creating a ticket via the osTicket API using PHP & Crontab (cron job)”

  1. Really thanks for your help!!
    But… I have a problem…
    when I ran the php application: that`s the request:

    HTTP/1.1 200 OK Date: Fri, 06 Feb 2015 17:57:49 GMT Server: Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.9 X-Powered-By: PHP/5.5.9 Content-Length: 12 Content-Type: text/html; charset=utf-8 System Error

    may you help me please?
    (:
    good day!

    1. Howdy,
      Sorry for the poor instructions,
      Did you remember to change the URLs/variables/etc to match your server?

  2. Hi, I’m interested in reading the tickets via api, but all the information I find is about how to write them. Is it possible to read ?, where can I get the full documentation of the API, Thanks

    1. Good question, it appears osTicket only supports creation of tickets “The osTicket API is implemented as (somewhat) simple XML or JSON over HTTP. For now, only ticket creation is supported, but eventually, all resources inside osTicket will be accessible and modifiable via the API.”
      (https://docs.osticket.com/en/latest/Developer%20Documentation/API%20Docs.html)

      For another project, I was using LimeSurvey and I couldn’t make the API work the way I wanted, so I just used a really lightweight PHP framework (might have been Slim? I can’t recall) and just built my own API that read the Database and returned some results. That might be an option for you, depending on your skills and what you want.

Leave a Reply to arman Cancel reply

Your email address will not be published. Required fields are marked *