Tag Archives: osTicket API

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