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.

39 comments

  • 0

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

  • 0

    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

  • 0

    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!

  • 0

    Hello All,

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

    Thanks.

    Nambi.

  • 0

    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";
    }
    }
    //

  • 0

    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";

      }

      }

    }

    ?> 

  • 0

    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?

  • 0

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

  • 0

    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.

  • 0

    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

  • 0

    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;challenge=rrk6cgi8kidl9bl">redirected</a>.</body></html>

     

    What's that all about?

  • 0

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

  • 0

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

  • 0

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

  • 0

    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.

  • 0

    I cant get it to display anything other than 

    Location header not found

    What am I doing wrong?  Please help.

  • 0

    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 :)

  • 0

    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,

     

  • 0

    hi Mmajoris,

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

  • 0

    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?

  • 0

    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...

  • 0

    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

  • 0

        $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
    *

  • 0

    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?

  • 0

    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?

  • 0

    Can anyone post the HTML as well please?
    Thx! 

  • 0

    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.

  • 0

    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

  • 0

    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

  • 0

    @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

Please sign in to leave a comment.