Kayako API: How to Post a Ticket

A basic PHP example of how to post a new ticket via the Kayako v4 Rest API

<?php 

$apiUrl = "https://support.domain.com/api/index.php?e=/Tickets/Ticket";
$apiKey = "78db96b0-1t7c-aea4-bd51-7xf39d50cdd6";
$salt = mt_rand();
$secretKey = "ZjBiZS0N2MtMzM3MTkyNTkxMjdiZWZkMGIjE3YmJiMGUtNmY0MC0yNWU0LWY5x6DktN1NzE0LWMxMGYyZDIzOThmYjY5YT7m";
$signature = base64_encode(hash_hmac('sha256',$salt,$secretKey,true));

$subject = "Test Ticket";
$fullname = "DropDeadDick";
$email = "test@domain.com";
$contents = "Test test test test";
$departmentid = "1";
$ticketstatusid = "1";
$ticketpriorityid = "1";
$tickettypeid = "1";
$staffid = "1";

$post_data = array('subject' => $subject,
 'fullname' => $fullname,
 'email' => $email,
 'contents' => $contents,
 'departmentid' => $departmentid,
 'ticketstatusid' => $ticketstatusid,
 'ticketpriorityid' => $ticketpriorityid,
 'tickettypeid' => $tickettypeid,
 'staffid' => $staffid, 
 'apikey' => $apiKey, 
 'salt' => $salt, 
 'signature' => $signature);

$post_data = http_build_query($post_data, '', '&');

$curl = curl_init($apiUrl); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_URL, $apiUrl);
curl_setopt($curl, CURLOPT_HEADER, false); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);

$response = curl_exec($curl);  
curl_close($curl);

$xml = simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
echo "<pre>".print_r($xml, true)."</pre>";
 ?>