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 Continue reading