Howdy, Stranger!

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

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

API Create Ticket With Attachments Fixed

Hi Folks,

There's a few articles out there showing how to create tickets through the OSTicket API.

One of the ones I found (below) unfortunately had a couple of errors in the script for attachments.

http://tmib.net/using-osticket-1812-api

After some minor corrections I've been able to get this working to handle attachments when raising a ticket through the API.

I'm using PHP 5.4+ and osTicket V1.9+

(Script below as php file attachments aren't allowed in this forum)
#!/usr/bin/php -q
URL to api/task/cron e.g # http://yourdomain.com/support/api/tickets.json
# key => API's Key (see admin panel on how to generate a key)
# $data add custom required fields to the array.
#
# Originally authored by jared@osTicket.com
# Modified by ntozier@osTicket / tmib.net
# Attachments fixed by ddhnote@gmail.com


// You must configure the url and key in the array below.

$config = array(
'url'=>'http://your.domain.tld/api/tickets.json', // URL to site.tld/api/tickets.json
'key'=>'PUTyourAPIkeyHERE' // API Key goes here
);
# NOTE: some people have reported having to use "http://your.domain.tld/api/http.php/tickets.json" instead.

if($config['url'] === 'http://your.domain.tld/api/tickets.json') {
echo "Error: No URL
You have not configured this script with your URL!";
echo "Please edit this file ".__FILE__." and add your URL at line 18.";
die();
}
if(IsNullOrEmptyString($config['key']) || ($config['key'] === 'PUTyourAPIkeyHERE')) {
echo "Error: No API Key
You have not configured this script with an API Key!";
echo "Please log into osticket as an admin and navigate to: Admin panel -> Manage -> Api Keys then add a new API Key.
";
echo "Once you have your key edit this file ".__FILE__." and add the key at line 19.";
die();
}

# Fill in the data for the new ticket, this will likely come from $_POST.
# NOTE: your variable names in osT are case sensiTive.
# So when adding custom lists or fields make sure you use the same case
# For examples on how to do that see Agency and Site below.
$data = array(
'name' => 'John Doe', // from name aka User/Client Name
'email' => 'john@gmail.com', // from email aka User/Client Email
'phone' => '1234567890', // phone number aka User/Client Phone Number
'subject' => 'Test API message', // test subject, aka Issue Summary
'message' => 'This is a test of the osTicket API', // test ticket body, aka Issue Details.
'ip' => $_SERVER['REMOTE_ADDR'], // Should be IP address of the machine thats trying to open the ticket.
'topicId' => '1', //BUGFIX: there was a semi-colon instead of a comma // the help Topic that you want to use for the ticket
//'Agency' => '58', //this is an example of a custom list entry. This should be the number of the entry.
//'Site' => 'Bermuda'; // this is an example of a custom text field. You can push anything into here you want.
'attachments' => array(
array(
// BUGFIX: there was a semi-colon instead of comma after base64
'savedname.mp3' => 'data:audio/mpeg;base64,' . base64_encode(file_get_contents('/path/to/attachment.mp3')),
/*
savedname.mp3 is the display name the file will have in the ticket

This example attaches a mp3 (voicemail) to the ticket but the method is the same
for other files just change the 'audio/mpeg' to another file type

'/path/to/attachment1.mp3' is the file path for the attachment

if you want to use a file from a stream or remote URL you can add a line in before declaring the $data array to do something like this

it does require the /path/to/ directory to be chmod 777 (writable)
*/

)
);


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

#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.8');
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)
die('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

function IsNullOrEmptyString($question){
return (!isset($question) || trim($question)==='');
}
?>
Sign In or Register to comment.