11

PHP API Library

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

125 comments

  • 0

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

  • 0

    Absolutely! Thanks Brian, stunning.

  • 0

    Fantastic! Thank you/

  • 0

    Great library. Works perfect

  • 0

    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?

  • 0

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

  • 0

    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'
    ));

  • 0

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

  • 0

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

  • 0

    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.

  • 0

    Thanks Brian all working well so far.

  • 0

    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?

  • 0

    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.

  • 0

    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');

  • 0

    Thanks Brian !

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

  • 0

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

  • 0

    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!

     

  • 0

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

  • 0

    @mantra1 - ZenDesk pages things so no

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

  • 0

    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'));

  • 0

    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

  • 0

    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;

  • 0

    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?

  • 0

    Hi Brian!

    Useful lib, thanks for that.

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

  • 0

    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

     

  • 0

    ops, sorry

    don't know how to manage that parser

  • 0

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

  • 0

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

     

    Cheers, Tikhon

  • 0

    Thanks, Tikhon!

  • 0

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

Please sign in to leave a comment.