forked from HubSpot/haPiHP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.baseclient.php
542 lines (501 loc) · 18.1 KB
/
class.baseclient.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<?php
/**
* Copyright 2011 HubSpot, Inc.
*
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
include 'class.exception.php';
class HubSpot_BaseClient {
// HubSpot_BaseClient class to be extended by specific hapi clients
// Declare variables
protected $HAPIKey;
protected $ACCESS_TOKEN;
protected $API_PATH;
protected $REFRESH_TOKEN;
protected $CLIENT_ID;
protected $API_VERSION;
protected $isTest = false;
protected $PATH_DIV = '/';
protected $API_KEY_PARAM = '?hapikey=';
protected $TOKEN_PARAM = '?access_token=';
protected $PROD_DOMAIN = 'https://api.hubapi.com';
protected $QA_DOMAIN = 'https://hubapiqa.com';
protected $userAgent;
/**
* The HTTP status of the most recent request
*
* @var integer
*/
protected $lastStatus;
/**
* The HTTP code for a successful request
*/
const STATUS_OK = 200;
/**
* The HTTP code for bad request
*/
const STATUS_BAD_REQUEST = 400;
/**
* The HTTP code for unauthorized
*/
const STATUS_UNAUTHORIZED = 401;
/**
* The HTTP code for resource not found
*/
const STATUS_NOT_FOUND = 404;
/**
* Constructor.
*
* @param HAPIKey: String value of HubSpot API Key for requests
* access_token: String value of Hubspot OAuth Token
* refresh_token: String value of refresh token given initially for OAuth
* client_id: Unique ID for your registered app
*
* */
public function __construct( $HAPIKey=null, $access_token=null, $refresh_token=null, $client_id=null, $userAgent="haPiHP default UserAgent" ) {
if ( $HAPIKey and $access_token ) {
throw new Exception( "Cannot use hapikey and OAuth token", 1 );
}
else {
$this->HAPIKey = $HAPIKey;
print_r( "HAPIKey:".$this->HAPIKey );
$this->ACCESS_TOKEN = $access_token;
print_r( "token:".$this->ACCESS_TOKEN );
}
$this->userAgent = $userAgent;
}
/**
* Gets the status code from the most recent curl request
*
* @return integer
*/
public function getLastStatus() {
return (int)$this->lastStatus;
}
/**
* Returns API_PATH that is set in specific hapi clients. All
* clients that extend HubSpot_BaseClient should set $API_PATH to the
* base path for the API (e.g.: the leads api sets the value to
* 'leads')
*
* @throws HubSpot_Exception
*/
protected function get_api() {
if ( $this->isBlank( $this->API_PATH ) )
throw new HubSpot_Exception( 'API_PATH must be defined' );
else
return $this->API_PATH;
}
/**
* Returns API_VERSION that is set in specific hapi clients. All
* clients that extend HubSpot_BaseClient should set $API_VERSION to the
* version that the client is developed for (e.g.: the leads v1
* client sets the value to 'v1')
*
* @throws HubSpot_Exception
*/
protected function get_api_version() {
if ( $this->isBlank( $this->API_VERSION ) )
throw new HubSpot_Exception( 'API_VERSION must be defined' );
else
return $this->API_VERSION;
}
/**
* Allows developer to set testing flag to true in order to
* execute api requests against hubapiqa.com
*
* @param unknown $testing: Boolean
*/
public function set_is_test( $testing ) {
if ( $testing == TRUE ) {
$this->isTest = TRUE;
}
}
/**
* Returns the hapi domain to use for requests based on isTesting
*
* @returns: String value of domain, including https protocol
* */
protected function get_domain() {
if ( $this->isTest == true ) {
return $this->QA_DOMAIN;
} else {
return $this->PROD_DOMAIN;
}
}
/**
* Creates the url to be used for the api request. If OAuth token is provided but invalid, will attempt a refresh.
*
* @param endpoint: String value for the endpoint to be used (appears after version in url)
* @param params: Array containing query parameters and values
*
* @returns String
* */
protected function get_request_url( $endpoint, $params ) {
$paramstring = $this->array_to_params( $params );
echo $this->HAPIKey;
if ( $this->HAPIKey ) {
print_r( "Trying to use hapikey" );
return $this->get_domain() . $this->PATH_DIV .
$this->get_api() . $this->PATH_DIV .
$this->get_api_version() . $this->PATH_DIV .
$endpoint .
$this->API_KEY_PARAM . $this->HAPIKey .
$paramstring;
}
else {
print "went to else";
if ( $this->check_auth()>=400 ) {
print_r( "Auth check failed" );
try {
$refreshed_token = $this->refresh_access_token( $this->REFRESH_TOKEN, $this->CLIENT_ID );
$this->ACCESS_TOKEN = $refreshed_token['access_token'];
return $this->get_domain() . $this->PATH_DIV .
$this->get_api() . $this->PATH_DIV .
$this->get_api_version() . $this->PATH_DIV .
$endpoint .
$this->TOKEN_PARAM . $this->ACCESS_TOKEN .
$paramstring;
} catch ( HubSpot_Exception $e ) {
print_r( 'Unable to refresh the OAuth token. Please provide a valid access_token or refresh_token' );
}
}
else {
return $this->get_domain() . $this->PATH_DIV .
$this->get_api() . $this->PATH_DIV .
$this->get_api_version() . $this->PATH_DIV .
$endpoint .
$this->TOKEN_PARAM . $this->ACCESS_TOKEN .
$paramstring;
}
}
}
/**
* Creates the url to be used for the api request for Forms API
*
* @param endpoint: String value for the endpoint to be used (appears after version in url)
* @param params: Array containing query parameters and values
*
* @returns String
* */
protected function get_forms_request_url( $url_base, $params ) {
$paramstring = $this->array_to_params( $params );
return $url_base .
$paramstring;
}
/**
* Executes HTTP GET request
*
* @param URL: String value for the URL to GET
*
* @return Body of request result
*
* @throws HubSpot_Exception
*/
protected function execute_get_request( $url ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent );
$output = curl_exec( $ch );
$errno = curl_errno( $ch );
$error = curl_error( $ch );
$this->setLastStatusFromCurl( $ch );
curl_close( $ch );
if ( $errno > 0 )
throw new HubSpot_Exception( 'cURL error: ' . $error );
else
return $output;
}
/**
* Executes HTTP POST request
*
* @param URL: String value for the URL to POST to
* @param fields: Array containing names and values for fields to post
*
* @return Body of request result
*
* @throws HubSpot_Exception
*/
protected function execute_post_request( $url, $body, $formenc = FALSE ) {
// intialize cURL and send POST data
$ch = curl_init();
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent );
if ( $formenc )
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded' ) );
$output = curl_exec( $ch );
$errno = curl_errno( $ch );
$error = curl_error( $ch );
$this->setLastStatusFromCurl( $ch );
curl_close( $ch );
if ( $errno > 0 ) {
throw new HubSpot_Exception ( 'cURL error: ' + $error );
} else {
return $output;
}
}
/**
* Executes HTTP POST request with JSON as the POST body
*
* @param URL String value for the URL to POST to
* @param fields Array containing names and values for fields to post
*
* @return Body of request result
*
* @throws HubSpot_Exception
*/
protected function execute_JSON_post_request( $url, $body ) {
// intialize cURL and send POST data
$ch = curl_init();
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' ) );
$output = curl_exec( $ch );
$errno = curl_errno( $ch );
$error = curl_error( $ch );
$this->setLastStatusFromCurl( $ch );
curl_close( $ch );
if ( $errno > 0 )
throw new HubSpot_Exception( 'cURL error: ' . $error );
else
return $output;
}
/**
* Executes HTTP POST request with XML as the POST body
*
* @param URL String value for the URL to POST to
* @param fields Array containing names and values for fields to post
*
* @return Body of request result
*
* @throws HubSpot_Exception
*/
protected function execute_xml_post_request( $url, $body ) {
// intialize cURL and send POST data
$ch = curl_init();
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/atom+xml' ) );
$output = curl_exec( $ch );
$errno = curl_errno( $ch );
$error = curl_error( $ch );
$this->setLastStatusFromCurl( $ch );
curl_close( $ch );
if ( $errno > 0 )
throw new HubSpot_Exception( 'cURL error: ' . $error );
else
return $output;
}
/**
* Executes HTTP PUT request
*
* @param URL String value for the URL to PUT to
* @param body String value of the body of the PUT request
*
* @return Body of request result
*
* @throws HubSpot_Exception
*/
protected function execute_put_request( $url, $body ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen( $body ) ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PUT" );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
$result = curl_exec( $ch );
$apierr = curl_errno( $ch );
$errmsg = curl_error( $ch );
$this->setLastStatusFromCurl( $ch );
curl_close( $ch );
if ( $apierr > 0 )
throw new HubSpot_Exception( 'cURL error: ' . $errmsg );
else
return $result;
}
/**
* Executes HTTP PUT request with XML as the PUT body
*
* @param URL String value for the URL to PUT to
* @param body String value of the body of the PUT request
*
* @return Body of request result
*
* @throws HubSpot_Exception
*/
protected function execute_xml_put_request( $url, $body ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/atom+xml', 'Content-Length: ' . strlen( $body ) ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PUT" );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
$result = curl_exec( $ch );
$apierr = curl_errno( $ch );
$errmsg = curl_error( $ch );
$this->setLastStatusFromCurl( $ch );
curl_close( $ch );
if ( $apierr > 0 )
throw new HubSpot_Exception( 'cURL error: ' . $errmsg );
else
return $result;
}
/**
* Executes HTTP DELETE request
*
* @param URL String value for the URL to DELETE to
* @param body String value of the body of the DELETE request
*
* @return Body of request result
*
* @throws HubSpot_Exception
*/
protected function execute_delete_request( $url, $body ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen( $body ) ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "DELETE" );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
$result = curl_exec( $ch );
$apierr = curl_errno( $ch );
$errmsg = curl_error( $ch );
$this->setLastStatusFromCurl( $ch );
curl_close( $ch );
if ( $apierr > 0 )
throw new HubSpot_Exception( 'cURL error: ' . $errmsg );
else
return $result;
}
/**
* Converts an array into url friendly list of parameters
*
* @param array params Multidimensional array of parameters (name=>value)
*
* @return String of url friendly parameters (&name=value&foo=bar)
*/
protected function array_to_params( $params ) {
$paramstring = '';
if ( $params != NULL ) {
foreach ( $params as $parameter => $value ) {
if ( is_array( $value ) ) {
foreach ( $value as $subparam ) {
$paramstring = $paramstring . '&' . $parameter . '=' . urlencode( $subparam );
}
}
else {
$paramstring = $paramstring . '&' . $parameter . '=' . urlencode( $value );
}
}
}
return $paramstring;
}
/**
* Utility function used to determine if variable is empty
*
* @param s Variable to be evaluated
*
* @returns Boolean
*/
protected function isBlank( $s ) {
if ( ( trim( $s )=='' ) or ( $s == NULL ) )
return true;
else
return false;
}
/**
* Sets the status code from a curl request
*
* @param resource $ch
*/
protected function setLastStatusFromCurl( $ch ) {
$info = curl_getinfo( $ch );
$this->lastStatus = ( isset( $info['http_code'] ) ) ? $info['http_code'] : null;
}
/**
* Quick check of access_token
*
*
* @return: Response code for request
*
* */
protected function check_auth() {
$url = 'https://api.hubapi.com/contacts/v1/properties/email?access_token='.$this->ACCESS_TOKEN;
print_r( $url );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent );
$output = curl_exec( $ch );
$response_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
return $response_code;
}
/**
* Refresh OAuth token
*
* @param refresh_token: The refresh token for your portal
* @param client_id: Unique ID for your app, generated when the app is registered
*
* @return: Body of request result
*
* @throws HubSpot_Exception
* */
protected function refresh_access_token( $refresh_token, $client_id ) {
if ( $refresh_token and $client_id ) {
$url = 'https://api.hubapi.com/auth/v1/refresh';
$body = 'refresh_token='.$refresh_token.'&client_id='.$client_id.'&grant_type=refresh_token';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded' ) );
$output = curl_exec( $ch );
$apierr = curl_errno( $ch );
$errmsg = curl_error( $ch );
$response_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
$output_array = json_decode( $output );
if ( $response_code<400 ) {
return $output_array;
} else {
throw new HubSpot_Exception( "cURL error: " + $errmsg );
}
}
else {
throw new HubSpot_Exception( "Please provide a refresh_token and client_id" );
}
}
}