Overview
This article shares a PHP script that allows for creating a conversation and attaching a file in a single instance.
Important Note: If you are unsure about securely performing the steps mentioned in this article, always make a backup before making any changes or reach out to support for more help.
Information
Use the following PHP script with the mandatory parameters of domain, email and password of the Kayako instance to create a conversation with an attachment:
<?php
/*Agent Username and password along with the helpdesk URL*/
define("TNKName", "a4@gmail.com");
define("TNKPass", "a");
define("TNKURL", "https://domain.kayako.com/api/v1/");
function curlWrap($url, $json, $action)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_URL, TNKURL . $url);
curl_setopt($ch, CURLOPT_USERPWD, TNKName . ":" . TNKPass);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
switch ($action) {
case "POST":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
break;
case "GET":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
break;
case "PUT":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
break;
case "DELETE":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
default:
break;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'content-type: multipart/form-data; boundary=---011000010111000001101001'
));
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
/*Execute the call and display errors*/
$output = curl_exec($ch);
if ($output === FALSE) {
die(curl_error($ch));
}
curl_close($ch);
return json_decode($output, true);
}
/*Json encoding of the params*/
/*Please make sure that the correct path to the file is entered here*/
$json = "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"subject\"\r\n\r\nWOW! This is the subject of the case created through APIs from PHP script\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"contents\"\r\n\r\nThis is a test case. Please ignore. This is created via APIs. The attachment is coming through\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"files[]\";filename=\"sheet-music.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n" . file_get_contents("sheet-music.jpg") . "\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"files[]\";filename=\"The_Entertainer.mp3\"\r\nContent-Type: audio/mpeg\r\n\r\n" . file_get_contents("The_Entertainer.mp3") . "\r\n-----011000010111000001101001--";
/*Call function*/
echo '<br> Completed case info:<br>' . print_r(curlWrap("conversations.json", $json, "POST"));
?>