Forums/Community/Community tips & tricks

PHP API Library

Brian Hartvigsen
posted this on April 08, 2009 10:26

I've been working on a PHP class library that eases access to the ZenDesk API.  It's available via Google Code at http://code.google.com/p/zendesk-php-lib GitHub at https://github.com/tresni/zendesk-php-lib/.  Can use CURL with PHP 4 or 5 if it is available, otherwise it uses stream_context_create / file_get_contents and likely requires PHP 5.1.0.

require_once 'Zendesk.php';

$zd
= new Zendesk(ACCOUNT, USERNAME, PASSWORD);

// returns users list in XML format (default)

$result = $zd->get(ZENDESK_USERS);

// swith to JSON
$zd->set_output(ZENDESK_OUTPUT_JSON);

$result = $zd->get(ZENDESK_USERS);

More examples at http://code.google.com/p/zendesk-php-lib/wiki/Examples .  Hope someone finds it useful ;-)

 

Comments

User photo
Jason Knudsen
Tungle Corporation

This is really awesome. Thanks for your hard work Brian! It's very appreciated.

April 28, 2009 07:18
User photo
Morten Primdahl
Zendesk

Absolutely! Thanks Brian, stunning.

April 28, 2009 11:36
User photo
Jossi Fresco

Fantastic! Thank you/

July 12, 2009 08:45
User photo
Tom Deryckere

Great library. Works perfect

October 07, 2009 11:09
User photo
rich breton

what if you dont know the users password, is there a way to use the zendesk X-On-Behalf-Of: joe.enduser@theendusers.com header?

December 04, 2009 09:47
User photo
Tanya

This is awesome - thank you so much for putting this together and sharing it with us!

January 26, 2010 09:21
User photo
Brian Hartvigsen

Use $arg['on-behalf-of'] = 'joe.enduser@theendusers.com'; . So for example:

$result = $zd->create(ZENDESK_TICKETS, array(
   
'details' => array(
       
'description' => 'My printer is not working',
       
'requester-id' => 3,
       
'priority' => 4,
       
'set-tags' => 'pr facility'
   
),
   
'on-behalf-of' => 'joe.enduser@theendusers.com'
));
February 02, 2010 22:04
User photo
Tanya
Hello Brian, I am trying to use this library for forum search. How would you recommend adding tag specific search into the URL? Also, there's a bug in how the GET parameters get assembled in this code ('&') (that character is missing between the parameters in the final URL): if (is_array($opts['query'])) { $query = '?'; foreach ($opts['query'] as $key=>$value) $query .= urlencode($key).'='.urlencode($value); $url .= $query; }
February 08, 2010 10:43
User photo
Adam

Does this work for users, getting list of tickets, etc?

February 28, 2010 05:53
User photo
Brian Hartvigsen

Tanya: It doesn't appear to be possible to search forums using the API (unless it's implemented in the standard search rest api, but that appears to relate specifically to tickets.)  If entries support searching the same way users do, then you would do something like $array('query' => 'query=tags:blah keyword keyword2') with the options array.  I've never tested that. The GET parameters building is definitely a bug, only applies if you pass in the options as an array.  I've fixed it locally, need to upload.  Just want to confirm so other tweaks I've made.

Adam: Sure, use the 'on-behalf-of' argument mentioned a couple posts previously, than something like:

$result = $zd->get(ZENDESK_REQUESTS, array(
    
'on-behalf-of' => 'joe.enduser@theendusers.com'
));

That will get a list of all tickets 'joe.enduser@theendusers.com' opened which can then be displayed to the end user.  For most things, just follow the API documentation and it should work.

February 28, 2010 23:34
User photo
Adam

Thanks Brian all working well so far.

March 01, 2010 05:04
User photo
Adam

Brian I'm struggling with updating a ticket with an end user comment. Currently got this:

 

 

$result = $zd->update(ZENDESK_TICKETS, array(
   'id' => '6',
   'details' => array(
       'comment' => 'My printer is not working',
      ),
   'on-behalf-of' => 'joe.enduser@theendusers.com'
));

$result = $zd->update(ZENDESK_TICKETS, array(   

 'id' => '6',

 'details' => array(        'comment' => 'My printer is not working',

  ),

 'on-behalf-of' => 'joe.enduser@theendusers.com'

));

 

Obviously I'm using the right email address. I think I'm doing the ticket id wrong?

March 01, 2010 07:44
User photo
Brian Hartvigsen

Hey Adam, you aren't doing anything wrong.  That's one of those "tweaks" I was mentioning in my reply to Tanya.  Adding a comment doesn't work correctly in that build since it wraps the XML data in a <ticket> object instead of a <comment> object.  Copy the following code and replace the existing $args['data' ] = ... in function update($page, $args):

 

        if ($page != ZENDESK_REQUESTS && !($page == ZENDESK_TICKETS && isset($args['details']['value'])))       
            $args['data'] = $this->_build_xml($args['details'], $this->_singular($page));
        else

        if ($page != ZENDESK_REQUESTS && !($page == ZENDESK_TICKETS && isset($args['details']['value'])))
                  $args['data'] = $this->_build_xml($args['details'], substr($page, 0, -1));
        else
                  $args['data'] = $this->_build_xml($args['details'], 'comment'); 

 see if that resolves the issue for you.  I'm surprised that never came up sooner.

March 01, 2010 09:42
User photo
Brian Hartvigsen

Not sure what happened w/ my copy paste there.  But that code should simply be:

        if ($page != ZENDESK_REQUESTS && !($page == ZENDESK_TICKETS && isset($args['details']['value'])))
                  $args['data'] = $this->_build_xml($args['details'], substr($page, 0, -1));
        else
                  $args['data'] = $this->_build_xml($args['details'], 'comment');

March 01, 2010 09:51
User photo
Adam

Thanks Brian !

Still not working for me though. Not getting any errors logged and nothing back from $result. Will have a think!

March 01, 2010 10:07
User photo
mantra1

Hi, I'm using PHP API Library Is there a way to get all the tickets of a particular end-user ?

March 25, 2010 00:50
User photo
Chris Houseknecht

Brian-

Nice job!  We're using your library to connect external, hosted apps to Zendesk. 

I found something that might be of interest to you or others. The _singular function only returns the first character of the input parameter, if the input parameter does not contain 'ies'. When trying to create a ticket, the string 'tickets' is passed in, and the result is 't'.

To correct this, I changed the last line of the _singular function to the following:

else return substr($noun, 0, strlen($noun) - 1); 

This causes it to return the full string minus the last character.  In other words, pass in 'tickets', and the result is now 'ticket'. 

Hope this helps!

 

April 18, 2010 16:14
User photo
Chris Houseknecht

It just occurred to me, there is a simpler way to write that...

else return substr($noun, 0, -1);

No need for a call to strlen().  All that was missing was a '-'. 

April 18, 2010 22:26
User photo
Brian Hartvigsen

@mantra1 - ZenDesk pages things so no

@Chris Houseknecht - Good catch, I've updated the download on Google Code.

April 21, 2010 15:11
User photo
Samuel Nilsson

Hi, thanks for spending time on the library. I´ve been trying to get it working but it fails somewhere.. I can get users and tags but not any entries nor posts and requests. I also tried to do a search with no success. What am  I doing wrong:

$result = $zd->get(search, array('query' => 'type:ticket', 'on-behalf-of' => 'test@domain.com'));

May 10, 2010 18:40
User photo
Sahil Parikh

Hello!

Thanks for the library. Before we start to use it I just wanted to know if it does the following. Please let me know:

1. Adding a new ticket

2. Adding a new ticket with attached files

3. Adding a comment to an existing ticket

4. Closing a ticket

5. Adding time to a ticket

June 08, 2010 21:16
User photo
Dennis Monsewicz

I am trying to utilize the api library that you have created but when I try to retrieve all of the tickets in our system... I get a blank page... not that there are any errors but that the script isn't pulling all of the tickets.

require_once "Zendesk.lib.php";
        
        $zd = new Zendesk("account", "username", "password", true);
        $result = $zd->get(ZENDESK_TICKETS);
        
        echo $result;

August 04, 2010 07:40
User photo
Eric Dahl
groupon

I am trying to figure out how to make a simple form that I can manually enter my user's external ID and have it populate.

can anyone point me in the right direction?

September 13, 2010 17:25
User photo
Tikhon

Hi Brian!

Useful lib, thanks for that.

Have a small question - Is it possible to sync users' passwords as well?

September 15, 2010 22:06
User photo
Tikhon

Me again :-)

Answering to my question - yes, it's possible.

 

'id' => $zendesk_id,
        'details' => array(
              'email' => $account->mail,
              'name' => $account->name,
              'external-id' => $account->uid,
 'password' => $edit["pass"], //password synchronize
            ),
          )

'id' => $zendesk_id,

'details' => array(

'email' => $account->mail,

'name' => $account->name,

'external-id' => $account->uid,

'password' => $edit["pass"], //password synchronize

),

)

Maybe it will be useful to anyone

 

September 16, 2010 21:38
User photo
Tikhon

ops, sorry

don't know how to manage that parser

September 16, 2010 21:39
User photo
Nicholas Van Dusen

I'm not seeing any downloads on Google Code, am I missing something? I'd really like to use this library!

April 06, 2011 08:52
User photo
Tikhon

Hey Nicholas, try look here http://zendesk-php-lib.googlecode.com/svn/trunk/

 

Cheers, Tikhon

April 06, 2011 13:20
User photo
Nicholas Van Dusen

Thanks, Tikhon!

April 06, 2011 13:31
User photo
Brian Hartvigsen

Yeah.  I got rid of the download since I was doing a lot of changes in a very short period of time and realized it wasn't actually very helpful in this case to have a separate download.  As the main page says:

"Grab the latest download from repository."

April 06, 2011 17:12
User photo
Nicholas Van Dusen

I ran into a problem when trying to include custom field values in my ticket create calls - there was no way to specify multiple ticket-field-entry items, since only the last one in the array would be used.

To fix this, I modified the _build_xml function:

 private function _build_xml($data, $node, $is_array = false)
 {
  $node = str_replace(strrchr($node,':'),'',$node);
  $xml = "<$node" . ($is_array && strpos($node,'-entry') == false ? ' type=\'array\'>' : '>');

This allowed me to name my array items thusly:
   'ticket-field-entry:123'=>array('ticket-field-id'=>'123','value'='true')
   'ticket-field-entry:456'=>array('ticket-field-id'=>'456','value'='Yes')

The strpos check for -entry ensures that "type='array'" is not appended to the ticket-field-entry items, which seems to break them when making the final API call.

April 11, 2011 13:25
User photo
Brian Hartvigsen

Or you could do it like this which already works:

'ticket-field-entries' => array(
array(
'ticket-field-id' => 123,
'value'=> true
),
array(
'ticket-field-id' => 245,
'value' => 'Yes'
)
)
)

'ticket-field-entries' => array(
array(
'ticket-field-id' => 123,
'value'=> true
),
array(
'ticket-field-id' => 245,
'value' => 'Yes'
)
)

Which outputs:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; background-color: #f0f0f0}

<ticket-field-entries type='array'>
<ticket-field-entry>
<ticket-field-id>123</ticket-field-id>
<value>1</value>
</ticket-field-entry>
<ticket-field-entry>
<ticket-field-id>245</ticket-field-id>
<value>Yes</value>
</ticket-field-entry>

</ticket-field-entries>

This matches what the documentation says at http://www.zendesk.com/api/tickets .

April 11, 2011 14:31
User photo
Brian Hartvigsen

Ignore the second 'ticket-field-entries' array there, not sure where the double post came from.

April 11, 2011 14:33
User photo
Nicholas Van Dusen

That's even better, thanks Brian!

April 11, 2011 14:39
User photo
Matt Fairbrass

Hi Brian,

First off let me thank you for taking the time to write this class, exactly what I have been looking for. I have read all of the comments on this ticket and have been through the examples on the google code site but I am running into some issues when creating tickets.

I can use the get method to retrieve all users, so I know that I am being authenticated correctly, but when I try to create a new ticket the result is null, I am using the example which you posted above, what am I do wrong?

 

require_once ('zendesk.php');

$zd = new Zendesk(ACCOUNT, USERNAME', PASSWORD);
$result = $zd->create(ZENDESK_TICKETS, array(
          'details' => array(
                    'description' => 'My printer is not working',
                    'priority' => 4,
                    'set-tags' => 'pr facility'
           ),
           'on-behalf-of' => 'joe.enduser@theendusers.com'

));
                   
echo $result;

May 13, 2011 02:21
User photo
Matt Fairbrass

Having said that I was being authenticated I dumped out the $this->result and got the following:

array(3) { ["header"]=> string(453) "HTTP/1.1 401 Unauthorized Server: nginx/0.8.53 Date: Fri, 13 May 2011 10:25:46 GMT Content-Type: application/xml; charset=utf-8 Connection: keep-alive Status: 401 Unauthorized WWW-Authenticate: Basic realm="Web Password" Content-Length: 41 Set-Cookie: _zendesk_session=BAh7CDoMYWNjb3VudGkCWGg6D3Nlc3Npb25faWQiJTMyMGE4OTRhYTY2MGMwYzc5MjNkNDE5YmMxNzFmY2NlOgdpZCIUYTJ0bGwzeDhwYjZwN2pr--b3d789288b56b30348e2b8eed624e049f8f8e5f5; path=/; HttpOnly " ["content"]=> string(41) "<error>Couldn't authenticate you</error> " ["code"]=> int(401) }

I have double and triple checked my username and password and they are correct. Am I right in thinking that the account is just the first part of the domain? So for me it would be clearbooks, seeing as our zendesk is http://clearbooks.zendesk.com correct?

May 13, 2011 03:30
User photo
Matt Fairbrass

Never mind, I worked it out. I misunderstood what on-behlf-of meant and that was why the basic authentication was failing. Silly mistake which has cost me 5 hours :-(

May 13, 2011 07:09
User photo
Mark Smith
I'm stuck!!

I've had to modify the code slightly to post directly to the support.domain.com address, not the
domain.zendesk.com address as I always got a 301 header.

Now that's been sorted I get a 406 error with the code below, or a 401 if I add the 'X-On-Behalf-Of'
as documented a few posts up.

I have exhausted all possibilities I can think of, even going through the 'My Account' section on ZenDesk
to see if I need to activate it. I can't see such an option.

This is the code I'm using:
$result = $zd->create(ZENDESK_TICKETS, "details" => array { ["description"] => "this is a test" ["requester-name"] => "mark smith" ["requester-email"] => "webmaster@domain.com" ["priority"] => 0 } );

The result of $result is 0.

The result of $this->result just above the line: if ($this->result['code'] == 201) { in the create method is:
array(3) { ["header"] => string(662) "HTTP/1.1 406 Not Acceptable Server: nginx/0.8.53 Date: Sun, 15 May 2011 10:18:16 GMT Content-Type: application/xml; charset=utf-8 Connection: close Status: 406 Not Acceptable P3P: CP="NOI DSP COR NID ADMa OPTa OUR NOR" Content-Length: 84 Set-Cookie: _zendesk_session=BAh7DDoMYWNjb3VudGkD3icBIhx3YXJkZW4udXNlci5kZWZhdWx0LmtleWkEMiPeAToNYXV0aF92aWEiEkJhc2ljU3RyYXRlZ3k6D3VwZGF0ZWRfYXRsKwdoqM9NOg9zZXNzaW9uX2lkIiU5MzI3MWQ3YjgwZDVjNWJmNDdlZjE0ODYxYTQ1ZGZiMiITd2FyZGVuLm1lc3NhZ2V7ADoHaWQiFTVyYmxsNm9reS15dDNqcWo%3D--bafcce4ba28ab721aa8b286d32dca3dbe557c219; path=/; HttpOnly Set-Cookie: oid_user=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT Cache-Control: no-cache" ["content"] => bool(false) ["code"] => string(3) "406" }

I appreciate any help with this.
May 15, 2011 03:29
User photo
Brian Hartvigsen

Hi Mark,

Can you explain your situation a little more?  Are you using SSL with your domain if accessing the site directly?  Are you making the Zendesk object HTTPS enabled?

Normally a 406 indicates that you sent something incorrectly (sending JSON instead of XML or malformed XML), some required component was missing from the request, or you are trying to access an HTTPS enforced support desk over HTTP.  Note that these are just the times I have seen it happen personally, the error code is not documented by Zendesk.

If you continue to have issues, open a bug on the Google Code project, include your patch and details of your Zendesk setup.  Add a Label to the issue that simply states "Private".  That means it is only viewable by Commiters and the person who opened the issue.

May 15, 2011 08:50
User photo
Mark Smith

Hi Brian,

I've found the issue. Firstly I didn't have cUrl enabled on my server (so the reporting of error messages wasn't as good) and secondly it seems the priority field is no longer valid.

I have removed the priority field out of the details array and it now works as expected.

Many Thanks

Mark

May 15, 2011 13:44
User photo
Brian Hartvigsen

Looks like that should be 'priority-id' => 0 according to the documentation at http://www.zendesk.com/api/tickets .  Glad you found it, it does seem that you will get better error messaging with cURL enabled as it normally tells you exactly what went wrong on a 406 (in my testing today for other stuff.)

May 15, 2011 13:50
User photo
Chris Ball

It's not clear to me how to update just a field on a ticket. The code below is not working for me.
Also, is it possible to append to a field, rather than replace the data?

Thanks

 

<?php
REQUIRE_ONCE './Zendesk.php';
 $zd = new Zendesk(ACCOUNT, USER, PASSWORD);
 $result = $zd->update(ZENDESK_TICKETS, array (
 'id' => ####,
    'ticket-field-entries' => array (
   array(
'ticket-field-id' => #######,
'value' => 'Some Content'
)
)
 ));
?>

May 17, 2011 09:33
User photo
Chris Ball

A follow up, I am getting the following error when I echo $zd->result['content']

"You do not have access to ticket #0. It may have been deleted or it may belong to another user."

I have enter a ticket number of 1996.

May 17, 2011 09:46
User photo
Eric Dahl
groupon
you first need to pull the current field content, then append.
May 17, 2011 09:47
User photo
Chris Ball

Thanks for the tip on the append.

May 17, 2011 09:48
User photo
Brian Hartvigsen

Chris, the "ticket-field-entries" array should be in a "details" array.  Should look like:

<!-- p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #1b360c; background-color: #f0f0f0} -->

<?php

    require_once './Zendesk.php';

    $zd = new Zendesk(ACCOUNT, USER, PASSWORD);

    $result = $zd->update(ZENDESK_TICKETS, array (

        'id' => ####,

        'details' => array(

           'ticket-field-entries' => array (

                array(

                    'ticket-field-id' => #######,

                    'value' => 'Some Content'

                )

            )

        )

    ));

?>

 

Beyond that, I'd need more data then what you are posting to know what's going on.  For example, the exact code you are using (open as a bug with a label of "Private" on Google code to protect your personal information).  The code you posted would have thrown an error due to the lack of the details entry in the array, so I know that's not the exact code.  Also the output of the below after the failure:

var_dump($zd);

That would be a good start.  Again, I would open a bug on Google Code with a label of Private and all the proper details including the revision number you are using.

May 17, 2011 11:45
User photo
Brian Hartvigsen

Both r23 and r24 were bugged.  r25 finally resolves all the issues with forcing XML for update, delete, & create.  Sorry for any inconvenience folks!

May 17, 2011 16:04
User photo
Gary Smith

Any chance of getting an example of how to list all open tickets? I see that the question has been asked a couple of times but I didn't see a response. I've tried just using ZENDESK_TICKETS and ZENDESK_REQUESTS in a $zd->get but it comes back empty.

 

Gary

May 17, 2011 18:02
User photo
Gary Smith

I figured it out. Here's a quick example that might help someone - it will display the ticket ID and subject then dump the contents of $tickets.

 

    include "include/Zendesk.lib.php";
   
    $zd = new Zendesk(ACCOUNT, USERNAME PASSWORD);

    // switch to JSON
    $zd->set_output(ZENDESK_OUTPUT_JSON);
    $result = $zd->get(ZENDESK_SEARCH, array('query' => array( 'query' => 'type:ticket')));   
   
    $tickets = json_decode($result);
   
    foreach($tickets as $ticket)
    {
        echo "<br>".$ticket->nice_id.": ".$ticket->subject;
    }
   
    // Display all $tickets objects
    var_dump($tickets);

 

Gary

May 17, 2011 18:49
User photo
Gary Smith

That first line should be:

 

$zd = new Zendesk(ACCOUNT, USERNAME, PASSWORD);

 

... I accidentally deleted a comma

 

Gary

May 17, 2011 18:51
User photo
Brian Hartvigsen

Hi Gary, just wanted to put some additional notes here for you and future readers:

Viewing a list of tickets normally uses ZENDESK_RULES (see  http://www.zendesk.com/api/tickets )  This is for things like Unassigned, Assigned to Me, etc.

ZENDESK_TICKETS is used to work on a single ticket from an agent's perspective.  It can not be used to get a list of tickets.

ZENDESK_REQUESTS is for an end user.  It can be used to get() a list of tickets, but it will return only open tickets, created by the end user.

So if you want a list of all open tickets (without having to search) use:

$zd->get(ZENDESK_RULES, array('id' => ID_OF_OPEN_TICKETS_VIEW));

May 18, 2011 09:32
User photo
Chris Ball

OK. Thanks to Brian's help I can now put data into a custom ticket field in Zendesk.

Thanks Brian!

But, I do have another question. Is there a way to insert a line break in to that custom field if it is a multi-line text box?

As always, much thanks.

May 18, 2011 17:13
User photo
Chris Ball

One more question (at least for today).

I want to use this library to pull the value of a custom field so that I can append new data to the field when I update it rather than replace the data.

I can pull the correct ticket easily enough. But, I don't know how to get that field's contents into a usable format.

I'm sure this is a basic question, but I really appreciate the help.

Thanks,

Chris

May 18, 2011 18:16
User photo
Eric Dahl
groupon

How do I post a comment to an existing ticket?

October 18, 2011 11:47
User photo
Eric Dahl
groupon

update:

I figured it out.

$result = $zd->update(ZENDESK_TICKETS, array(
'id' => 12345,
'details' => array(
'value' => 'comment stuff here'
)
));

October 18, 2011 14:55
User photo
Wicked Nico

Hello,

I have an issue with $zd->get(ZENDESK_RULES), that does not return anything (empty var).

Is there something special to do to activate this ? Does this work on Regular plan ?

Thanks,

Nicolas

October 25, 2011 01:38
User photo
Brian Hartvigsen

You have to pass a rule (aka view) ID.  See http://www.zendesk.com/api/tickets under List.

$zd->get(ZENDESK_RULES, array('id' => 1234));

There is a default rule, but even it has an id that must be passed.

October 25, 2011 13:02
User photo
Wicked Nico

Thank you, this is fixed :)

October 26, 2011 08:24
User photo
Georg M. Sorst

Hi Brian,

been using and loving your wrapper for a while!

I recently ran into an encoding problem with German Umlaut (ä,ö,ü and the likes). This is what I get from Zendesk:

<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <error>Unprocessable Entity</error>
  <message>Server could not parse XML</message>
</hash>

The problem is definitely caused by the Umlaute. Any idea what PHP function I have to run them through to get it right? I guess htmlentities() won't work as the communication is working on pure XML, not (X)HTML.

October 27, 2011 11:13
User photo
Brian Hartvigsen

Are you using HTML entities or some other encoding before passing information to a function?  I've tested support of international characters post Issue 6 ( http://code.google.com/p/zendesk-php-lib/issues/detail?id=6 .)  The fact that it says "Uprocessable Entity" indicates it's getting an & character, which leads me to believe the information is getting passed in encoded.  The only encoding done in this library is for URL components.  Nothing in the XML (it's <![CDATA[ ]]> escaped where appropriate.)

October 27, 2011 15:29
User photo
Brian Hartvigsen

Actually the more I think about it the more I have to wonder, are you passing the umlaut as a value or as a field name?  Can you give an example?  (Might be better to open this as an issue on http://code.google.com/p/zendesk-php-lib/issues/ )

October 28, 2011 00:32
User photo
Brian Hartvigsen

I just tested with:

$zd = new Zendesk("myZendeskAccount.zendesk.com", "user@name.com", "myPassword", true, true);
$result = $zd->create(ZENDESK_REQUESTS, array(
'details' => array(
'description' => 'Ωå∑ƒåßπƒˆ™£®¬œ…ß˚ƒåߺ®ˆ£ä,ö,üøæå&><sadasd<![CDATA[]]>
Sorry, don\'t have my own ZD instance to test against :(',
'subject' => 'Testing some stuff'
),

));

 

and it went through fine...  Make sure you have the latest version and please open a bug report if you continue to have issues (with details about the query you are trying)

October 28, 2011 01:26
User photo
Georg M. Sorst

Hi Brian,

thanks for the quick reply!

I figured it out...well, partially. The included Smarty would mb_set_encoding('UTF-8') which in turn seems to mess with Zendesk lib's encoding. Why? I have no idea. Anyway, I submitted an issue: http://code.google.com/p/zendesk-php-lib/issues/detail?id=15, maybe it helps.

October 28, 2011 06:31
User photo
Georg M. Sorst

PS: Using mb_internal_encoding('ISO-8859-1') before the request helps...

October 28, 2011 06:32
User photo
Brian Hartvigsen
October 28, 2011 09:02
User photo
CAS Netlink

Hi Brian,

 

I have a ticket that looks something like the one below. My question is - how do I get the details for just that ticket using the 'get' function? Basically, I'm trying to integrate the ability to reply to tickets in my app, so when someone clicks on the subject I need to pull the details for that ticket using the API - my first thought was that I should be able to do a search for the 'nice_id' but that always returns NULL if I try something like:

$result = $zd->get(ZENDESK_RULES, array('nice_id' => $ticketid));

 

stdClass Object
(
    [ticket_field_entries] => Array
        (
        )

    [channel] => 
    [nice_id] => 187
    [initially_assigned_at] => 
    [entry_id] => 
    [created_at] => 2011/10/30 05:46:40 -0700
    [has_incidents] => 
    [resolution_time] => 
    [priority_id] => 0
    [current_tags] =>  
November 02, 2011 06:23
User photo
Brian Hartvigsen

Getting a ticket is an interesting thing.  There are essentially 2 ways to get it, one is as an agent which includes replies and comments (which you may not want to show the end user.)  The other way is as the end-user which limits the returned data to what would be shown if they logged into your Zendesk site.  Check out http://www.zendesk.com/support/api/tickets and http://www.zendesk.com/support/api/rest-introduction for more information.

$zd->get(ZENDESK_TICKETS, array('id' => $ticketid)); // Must be a Zendesk Agent
$zd->get(ZENDESK_REQUESTS, array('id' => $ticketid)); // As End User, might need on-behalf-of

 

ZENDESK_RULES is for getting a specific view of tickets, not an individual ticket.  And it should almost always be array('id' => ...).

November 02, 2011 21:15
User photo
Georg M. Sorst

Brian, thanks for fixing the issues mb_set_encoding(), works perfectly now!

I also just figured out a gotcha thanks to your last comment: when using the on-behalf-of option you have to use ZENDESK_REQUESTS instead of ZENDESK_TICKETS. I'm sure this has been mentioned before but it took me some time, so here it is again :)

November 04, 2011 03:16
User photo
CAS Netlink

Thanks Brian. ZENDESKS_REQUESTS with on-behalf-of was what I needed.

November 04, 2011 07:51
User photo
CAS Netlink

Hello Brian,

Next question. I noticed in an earlier quote someone was saying they couldn't get comments added to existing tickets working. I'm having the same problem. I get a 403 Forbidden error. However, I can create and list tickets using the same auth details.

Here's the var_dump of $zd after the request is sent, followed by the PHP code I'm using to add a comment to the ticket. The ticket ID and the email address for the end user are both valid.

object(Zendesk)#2 (5) { ["account"]=> string(9) "Our Helpdesk" ["output"]=> int(0) ["secure"]=> bool(false) ["curl"]=> resource(7) of type (curl) ["result"]=> array(3) { ["header"]=> string(739) "HTTP/1.1 403 Forbidden Server: nginx/0.8.53 Date: Sun, 06 Nov 2011 15:13:51 GMT Content-Type: application/xml; charset=utf-8 Connection: keep-alive Status: 403 Forbidden P3P: CP="NOI DSP COR NID ADMa OPTa OUR NOR" X-Runtime: 50 Content-Length: 175 

$result = $zd->update(ZENDESK_TICKETS,array(
'id' => $post_data['ticketid'],
'details' => array(
'comment' => $post_data['ticket_msg']
),
'on-behalf-of' => $post_data['useremail']
));

November 06, 2011 07:17
User photo
CAS Netlink

I forgot to add the text version of the error that gets returned:

'You do not have access to this page. Please contact the account owner of this help desk for further help.'

November 06, 2011 07:19
User photo
Brian Hartvigsen

@CAS Netlink -> try:

$result = $zd->update(ZENDESK_REQUESTS,array(
'id' => $post_data['ticketid'],
'details' => array(
'value' => $post_data['ticket_msg']
),
'on-behalf-of' => $post_data['useremail']
));

First, updating ZENDESK_TICKET with a comment makes an administrative comment which will likely never work with on-behalf-of.

Second, it should be "value" and not "comment".

See http://www.zendesk.com/support/api/tickets (search for "PUT /tickets/#{id}.xml" and "PUT /requests/#{id}.xml" to see the difference between the 2 types of comments.)

November 06, 2011 16:06
User photo
CAS Netlink

Thanks again Brian. I honestly don't know why I didn't get that. I pored through the docs and tried various arrays and values. Once I'm done with this integration I'm going to publish my PHP functions that use your library. They might save someone a bit of time. The API docs are good but I know that the transposition of the XML to PHP arrays confuses me a bit - so I'm probably not alone in that struggle :).

Gary

November 06, 2011 18:27
User photo
Filip Dostál

Hi,

Thanks for this library, it is very useful. I just have one issue that I cannot figure out:

What code will I use to update a ticket state? I am using the bellow, which does not seem to work:

$zd->update(ZENDESK_TICKETS, array('id' => $_id, 'details' => array('created-at' => $_created, 'value' => $_comment , 'is-public' => $_is_public), 'status-id' => 2));

Thanks!

Filip

November 13, 2011 03:55
User photo
Wicked Nico

Hello,

I would like to export users but I can only get the first 100.

Is there a trick to get the whole list ?

Thanks,

Nicolas

November 22, 2011 01:26
User photo
Skip Moore
Zendesk

@Nicolas we not paginate the users.xml and only return 100 at a time. http://www.zendesk.com/support/api/users

November 22, 2011 08:13
User photo
Brian Hartvigsen

@Filip 'status-id' should be part of the 'details' array.

@Nicolas to paginate use $zd->get(ZENDESK_USERS, array(..., 'query'=>array('page'=>2))) or $zd->get(ZENDESK_USERS, array(..., 'query'=>'page=2')) .  The second way expects that you have already done the necessary formating/escaping of your query params.

November 22, 2011 10:15
User photo
Wicked Nico

Thank you, this works now ;)

November 22, 2011 12:55
User photo
Filip Dostál

Hey Brian,

Thanks for you help, but I still cannot make this work. I have tried:

$zd->update(ZENDESK_TICKETS, array('id' => $_id, 'details' => array('created-at' => $_created, 'value' => $_comment, 'is-public' => $_is_public, 'status-id' => $_status_id)));

...but this does not work at all, i.e. no comment is posted. Could there be something else wrong?

Thanks,

Filip

November 23, 2011 05:29
User photo
Filip Dostál

In fact, even this simplified version does not work:

$zd->update(ZENDESK_TICKETS, array('id' => $_id, 'details' => array('status-id' => 2)));

November 23, 2011 05:34
User photo
Brian Hartvigsen

Sorry I failed to notice this on your first post, but you can not specify a comment (details->value) and update a ticket in the same call.  Comments are special and require custom XML to work.  So if you are using 'value' to post a comment, nothing else will happen (and Zendesk may well reject the request as malformed.)

The syntax for the simplified version looks correct, if that's not working, supply a var_dump from immediately after the update is call is placed:

$zd->update(ZENDESK_TICKETS, array('id' => $_id, 'details' => array('status-id' => 2)));
var_dump($zd);

and tell us if you are authenticating as a user or administrator.

November 23, 2011 11:20
User photo
Carlos Flores
playdom

Hi Brian,

Thank you for this.  

Not very familiar with PHP so not sure what I'm missing but when I tried to use this (just copy pasted your code in the original post & saved the API in the same directory) I get a 500 Internal server error when I try to echo $zd and $results is empty.

We use SSO so not sure if that makes a difference.

Thanks for any insight you can provide.

 

 

November 23, 2011 19:56
User photo
billy.teves

Hi Brian!,

I am really happy that you have this Class. Zendesk has a poor documentation. . Can you please post a code for tickets that will list all the tickets with pagination? I am not sure on what parameter/s to be added on listing the tickets. 

$result = $zd->get(ZENDESK_RULES, array( 'id' => 24616316));

not sure if this is correct . I really need this. thanks!

November 24, 2011 05:10
User photo
Brian Hartvigsen

@billy.teves - To paginate see the code 6 posts up from yours. That query parameter works for anything that has pagination.

@carlos - try print_r($zd); echo is probably complaining that it can't convert an object to a string.

November 24, 2011 09:40
User photo
billy.teves

Hi again!, 

Why is it that when I use the above code, the return code is 302 redirect?

https://billyhb.zendesk.com/rules/24616316

this is the link of my ticket list

when i use this code: $result = $zd->get(ZENDESK_RULES, array( 'id' => 24616316));

then print_r($zd); it returns 302 redirect? I'm really not sure if I am wrong or is it really not working?

November 24, 2011 15:06
User photo
billy.teves
November 24, 2011 15:23
User photo
billy.teves

got it.! There was a problem on the class. Moving forward. Is it possible to count the ceiling of the tickets?

I am trying to create a pagination here I want to create  <<previous | next>> OR number listing 1 2 3 4 5 6 etc.

November 24, 2011 16:17
User photo
billy.teves

solved it sorry, yeah the sizeof array LOL. 

November 24, 2011 16:19
User photo
billy.teves

Always redirect T_T . . please help. 

Trying to push this data:

$result = $zd->update(ZENDESK_REQUESTS,array(
'id' => $p['ticketid'],
'details' => array(
'value' => $p['ticket_msg']
)
));
November 24, 2011 20:09
User photo
Carlos Flores
playdom

Hi again Brian,

 

Hope you had a nice holiday.  That worked, thanks!  Is there a way to use the created on date field to limit results for users?  (i.e. return all users created after 9/20/2011)

 

Thanks,

C

November 29, 2011 09:19
User photo
Brian Hartvigsen

@billy.teves - Without knowing what lead up to that call I can't help you.  Also, I would need the output of var_dump right after that update call:

var_dump($zd);
var_dump($result); 

Those may give you the information you need to resolve.

@Carlos - I'm not sure that's possible in the Zendesk API.  If it is, my guess would be you would need to do something like:

$zd->get(ZENDESK_USERS, array('query'=>array('query' => 'created-at>9/20/2011')))

If you can't do it through the Zendesk website, the API won't support it.  If you can do it through the website you should be able to do it through the API, but...

November 29, 2011 10:00
User photo
Shaun Tarves

Hi Brian -

I've added support for file uploads. It required a trivial amount of "new" code to do this. How can I contribute this back to the project?

December 01, 2011 07:35
User photo
Brian Hartvigsen

Shaun - The best way is to open an issue ( http://code.google.com/p/zendesk-php-lib/issues/entry ) and attach either your Zendesk.lib.php file or a unified diff.

December 01, 2011 08:36
User photo
Shaun Tarves

I submitted an issue with my file and some instruction. Let me know if anything isn't clear.

December 02, 2011 10:16
User photo
Filip Dostál

Hi Brian,

Thanks for your help with my earlier issues, with your feedback I got everything up and running.

I am now looking for another feature - would you be able to past here a sample code here for merging tickets when you get a chance?

Thanks a lot!

Filip

January 14, 2012 02:27
User photo
Aleksandar

I tried this class and it does not work for fetching users:

[code]

include_once 'Zendesk.php';
$zd = new Zendesk($account, $username, $password);
$result = $zd->get(ZENDESK_USERS);

[/code]

I am getting error: Couldn't authenticate you

I tried this with plain curl and it is working as it should.

Any hint?

March 01, 2012 08:12
User photo
Andreas Lydersen
LogMeIn Rescue Beta

Hi,

I'm struggling with line break in comments. Anybody figured out hot to get the API to not take <br /> or \n or &#xD; literally? My code is like this:

$result = $zd->update(ZENDESK_TICKETS, array(
'id' => 42,
'details' => array(
'value' => 'first line:&#xD;second line',
'is-public' => true,
)
));

Thanks,

Andreas

March 30, 2012 05:02
User photo
Brian Hartvigsen

@Aleksandar - You may need to use HTTPS, it seems most ZenDesk instances default to that being on.  Try initializing like:

$zd = new Zendesk($account, $username, $password, true, true);

 

@Andreas - Open a bug report on the project, with a fuller explanation (including what you are expecting to see in the comment and what you actually see.)  This may take a little more exploration to figure out what's going on and if there is anything that I can do about it in the PHP library.  I'm not sure I've ever bothered trying line breaks, and the API documentation doesn't really say if they are supported or not.

March 31, 2012 11:47
User photo
Andreas Lydersen
LogMeIn Rescue Beta

Thanks Brian,

When I tried to just add a line break in the XML it worked fine. Guess I should stop overcomplicating things...

Andreas

April 12, 2012 00:01
User photo
Patrick

Hi community, I'm wondering if anyone has any ideas here. This is a method for getting a basic HTML table on a webpage of organizations, 30 at a time, paginated with HTML links to increment the 'orgs' variable that allows you to move on to the next 30 by changing the URL.

 

$number = 1;
$zenLink = "https://z2.zendesk.com/organizations.xml?page=".$number;


if (isset($_GET['orgs'])){ 
$number = $_GET['orgs'];
$number = $number + 1;
$zenLink = "https://z2.zendesk.com/organizations.xml?page=".$number;
}

//Above is the incrementer for next page navigation links to get paginated Zendesk list of orgs

$c = curl_init($zenLink);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_USERPWD, 'user@email.com:password'); //ZENDESK login

$page = curl_exec($c);
echo $page;

 

 

$page spits out "Couldn't authenticate you." It might be some curl option I need to set, I just don't know what it is. This exact code was working like a week ago...

April 17, 2012 11:09