Forums/Community/Community tips & tricks

Submit ticket from PHP

Thomas Pedersen
posted this on December 16, 2008 16:40

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.

 

 

Comments

User photo
Kevin

How would you get the ticket # from this request? ie, if you want to auto-add a comment or attache a file?

February 23, 2009 12:43
User photo
Alexander Aghassipour
Zendesk

The ticket ID is in the location part of the response, which should be in $http_result in the above example.

Status: 201 Created
Location: http://account.zendesk.com/tickets/#{new-ticket-id}.xml

February 23, 2009 13:48
User photo
Neenad
scteam

Thanks for this topic. I'm integrating an existing recurring ticket system into zendesk. I've been able to insert tickets using http request from a python script. I've migrated this php to python, and I can post it here once complete. I've run into a block currently and I hope someone can help:

 

Our existing system has users to be assigned tickets to. Since the id on zendesk is different from our system, I was wondering if there was a way I could assign tickets to assignee's email instead of assignee id, similar to using <requester-email>.

I've tried <assignee-email>, <assignee> and <assignee-id> with email but none works. <assignee-id> with zendesk id of user works, but for that I'll have to keep a mapping of zendesk-id mapping to existing users of our system.

 

Furthermore, is there a list of valid acceptable xml tags for POSTing a ticket? Maybe I'm looking in the wrong places, but I couldn't find anything comprehensive other than what look like bits and pieces in the examples.

 

Thanks in advance!

March 21, 2009 09:08
User photo
Nambi Balasubramaniyam

Hello All,

How do we attach a file? any help would be appreciated.

Thanks.

Nambi.

March 25, 2009 11:19
User photo
Nambi Balasubramaniyam
in order to retrieve the ticket number correctly, following code needs to be changed from 
//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";
}
//
to this

//
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";
}
else
{
print "<br /><br />Location header not found";
}
}
//
April 17, 2009 13:42
User photo
Matthew Moore

I have taken this a little further and made something you then could embed in an iFrame

 

<?php

 

$Email = $_POST["Email"];

$Issue = $_POST["issue"];

$Descript = $_POST["descript"];

if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form

?>

<html> 

<head> 

<title>Personal INFO</title> 

</head> 

<body> 

<form method="post" action="<?php echo $PHP_SELF;?>">

Email:<input type="text" size="50" maxlength="50" name="Email"><br />

Issue:<input type="text" size="50" maxlength="50" name="issue"><br />

<textarea rows="5" cols="20" name="descript" wrap="physical">Enter a description of the problem</textarea><br />

<input type="submit" value="submit" name="submit"><br />

</form>

<br />

 

</body>

</html>

<?

}

else {

$url = "http://Your Zen desk account.zendesk.com/tickets.xml";

$request = "<ticket><subject>$Issue</subject><description>$Descript</description><requester_email>$Email</requester_email></ticket>"; 

$headers = array('Content-type: application/xml','Content-Length: ' . strlen($request));

 

$ch = curl_init();

curl_setopt($ch, CURLOPT_USERPWD, 'email goes here:The 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";

           }

  else

 {

     print "<br /><br />Location header not found";

  }

  }

}

?> 

September 03, 2009 19:06
User photo
rich breton

it doesnt look like anyone here is using X-on-behalfof is there a quick and easy way of doing this if i dont know ther persons password?

December 04, 2009 09:41
User photo
Skip Moore
Zendesk

the X-on-behalfof can be put in the headers array. You still need the agents login and password to make it work. 

December 04, 2009 10:03
User photo
Laimonas

Hi, how to add (attach) file (like MS word document), when a client wants to submit a new ticket? I did not find such a possibility.

January 04, 2010 08:43
User photo
Skip Moore
Zendesk

You have to upload the attachment and then you can attach it to a ticket or create a new ticket with the attachment token. Read more on the attachment API here http://www.zendesk.com/api/attachments

January 04, 2010 21:22
User photo
Morgan Catlin

That link to attachments is pretty useless.  When I attempt to upload an attachment NOT using CURL (e.g. Plain Ol' HTTP via Java), I keep getting this returned:

<html><body>You are being <a href="http://blah.zendesk.com/uploads.xml?filename=test.txt&amp;chall...>

 

What's that all about?

March 09, 2010 22:20
User photo
Dennis Monsewicz

I am looking to pull ALL of the tickets in our system... how could I use to cURL to accomplish this?

August 04, 2010 08:11
User photo
Brian Gerber

What tags do I use to edit the status group and assignee?

October 26, 2010 17:13
User photo
Brian Gerber

Also, is there a way to not send an email to the end-user when using this?

October 26, 2010 17:39
User photo
David Murphy
Rackspace

I am following this code above but I am getting the following headers...

 

HTTP/1.1 302 Found
Server: nginx/0.6.35
Date: Wed, 17 Nov 2010 20:09:21 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 302 Found
P3P: CP="NOI DSP COR NID ADMa OPTa OUR NOR"
Location: https://XXXXXX.zendesk.com/tickets.xml
X-Runtime: 49
Set-Cookie: _zendesk_session=<REMOVED>; path=/; HttpOnly
Set-Cookie: zendesk_user_version=users%<REMOVED>-20101011183231; path=/
Cache-Control: no-cache
Connection: close

<html><body>You are being <a href="https://<REMOVED>.zendesk.com/tickets.xml">redirected</a>.</body></html>

 

 

It is not behaving as expected, if I enable follow then its routes to a normal  default view page, so auth is working, and I am assuming something is wrong with the ticket creation post but I cant find out what.

November 17, 2010 12:20
User photo
Mmajoris
panopto

I cant get it to display anything other than 

Location header not found

What am I doing wrong?  Please help.

January 21, 2011 14:16
User photo
Lorenzo Vigentini

The top of the article showing how to add a ticket is very good.

Can anyone of the experts out there show a snippet of code on how to add/edit a user after finding if this user exists based on the email address? (and yes, if you ask i have looked at the php wrapper in another post, but a good commented example will be great to actually understand what and how the API can be used in practice for those of us who are not prgrammers :)

February 26, 2011 18:49
User photo
GoliveMedia

Hi,

 i just want to integrate zenddesk API with my PHP script . i am able create a ticket using that API .and i can view it in my zendesk.com . i would like to know how to send type,priority,groups,assignee, attachment etc, along with that ticket and those i want to see in zendesk.com (in my account ).

 

thanks in advance,

 

March 18, 2011 04:27
User photo
GoliveMedia

hi Mmajoris,

 if you are not  providing proper info like email id,issues, and description you will get this error. 

March 18, 2011 04:30
User photo
Nicholas Van Dusen

I can't get this to work either; always returns the "You are being redirected..." link. I'm on a trial version of Zendesk, with SAML enabled. Could this possibly be the problem?

April 06, 2011 08:54
User photo
Nicholas Van Dusen

I found the issue - I was using https:// in the URL. When I changed this to http:// everything else fell into place.

Unfortunately, I still have to use the Zendesk password rather than the SAML user's password...

April 06, 2011 09:06
User photo
Gopal Rathod

Hi, how to add (attach) file (like MS word document), when I wants to submit a new ticket? I did not find such a solutions.
i have already follows the http://www.zendesk.com/api/attachments

Thanks
Gopal

April 11, 2011 01:56
User photo
Gopal Rathod

    $Email = $_POST["Email"];
    $Issue = $_POST["issue"];
    $Descript = $_POST["descript"];
    $upload = $_FILES["upload"]["name"];
    //$upl = '@/'.getcwd().'/'.$_FILES['upload']['name'];
    $upl = '@'.$_FILES['upload']['name'] ;

    $url = "http://eminiacademy.zendesk.com/tickets.xml";
    //$url = "http://helpdesk.zendesk.com/uploads.xml?filename=$upl";
    $request = "<ticket><subject>$Issue</subject><description>$Descript</description><requester_email>$Email</requester_email><uploads>$upl</uploads></ticket>";
    $headers = array('Content-type: application/xml','Content-Length: ' . strlen($request));
    //$headers = array('Content-Type: application/binary','Content-Length: ' . strlen($request));

Mail:

Your request (#17) has been received, and is being reviewed by our support staff.

To review the status of the request and add additional comments, follow the link below:
http:/........zendesk.com/tickets/17

Problem:

I goe the Ticket number#  but i did not get my attachment link or detail.

 How can i solved it?

Thanks
Gopal




April 11, 2011 03:29
User photo
Logan B. Lehman

Fatal error: Call to undefined function curl_init()

That is based off of Matthew Moore's code. I have one other question besides fixing this error, and that is:

How do I know in the $request variable, what format to put the XML in? And how would I add a request based off a form that uses conditional dropdown boxes?

October 06, 2011 16:48
User photo
Logan B. Lehman

Nevermind, I just fixed all my own problems. But there is one more question that I cannot figure out.

In the $request variable, if I add a custom field, and that field is in the format of:

Product::Type::Version

How can I map the end user variables, such as $Product, $Version, etc. to actually come back to Zendesk in this form? As I said in the previous post, I have a form that specifies multiple products, and depending on which one you choose, it will populate those fields with different version numbers of the product. Is there any way to take the values in those fields, and pass them through using Zendesk API in the form of Product::Type::Version?

October 06, 2011 18:24
User photo
Gert Boers

Can anyone post the HTML as well please?
Thx! 

October 25, 2011 04:57
User photo
Fgardoqui

The following code worked for me:

$url = "http://yoursite.zendesk.com/api/v1/uploads.xml?filename=yourfilename";
$file = fopen(pathToFile/yourfilename, "r");
$size = filesize(pathToFile/yourfilename);
$fildata=fread($file,$size);
$header= array('Content-type: application/binary' );
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, "user:password");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS ,$fildata);
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_INFILE, $file);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER ,0); // DO NOT RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); // RETURN THE CONTENTS OF THE CALL
$data = curl_exec($ch);
$error = curl_error($ch);
$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);
curl_close($ch);

In the return XML you will finde the token id for the file uploaded, you must include this in the request when creating the ticket it is suposed to be attached to:

<ticket>

  <subject>yourticketsubject</subject>

  <description>yourticketdescription</description>

  ...

  <uploads>tokenRealtedtoFiles</uploads>

</ticket>

 

Hope this helps.

November 29, 2011 10:04
User photo
Roshtulum

I am getting same error as two users here. I am using trial version. Is this the reason?

Unprocessable Entity
Server could not parse XML
Location header not found
March 09, 2012 20:01
User photo
Jim Hoffman

I am getting a Version related error in the CURL response using the sample code.

"X-API-Deprecated: Please use /api/v1/tickets.xml"

 Anyone have any ideas

May 02, 2012 13:10
User photo
Skip Moore
Zendesk

@Jim we are moving to a new API using JSON, you can still use the XML by call the /api/v1/ but check out http://developer.zendesk.com/ for the new API

May 02, 2012 13:35
User photo
Jim Hoffman

Skip,

Thanks for the response. 

Don't mean to be lazy.  I am sure the new API is great, but if I can use what I have already written using the old sample code I would be very happy.

Thanks,

Jim

 

I changed:

$url = "http://section101.zendesk.com/tickets.xml"

to

$url = "http://section101.zendesk.com/api/v1/tickets.xml";

I no longer get the "X-API-Deprecated" message but I still get valid response or Ticket

The response now is:

You are being <a href="https://section101.zendesk.com/api/v1/tickets.xml">redirected</a>

The code I am using is:

$url = "http://section101.zendesk.com/api/v1/tickets.xml";

$request = "<ticket><subject>I need help!</subject><description>PHP Test</description><requester_email>info@section101.com</requester_email></ticket>";

$headers = array('Content-type: application/xml','Content-Length: ' . strlen($request));

$ch = curl_init();curl_setopt($ch, CURLOPT_USERPWD, {email}:{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";
}
else
{
print "<br /><br />Location header not found - ".$http_result;
}
}

 

 

 

 

 

 

May 02, 2012 18:12
User photo
Skip Moore
Zendesk

@Jim your change to the URL should be the only thing you need to do. Are you running into problems?

May 02, 2012 18:22
User photo
Jim Hoffman

Not getting a Ticket posted to the account and the CURL response now is:

You are being <a href="https://section101.zendesk.com/api/v1/tickets.xml">redirected</a>

I am sure it is something I am missing, but out of ideas.

May 02, 2012 18:44
User photo
Eric Shen
Zendesk

@Jim, looks like you have SSL enabled on your account.  Change "$url = "http://section101.zendesk.com/api/v1/tickets.xml";" to $url = "https://section101.zendesk.com/api/v1/tickets.xml"; and you should be good.

May 02, 2012 18:50
User photo
Skip Moore
Zendesk

@Jim, it looks like we are forcing all API connections through https so change the URL to https:// and  use the following options in your cURL set up

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt
($ch, CURLOPT_SSL_VERIFYPEER, false);
May 02, 2012 19:08
User photo
Jim Hoffman

Thanks so much.  The SSL was the trick.  Works great.

May 03, 2012 07:11
User photo
Victor ElBiady

I tried this script but this what I get as result

 

Location header not found

Any suggestion!

Victor

 

March 25, 2013 14:16
User photo
Adam
Zendesk

Hi Victor,

 

This article is pretty old. You might have better luck with this one: https://support.zendesk.com/entries/21562288-Creating-a-Custom-HTML...

April 15, 2013 11:24