-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathIntercom.php
313 lines (275 loc) · 9.64 KB
/
Intercom.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
* Intercom is a customer relationship management and messaging tool for web app owners
*
* This library provides connectivity with the Intercom API (https://api.intercom.io)
*
* Basic usage:
*
* 1. Configure Intercom with your access credentials
* <code>
* <?php
* $intercom = new Intercom('dummy-app-id', 'dummy-api-key');
* ?>
* </code>
*
* 2. Make requests to the API
* <code>
* <?php
* $intercom = new Intercom('dummy-app-id', 'dummy-api-key');
* $users = $intercom->getAllUsers();
* var_dump($users);
* ?>
* </code>
*
* @author Bruno Pedro <[email protected]>
* @copyright Copyright 2013 Nubera eBusiness S.L. All rights reserved.
* @link http://www.nubera.com/
* @license http://opensource.org/licenses/MIT
**/
/**
* Intercom.io API
*/
class Intercom
{
/**
* The Intercom API endpoint
*/
private $apiEndpoint = 'https://api.intercom.io/v1/';
/**
* The Intercom application ID
*/
private $appId = null;
/**
* The Intercom API key
*/
private $apiKey = null;
/**
* Last HTTP error obtained from curl_errno() and curl_error()
*/
private $lastError = null;
/**
* Whether we are in debug mode. This is set by the constructor
*/
private $debug = false;
/**
* The constructor
*
* @param string $appId The Intercom application ID
* @param string $apiKey The Intercom API key
* @param string $debug Optional debug flag
* @return void
**/
public function __construct($appId, $apiKey, $debug = false)
{
$this->appId = $appId;
$this->apiKey = $apiKey;
$this->debug = $debug;
}
/**
* Make an HTTP call using curl.
*
* @param string $url The URL to call
* @param string $method The HTTP method to use, by default GET
* @param string $post_data The data to send on an HTTP POST (optional)
* @return object
**/
private function httpCall($url, $method = 'GET', $post_data = null)
{
$headers = array();
if ($post_data) {
$headers[] = 'Content-Type: application/json';
$headers[] = 'Expect:';
}
$ch = curl_init($url);
if ($this->debug) {
curl_setopt($ch, CURLOPT_VERBOSE, true);
}
if ($method == 'POST') {
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_POST, true);
} elseif ($method == 'PUT') {
$putFile = tmpfile();
fwrite($putFile, $post_data);
fseek($putFile, 0);
curl_setopt($ch, CURLOPT_INFILE, $putFile);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($post_data));
curl_setopt($ch, CURLOPT_PUT, true);
} elseif ($method != 'GET') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 4096);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $this->appId . ':' . $this->apiKey);
$response = curl_exec($ch);
// Set HTTP error, if any
$this->lastError = array('code' => curl_errno($ch), 'message' => curl_error($ch));
return json_decode($response);
}
/**
* Get all users from your Intercom account.
*
* @param integer $page The results page number
* @param integer $perPage The number of results to return on each page
* @return object
**/
public function getAllUsers($page = 1, $perPage = null)
{
$path = 'users/?page=' . $page;
if (!empty($perPage)) {
$path .= '&per_page=' . $perPage;
}
return $this->httpCall($this->apiEndpoint . $path);
}
/**
* Get a specific user from your Intercom account.
*
* @param string $id The ID of the user to retrieve
* @return object
**/
public function getUser($id)
{
$path = 'users/';
if (preg_match('/@/', $id)) {
$path .= '?email=';
} else {
$path .= '?user_id=';
}
$path .= urlencode($id);
return $this->httpCall($this->apiEndpoint . $path);
}
/**
* Create a user on your Intercom account.
*
* @param string $id The ID of the user to be created
* @param string $email The user's email address (optional)
* @param string $name The user's name (optional)
* @param array $customData Any custom data to be aggregate to the user's record (optional)
* @param long $createdAt UNIX timestamp describing the date and time when the user was created (optional)
* @param string $lastSeenIp The last IP address where the user was last seen (optional)
* @param string $lastSeenUserAgent The last user agent of the user's browser (optional)
* @param long $lastRequestAt UNIX timestamp of the user's last request (optional)
* @param string $method HTTP method, to be used by updateUser()
* @return object
**/
public function createUser($id,
$email = null,
$name = null,
$customData = array(),
$createdAt = null,
$lastSeenIp = null,
$lastSeenUserAgent = null,
$lastRequestAt = null,
$method = 'POST')
{
$data = array();
$data['user_id'] = $id;
if (!empty($email)) {
$data['email'] = $email;
}
if (!empty($name)) {
$data['name'] = $name;
}
if (!empty($createdAt)) {
$data['created_at'] = $createdAt;
}
if (!empty($lastSeenIp)) {
$data['last_seen_ip'] = $lastSeenIp;
}
if (!empty($lastSeenUserAgent)) {
$data['last_seen_user_agent'] = $lastSeenUserAgent;
}
if (!empty($lastRequestAt)) {
$data['last_request_at'] = $lastRequestAt;
}
if (!empty($customData)) {
$data['custom_data'] = $customData;
}
$path = 'users';
return $this->httpCall($this->apiEndpoint . $path, $method, json_encode($data));
}
/**
* Update an existing user on your Intercom account.
*
* @param string $id The ID of the user to be updated
* @param string $email The user's email address (optional)
* @param string $name The user's name (optional)
* @param array $customData Any custom data to be aggregate to the user's record (optional)
* @param long $createdAt UNIX timestamp describing the date and time when the user was created (optional)
* @param string $lastSeenIp The last IP address where the user was last seen (optional)
* @param string $lastSeenUserAgent The last user agent of the user's browser (optional)
* @param long $lastRequestAt UNIX timestamp of the user's last request (optional)
* @return object
**/
public function updateUser($id,
$email = null,
$name = null,
$customData = array(),
$createdAt = null,
$lastSeenIp = null,
$lastSeenUserAgent = null,
$lastRequestAt = null)
{
return $this->createUser($id, $email, $name, $customData, $createdAt, $lastSeenIp, $lastSeenUserAgent, $lastRequestAt, 'PUT');
}
/**
* Delete an existing user from your Intercom account
*
* @param string $id The ID of the user to be deleted
* @return object
**/
public function deleteUser($id)
{
$path = 'users/';
if (preg_match('/@/', $id)) {
$path .= '?email=';
} else {
$path .= '?user_id=';
}
$path .= urlencode($id);
return $this->httpCall($this->apiEndpoint . $path, 'DELETE');
}
/**
* Create an impression associated with a user on your Intercom account
*
* @param string $userId The ID of the user
* @param string $email The email of the user (optional)
* @param string $userIp The IP address of the user (optional)
* @param string $userAgent The user agent of the user (optional)
* @param string $currentUrl The URL the user is visiting (optional)
* @return object
**/
public function createImpression($userId, $email = null, $userIp = null, $userAgent = null, $currentUrl = null)
{
$data = array();
$data['user_id'] = $userId;
if (!empty($email)) {
$data['email'] = $email;
}
if (!empty($userIp)) {
$data['user_ip'] = $userIp;
}
if (!empty($userAgent)) {
$data['user_agent'] = $userAgent;
}
if (!empty($currentUrl)) {
$data['current_url'] = $currentUrl;
}
$path = 'users/impressions';
return $this->httpCall($this->apiEndpoint . $path, 'POST', json_encode($data));
}
/**
* Get the last error from curl.
*
* @return array Array with 'code' and 'message' indexes
*/
public function getLastError()
{
return $this->lastError;
}
}
?>