Kayako API: How to Post a Ticket

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


$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);  

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

5 thoughts on “Kayako API: How to Post a Ticket

  1. Very Helpful!! Thanks a lot for the post.

    One question : This PHP create a ticket and send just a automated response confirming the receipt of your ticket to the customer.

    If I create thru the web interface it sends the ticket to teh customer by mail, not teh automated response.

    It is possible to do that using your PHP ?

Comments are closed.