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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
<?php if(!defined('BASEPATH')) exit('No direct script access allowed'); define("ZDAPIKEY", "your zendesk api key here"); define("ZDUSER", "username or email here"); define("ZDURL", "https://abc.zendesk.com/api/v2"); //replace abc with your domain name. class Admin_zendesk extends CI_Controller{ public function __construct() { parent::__construct(); if(!$this->auth->try_session_login() || get_field('role_id') < '99') redirect('/account/'); } public function index() { die('Nothing here'); } function curlWrap($url, $json, $action) { $ch = curl_init(); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 10 ); curl_setopt($ch, CURLOPT_URL, ZDURL.$url); curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY); curl_setopt($ch, CURLOPT_CAINFO, "C:/cacert.pem"); //I downloaded the certifcate to this location for CURL. switch($action){ case "POST": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); break; case "GET": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); break; case "PUT": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); break; case "DELETE": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); break; default: break; } curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $output = curl_exec($ch); echo 'Curl output: ' . curl_error($ch); curl_close($ch); $decoded = json_decode($output); return $decoded; } public function export_users(){ //dummy users array for exporting to Zendesk. You can fetch from your database/model here. $users = array( 'users' => array( 0 => array( 'name' => 'Test User', 'email' => 'test@gmail.com', 'external_id' => '', 'details' => 'details', 'notes' => 'any notes', 'phone' => '1234567', 'role' => 'end-user', 'restriction' => 'restriction', 'organization' => 'org', 'tags' => 'tags, etc' ), 1 => array( 'name' => 'Test User 2', 'email' => 'test2@gmail.com', 'details' => 'details', 'notes' => 'any notes', 'phone' => '1234567', 'role' => 'end-user', 'restriction' => 'restriction', 'organization' => 'org', 'user_fields' => array( 'user_id' => '444444444', 'company' => urlencode('abc'), ) ) ) ); $json = json_encode($users); $data = $this->curlWrap("/users/create_many.json", $json, "POST"); //create_many is used for exporting multiple users. print_r($data); // for displaying what is returned. exit; } } /* End of file zendesk.php */ /* Location: ./application/controllers/zendesk.php */ |
Add the following line to application/config/routes.php to access the export_users function
1 2 |
// zendesk admin $route['admin/zendesk/export_users'] = "admin/admin_zendesk/export_users"; |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public function update_users(){ //dummy user array for updating user on Zendesk. $users = array( 'user' => array( 'id'=> 123456789, //this id is zendesk's user id 'name' => 'John Smith' ) ); $json = json_encode($users); $data = $this->curlWrap("/users/123456789.json", $json, "PUT"); print_r($data); exit; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
//based on condition that if user already exists in Zendesk database or not, updating and adding user both in one function. public function zendesk_user() { //sample array $zendesk_user_data['user'] = array( 'name' => 'test name', 'email' => 'test@gmail.ocm', 'details' => '', 'role' => 'end-user', 'restriction' => '', 'tags' => array('abc','def'), 'user_fields' => array( 'user_id' => 32323, 'company' => 'abc' ) ); //first get the user information based on email from zendesk $user_email = $zendesk_user_data['user']['email']; $data = $this->curlWrap("/search.json?query=type:user+email:$user_email", NULL, "GET"); if (!empty($data->count) && $data->count == 1) { //if user exists then update $zendesk_user_data['user']['id'] = $data->results[0]->id; //now add zendesk user id too to the Zendesk user data array $json = json_encode($zendesk_user_data); $data = $this->curlWrap("/users/" . $zendesk_user_data['user']['id'] . ".json", $json, "PUT"); } else { //add user $json = json_encode($zendesk_user_data); $data = $this->curlWrap("/users.json", $json, "POST"); //for adding single user to zendesk. } //print_r($data); return $data; } |
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.