Zendesk API Implementation In Codeigniter

Recently I had to implement the Zendesk API for exporting users to Zendesk. First here is the controller file created on this path application/controllers/admin/admin_zendesk.php

Add the following line to application/config/routes.php to access the export_users function

Now you can access the URL http://testdomain.dev/admin/zendesk/export_users or http://localhost/admin/zendesk/export_users to see above code in action.

In controller code on line 66, I exported two users to Zendesk. You can instead, fetch your users from database/model on line 66 and export that data to zendesk.

Important Note

Zendesk returns the user id that it generats for exported data to their database from you. You can see that user id printed out by line 99 in controller code. You will have to save that Zendesk user id in your database for later reference e.g for updating or deleting that user on Zendesk. The reason is that Zendesk doesn’t give any other option to update their database other than their provided user id.

Also, please make sure that you make proper use of POST, PUT and GET accordingly. I got an error

Array([error] => InvalidEndpoint, [description] => Not found) 

while coding which was because I was using POST instead of PUT while updating user on Zendesk using API.

Useful Links

  •  To know what else options you have to work with Zendesk data, you can visit following link https://developer.zendesk.com/rest_api/docs/core/users#create-user. As an example you can see that in controller code on line 98, I used /users/create_many.json for creating many users on Zendesk.
  • To get the API key, you will have to login to your agent account and access following link https://abc.zendesk.com/agent/admin/api  and then enable “Token Access”. After doing this Zendesk will provide you API key.
  • For downloading CURL certificate, you can visit this link http://curl.haxx.se/docs/caextract.html

An Example for Updating User on Zendesk

You can add following function to the controller code too for updating any user.

An important thing to note here is “PUT” instead of “POST” that we used while creating users.

 


Add and Update Both in One Function

In order to update and add both in one function here is the code.

On line 21 in above code I used Search option provided by Zendesk. You can find more search options available from Zendesk on this link too  https://developer.zendesk.com/rest_api/docs/core/search.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">