Created
July 4, 2023 14:31
-
-
Save mpdroog/45f2ff0ace8254398adbc2213b858029 to your computer and use it in GitHub Desktop.
Kayako API add ticket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
const API_KEY = "a-b-c-d"; | |
const SECRET_KEY = "SuperSecretKeyFromKayako"; | |
const HTTP_AUTH = "base64user:pass"; // Leave empty if none | |
const HTTP_URL = "https://www.yourwebsite.com/kayako/api/index.php?e="; | |
function has_prefix($s, $prefix) { | |
return mb_substr($s, 0, mb_strlen($prefix)) === $prefix; | |
} | |
function ky_xml_to_array($xml, $namespaces = null) { | |
$iter = 0; | |
$arr = array(); | |
if (is_string($xml)) | |
$xml = new SimpleXMLElement($xml); | |
if (!($xml instanceof SimpleXMLElement)) | |
return $arr; | |
if ($namespaces === null) | |
$namespaces = $xml->getDocNamespaces(true); | |
foreach ($xml->attributes() as $attributeName => $attributeValue) { | |
$arr["_attributes"][$attributeName] = trim($attributeValue); | |
} | |
foreach ($namespaces as $namespace_prefix => $namespace_name) { | |
foreach ($xml->attributes($namespace_prefix, true) as $attributeName => $attributeValue) { | |
$arr["_attributes"][$namespace_prefix.':'.$attributeName] = trim($attributeValue); | |
} | |
} | |
$has_children = false; | |
foreach ($xml->children() as $element) { | |
$has_children = true; | |
$elementName = $element->getName(); | |
if ($element->children()) { | |
$arr[$elementName][] = ky_xml_to_array($element, $namespaces); | |
} else { | |
$shouldCreateArray = array_key_exists($elementName, $arr) && !is_array($arr[$elementName]); | |
if ($shouldCreateArray) { | |
$arr[$elementName] = array($arr[$elementName]); | |
} | |
$shouldAddValueToArray = array_key_exists($elementName, $arr) && is_array($arr[$elementName]); | |
if ($shouldAddValueToArray) { | |
$arr[$elementName][] = trim($element[0]); | |
} else { | |
$arr[$elementName] = trim($element[0]); | |
} | |
} | |
$iter++; | |
} | |
if (!$has_children) { | |
$arr['_contents'] = trim($xml[0]); | |
} | |
return $arr; | |
} | |
function http($method, $url, array $data) { | |
// auth | |
$data['apikey'] = API_KEY; | |
$data['salt'] = mt_rand(); | |
$data['signature'] = base64_encode(hash_hmac('sha256', $data["salt"], SECRET_KEY, true)); | |
$post_data = http_build_query($data, '', '&'); | |
$curl = curl_init(HTTP_URL . $url); | |
$ok = 1; | |
$ok &= curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); | |
$ok &= curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); | |
$ok &= curl_setopt($curl, CURLOPT_HEADER, false); | |
$ok &= curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
$ok &= curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); | |
if (strlen(HTTP_AUTH) > 0) { | |
$ok &= curl_setopt($curl, CURLOPT_USERPWD, HTTP_AUTH); | |
} | |
$ok &= curl_setopt($curl, CURLOPT_TIMEOUT, 5*60); // 5min | |
if ($ok !== 1) { | |
user_error("curl_setopt fail"); | |
} | |
$res = curl_exec($curl); | |
$http = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
$contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); | |
curl_close($curl); | |
if ($http !== 200) { | |
var_dump( debug_backtrace() ); | |
user_error("Invalid http-code=$http res=$res"); | |
} | |
if (! has_prefix($res, "<?xml")) { | |
user_error("Invalid http-res res=$res"); | |
} | |
return ky_xml_to_array($res); | |
} | |
function add_ticket($email, $subject, $contents) { | |
$fullname = "Customer"; | |
$departmentid = "1"; | |
$ticketstatusid = "1"; | |
$ticketpriorityid = "1"; | |
$tickettypeid = "1"; | |
$staffid = "4"; // TODO: Move to global? | |
return http("POST", "/Tickets/Ticket", [ | |
'subject' => $subject, | |
'fullname' => $fullname, | |
'email' => $email, | |
'contents' => $contents, | |
'departmentid' => $departmentid, | |
'ticketstatusid' => $ticketstatusid, | |
'ticketpriorityid' => $ticketpriorityid, | |
'tickettypeid' => $tickettypeid, | |
'staffid' => $staffid | |
]); | |
} | |
function usersearch($email) { | |
return http("POST", "/Base/UserSearch", [ | |
'query' => $email | |
]); | |
} | |
function add_user($email) { | |
return http("POST", "/Base/User", [ | |
'fullname' => "Customer", | |
'password' => mt_rand(), | |
'email' => $email, | |
'usergroupid' => "2" | |
]); | |
} | |
function update_ticket($id, array $d) { | |
return http("PUT", "/Tickets/Ticket/$id/", $d); | |
} | |
// Actual code | |
$flags = ["verbose" => false]; | |
$req = ["email" => "someone@somewhere.com", "subject" => "Question", "contents" => "Blabla"]; | |
{ | |
// 1. user create if needed | |
$res = usersearch($req["email"]); | |
if ($flags["verbose"]) var_dump($res); | |
$userid = null; | |
if (isset($res["user"])) { | |
if ($flags["verbose"]) var_dump($res); | |
foreach ($res["user"] as $user) { | |
if (is_array($user["email"])) { | |
foreach ($user["email"] as $email) { | |
if ($email === $req["email"]) { | |
$userid = $user["id"]; | |
} | |
} | |
} else if ($user["email"] === $req["email"]) { | |
$userid = $user["id"]; | |
} | |
} | |
} | |
if ($userid === null) { | |
// Create a new userid | |
$res = add_user($req["email"]); | |
if ($flags["verbose"]) var_dump($res); | |
if (! is_array($res["user"]) || count($res["user"]) !== 1) { | |
user_error("invalid res=" . print_r($res, true)); | |
} | |
$userid = $res["user"][0]["id"]; | |
} | |
// 2. create ticket | |
$res = add_ticket($req["email"], $req["subject"], $req["contents"]); | |
if ($flags["verbose"]) var_dump($res); | |
$ticket = $res["ticket"][0]; | |
// 3. change ticket owner | |
$res = update_ticket($ticket["_attributes"]["id"], [ | |
"email" => $req["email"], | |
"userid" => $userid, | |
"ownerstaffid" => $userid | |
]); | |
if ($flags["verbose"]) var_dump($res); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment