3

Submit ticket from PHP

We encourage customers to make use of our API in those cases where Zendesk's user portal isn't flexible enough or you want to embed a simple form on your own site.

Here is a very simple script that submits a ticket from PHP. 

<?php

$url = "http://SUBDOMAIN.zendesk.com/tickets.xml";
$request = "<ticket><subject>I need help!</subject><description>PHP Test</description><requester\_email>requester-email@domain.com</requester\_email></ticket>";
$headers = array('Content-type: application/xml','Content-Length: ' . strlen($request));

$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, 'USERNAME:PASSWORD');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_HEADER, true);

$http_result = curl_exec($ch);
$error = curl_error($ch);
$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);

curl_close($ch);

if ($error) {
print "<br /><br />$error";
} else {
if (preg_match("/Location: .*\/tickets\/(\d*).xml/", $http_result, $matches)) {
$ticket_id = $matches[1];
print "Ticket submitted: $ticket_id\n";
}
print "<br /><br />Location header not found";
}

?>

You must replace three strings in the script before running it:

  • SUBDOMAIN is the subdomain of your helpdesk, e.g. myhelpdesk.zendesk.com
  • LOGIN must be the email address of an agent registered in your Zendesk account
  • PASSWORD must be the password of the agent 

You can also pass custom fields in by adding a something like this:

<fields><51>Custom field value</51></fields>

inside the <ticket></ticket> tags. The ID of the custom field is shown in the sidebar when you edit the field under Manage|Ticket fields.

 

38 comments

Please sign in to leave a comment.