forked from blak3r/freshdesk-solutions
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FreshdeskRest.php
249 lines (200 loc) · 7.58 KB
/
FreshdeskRest.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
* Implements FreshDesk API methods for Tickets and Surveys in PHP.
* See README.md
* Forked from: https://github.com/blak3r/freshdesk-solutions
* Big thanks to Blake for building the initial API Object Methods
*/
class FreshdeskRest {
private $domain = '', $username = '', $password = '';
private $lastHttpStatusCode = 200;
private $lastHttpResponseText = '';
private $proxyServer = "";
/**
* Constructor
* @param $domain - yourname.freshdesk.com - but will also accept http://yourname.freshdesk.com/, etc.
* @param $username String Can be your username or it can be the API Key.
* @param $password String Optional if you use API Key.
*/
function __construct($domain, $username, $password = 'X') {
$strippedDomain = preg_replace('#^https?://#', '', $domain); // removes http:// or https://
$strippedDomain = preg_replace('#/#', '', $strippedDomain); // get trailing slash
$this->domain = $strippedDomain;
$this->password = $password;
$this->username = $username;
}
/**
* @param $urlMinusDomain - should start with /... example /solutions/categories.xml
* @param $method - should be either GET, POST, PUT (and theoretically DELETE but that's untested).
* @param string $postData - only specified if $method == POST or PUT
* @param $debugMode {bool} optional - prints the request and response with headers
* @return the raw response
*/
private function restCall($urlMinusDomain, $method, $postData = '',$debugMode=false) {
$url = "https://{$this->domain}$urlMinusDomain";
$header[] = "Content-type: application/json";
$ch = curl_init ($url);
if( $method == "POST") {
if( empty($postData) ){
$header[] = "Content-length: 0"; // <-- seems to be unneccessary to specify this... curl does it automatically
}
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postData);
}
else if( $method == "PUT" ) {
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PUT" );
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postData);
}
else if( $method == "DELETE" ) {
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "DELETE" ); // UNTESTED!
}
else {
curl_setopt ($ch, CURLOPT_POST, false);
}
curl_setopt($ch, CURLOPT_USERPWD, "{$this->username}:{$this->password}");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
if( !empty($this->proxyServer) ) {
curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
}
$verbose = ''; // set later...
if( $debugMode ) {
// CURLOPT_VERBOSE: TRUE to output verbose information. Writes output to STDERR,
// or the file specified using CURLOPT_STDERR.
curl_setopt($ch, CURLOPT_VERBOSE, true);
$verbose = fopen('php://temp', 'rw+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
}
$httpResponse = curl_exec ($ch);
if( $debugMode ) {
!rewind($verbose);
$verboseLog = stream_get_contents($verbose);
print $verboseLog;
}
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//curl_close($http);
if( !preg_match( '/2\d\d/', $http_status ) ) {
//print "ERROR: HTTP Status Code == " . $http_status . " (302 also isn't an error)\n";
}
// print "\n\nREST RESPONSE: " . $httpResponse . "\n\n";
$this->lastHttpResponseText = $httpResponse;
return $httpResponse;
}
/**
* Returns the HTTP status code of the last call, useful for error checking.
* @return int
*/
public function getLastHttpStatus() {
return $this->lastHttpStatusCode;
}
/**
* Returns the HTTP Response Text of the last curl call, useful for error checking.
* @return int
*/
public function getLastHttpResponseText() {
return $this->lastHttpResponseText;
}
/**
* Will force cURL requests to use the proxy. Can be useful to debug requests and responses
* using Fiddler2 or WireShark.
* curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888'); // Use with Fiddler to debug
* @param $proxyServer - example for fiddler2 default: '127.0.0.1:8888'
*/
public function setProxyServer($proxyServer)
{
$this->proxyServer = $proxyServer;
}
/**
* Returns all the open tickets of the API user's credentials used for the request
* @return bool FALSE if it doesn't exist, the object otherwise.
*/
public function getApiUserTickets() {
$json = $this->restCall("/helpdesk/tickets.json", "GET");
if( empty($json) ) {
return FALSE;
}
$json = json_decode($json);
return $json;
}
/**
* Returns all the tickets
* @params $page
* @return bool FALSE if it doesn't exist, the object otherwise.
*/
public function getAllTickets($page) {
$json = $this->restCall("/helpdesk/tickets.json?filter_name=all_tickets&page=$page", "GET");
if( empty($json) ) {
return FALSE;
}
$json = json_decode($json);
return $json;
}
/**
* Returns the Ticket, this method takes in the IDs for a ticket.
* @param $ticketId
* @return bool FALSE if it doesn't exist, the object otherwise.
*/
public function getSingleTicket($ticketId) {
$json = $this->restCall("/helpdesk/tickets/$ticketId.json", "GET");
if( empty($json) ) {
return FALSE;
}
$json = json_decode($json);
return $json;
}
/**
* Returns all tickets from the user specified by email address
* @param $email
* @return bool FALSE if it doesn't exist, the object otherwise.
*/
public function getUserTickets($email) {
$json = $this->restCall("/helpdesk/tickets/user_ticket.json?email=$email", "GET");
if( empty($json) ) {
return FALSE;
}
$json = json_decode($json);
return $json;
}
/**
* Returns tickets for a specific view
* @params $viewId
* @params $page
* @return bool FALSE if it doesn't exist, the object otherwise.
*/
public function getTicketView($viewId, $page) {
$json = $this->restCall("/helpdesk/tickets/view/$viewId?format=json&page=$page", "GET");
if( empty($json) ) {
return FALSE;
}
$json = json_decode($json);
return $json;
}
/**
* Returns the fields available to helpdesk tickets
* @return bool FALSE if it doesn't exist, the object otherwise.
*/
public function getTicketFields() {
$json = $this->restCall("/ticket_fields.json", "GET");
if( empty($json) ) {
return FALSE;
}
$json = json_decode($json);
return $json;
}
/**
* Returns the Survey for a given ticket, this method takes in the IDs for a ticket
* @param $ticketId
* @return bool FALSE if it doesn't exist, the object otherwise.
*/
public function getTicketSurvey($ticketId) {
$json = $this->restCall("/helpdesk/tickets/$ticketId/surveys.json", "GET");
if( empty($json) ) {
return FALSE;
}
$json = json_decode($json);
return $json;
}
}