-
Notifications
You must be signed in to change notification settings - Fork 3
/
zendesk.lib.php
122 lines (116 loc) · 3.86 KB
/
zendesk.lib.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* A minimal Zendesk API PHP implementation
*
* @package Zendesk
*
* @author Julien Renouard <[email protected]> (deeply inspired by Darren Scerri <[email protected]> Mandrill's implemetation)
*
* @version 1.0
*
*/
class zendesk
{
/**
* API Constructor. If set to test automatically, will return an Exception if the ping API call fails
*
* @param string $apiKey API Key.
* @param string $user Username on Zendesk.
* @param string $subDomain Your subdomain on zendesk, without https:// nor trailling dot.
* @param string $suffix .json by default.
* @param bool $test=true Whether to test API connectivity on creation.
*/
public function __construct($apiKey, $user, $subDomain, $suffix = '.json', $test = false)
{
$this->api_key = $apiKey;
$this->user = $user;
$this->base = 'https://' . $subDomain . '.zendesk.com/api/v2';
$this->suffix = $suffix;
if ($test === true && !$this->test())
{
throw new Exception('Cannot connect or authentice with the Zendesk API');
}
}
/**
* Perform an API call.
*
* @param string $url='/tickets' Endpoint URL. Will automatically add the suffix you set if necessary (both '/tickets.json' and '/tickets' are valid)
* @param array $json=array() An associative array of parameters
* @param string $action Action to perform POST/GET/PUT
*
* @return mixed Automatically decodes JSON responses. If the response is not JSON, the response is returned as is
*/
public function call($url, $json, $action)
{
if (substr_count($url, $this->suffix) == 0)
{
$url .= '.json';
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_USERPWD, $this->user."/token:".$this->api_key);
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 "DELETE":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
case "PUT":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
break;
case "UPLOAD":
// In this case the $json var should be a Drupal file object
$file = fopen($json->uri, 'r');
$filesize = $json->filesize;
$filedata = '';
while (!feof($file)) {
$filedata .= fread($file, 8192);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $filedata);
curl_setopt($ch, CURLOPT_INFILE, $file);
curl_setopt($ch, CURLOPT_INFILESIZE, $filesize);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
$url .= '?' . http_build_query(array("filename" => $json->filename));
break;
default:
break;
}
// Different headers for a file transfer.
switch ($action) {
case "UPLOAD":
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/binary'));
break;
default:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
break;
}
curl_setopt($ch, CURLOPT_URL, $this->base.$url);
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, variable_get('zendesk_curl_timeout', 10));
$output = curl_exec($ch);
if ($output === FALSE) {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
$decoded = json_decode($output);
return is_null($decoded) ? $output : $decoded;
}
/**
* Tests the API using /users/ping
*
* @return bool Whether connection and authentication were successful
*/
public function test()
{
return $this->call('/tickets', '', 'GET');
}
}