-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.php
More file actions
590 lines (453 loc) · 18.1 KB
/
Client.php
File metadata and controls
590 lines (453 loc) · 18.1 KB
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
<?php
namespace Behance;
/**
* Basic Behance Network API implementation to
* accessing Users, Projects and Works in Progress data
*
* Register your application FIRST: http://be.net/dev
*
*
* @author Bryan Latten <bryan@behance.com>
* @author Michael Klein <michael.klein@behance.com>
* @author Mark Dunphy <dunphy@adobe.com>
* @link http://be.net/dev
*
*/
class Client {
const API_ROOT = 'https://www.behance.net/v2';
const ENDPOINT_PROJECTS = '/projects';
const ENDPOINT_USERS = '/users';
const ENDPOINT_WIPS = '/wips';
const ENDPOINT_COLLECTIONS = '/collections';
const ENDPOINT_FIELDS = '/fields';
const TIMEOUT_DEFAULT_SEC = 30;
const VALID = 1;
const INVALID = 0;
/**
* @var string
*/
protected $_client_id;
/**
* Controls printing of exception messages.
*
* @var boolean
*/
protected $_debug;
/**
* Information can be found @ http://www.behance.net/dev/apps
*
* @param string $client_id
* @param bool $debug: OUTPUTS failures if they occur (non-2xx responses)
*/
public function __construct( $client_id, $debug = false ) {
$this->_client_id = $client_id;
$this->_debug = $debug;
} // __construct
/**
* Retrieves a full Project, by ID
*
* @param int $id which project to retrieve
* @param bool $assoc return object will be converted to an associative array
*
* @return array|stdClass|bool array or stdClass based on $assoc, false on failure
*/
public function getProject( $id, $assoc = false ) {
$endpoint = self::ENDPOINT_PROJECTS . '/' . $id;
return $this->_getDecodedJson( $endpoint, array(), 'project', $assoc );
} // getProject
/**
* Retrieves a list of a projects comments, by project ID
*
* @param int $id which project to retrieve comments for
* @param bool $assoc return objects will be converted to associative arrays
*
* @return array stdClass objects or associative arrays, based on $assoc
*/
public function getProjectComments( $id, $assoc = false ) {
$endpoint = self::ENDPOINT_PROJECTS . "/{$id}/comments";
$results = $this->_getDecodedJson( $endpoint, array(), 'comments', $assoc );
// IMPORTANT: Ensure this will always return an array
return ( empty( $results ) )
? array()
: $results;
} // getProjectComments
/**
* Retrieves a full User, based on either their ID or Username
*
* @param int|string $id_or_username who to retrieve
* @param bool $assoc return object will be converted to an associative array
*
* @return array|stdClass|bool array or stdClass based on $assoc, false on failure
*/
public function getUser( $id_or_username, $assoc = false ) {
$endpoint = self::ENDPOINT_USERS . '/' . $id_or_username;
return $this->_getDecodedJson( $endpoint, array(), 'user', $assoc );
} // getUser
/**
* Retrieves a list of $id_or_username's projects
*
* @param int|string $id_or_username user's projects to search
* @param array $params search parameters ex. [ per_page => 5, page => 2 ]
* @param bool $assoc return objects will be converted to associative arrays
*
* @see http://www.behance.net/dev/api/endpoints/2#users-get-2
*
* @return array stdClass objects or associative arrays, based on $assoc
*/
public function getUserProjects( $id_or_username, $params = array(), $assoc = false ) {
$endpoint = self::ENDPOINT_USERS . '/' . $id_or_username . '/projects';
$results = $this->_getDecodedJson( $endpoint, $params, 'projects', $assoc );
// IMPORTANT: Ensure this will always return an array
return ( empty( $results ) )
? array()
: $results;
} // getUserProjects
/**
* Retrieves a list of projects that $id_or_username has appreciated
*
* @param int|string $id_or_username user's projects to search
* @param bool $assoc return objects will be converted to associative arrays
*
* @return array stdClass objects or associative arrays, based on $assoc
*/
public function getUserAppreciations( $id_or_username, $assoc = false ) {
$endpoint = self::ENDPOINT_USERS . '/' . $id_or_username . '/appreciations';
$results = $this->_getDecodedJson( $endpoint, array(), 'appreciations', $assoc );
// IMPORTANT: Ensure this will always return an array
return ( empty( $results ) )
? array()
: $results;
} // getUserAppreciations
/**
* Retrieves a list of users the given user follows
*
* @param int|string $id_or_username user
* @param bool $assoc return objects will be converted to associative arrays
* @param array $options search options
*
* @return array stdClass objects or associative arrays, based on $assoc
*/
public function getUserFollows( $id_or_username, $options = array(), $assoc = false ) {
$endpoint = self::ENDPOINT_USERS . '/' . $id_or_username . '/following';
$results = $this->_getDecodedJson( $endpoint, $options, 'following', $assoc );
// IMPORTANT: Ensure this will always return an array
return ( empty( $results ) )
? array()
: $results;
} // getUserFollows
/**
* Retrieves a list of users who follow the provided user
*
* @param int|string $id_or_username user
* @param bool $assoc return objects will be converted to associative arrays
* @param array $options search options
*
* @return array stdClass objects or associative arrays, based on $assoc
*/
public function getUserFollowers( $id_or_username, $options = array(), $assoc = false ) {
$endpoint = self::ENDPOINT_USERS . '/' . $id_or_username . '/followers';
$results = $this->_getDecodedJson( $endpoint, $options, 'followers', $assoc );
// IMPORTANT: Ensure this will always return an array
return ( empty( $results ) )
? array()
: $results;
} // getUserFollowers
/**
* Retrieves a list of $id_or_username's works in progress
*
* @param int|string $id_or_username user's works in progress to search
* @param array $params search parameters ex. [ per_page => 5, page => 2 ]
* @param bool $assoc return objects will be converted to associative arrays
*
* @see http://www.behance.net/dev/api/endpoints/2#users-get-3
*
* @return array stdClass objects or associative arrays, based on $assoc
*/
public function getUserWips( $id_or_username, $params = array(), $assoc = false ) {
$endpoint = self::ENDPOINT_USERS . '/' . $id_or_username . '/wips';
$results = $this->_getDecodedJson( $endpoint, $params, 'wips', $assoc );
// IMPORTANT: Ensure this will always return an array
return ( empty( $results ) )
? array()
: $results;
} // getUserWips
/**
* Retrieves a full Work In Progress, by ID
*
* @param int $id
* @param bool $assoc return object will be converted to an associative array
*
* @return array|stdClass|bool array or stdClass based on $assoc, false on failure
*/
public function getWorkInProgress( $id, $assoc = false ) {
$endpoint = self::ENDPOINT_WIPS . '/' . $id;
$results = $this->_getDecodedJson( $endpoint, array(), 'wip', $assoc );
// IMPORTANT: Ensure this will always return an array
return ( empty( $results ) )
? array()
: $results;
} // getWorkInProgress
/**
* Retrieves a collection, by ID
*
* @param int $id which collection to retrieve
* @param boolean $assoc return objects will be converted to associative arrays
*
* @return array stdClass objects or associative arrays, based on $assoc
*/
public function getCollection( $id, $assoc = false ) {
$endpoint = self::ENDPOINT_COLLECTIONS . '/' . $id;
$results = $this->_getDecodedJson( $endpoint, array(), 'collection', $assoc );
// IMPORTANT: Ensure this will always return an array
return ( empty( $results ) )
? array()
: $results;
} // getCollection
/**
* Retrieves a list of collection $id's projects
*
* @param int $id collection's projects to search
* @param bool $assoc return objects will be converted to associative arrays
*
* @return array stdClass objects or associative arrays, based on $assoc
*/
public function getCollectionProjects( $id, $assoc = false ) {
$endpoint = self::ENDPOINT_COLLECTIONS . '/' . $id . '/projects';
$results = $this->_getDecodedJson( $endpoint, array(), 'projects', $assoc );
// IMPORTANT: Ensure this will always return an array
return ( empty( $results ) )
? array()
: $results;
} // getCollectionProjects
/**
* Retrieves a list of $id_or_username's collections
*
* @param int|string $id_or_username user's works in progress to search
* @param bool $assoc return objects will be converted to associative arrays
*
* @return array stdClass objects or associative arrays, based on $assoc
*/
public function getUserCollections( $id_or_username, $assoc = false ) {
$endpoint = self::ENDPOINT_USERS . "/{$id_or_username}/collections";
$results = $this->_getDecodedJson( $endpoint, array(), 'collections', $assoc );
// IMPORTANT: Ensure this will always return an array
return ( empty( $results ) )
? array()
: $results;
} // getUserCollections
/**
* Get user's statistics
*
* @param int|string $id_or_username user to retrieve stats
* @param boolean $assoc return objects will be converted to associative arrays
*
* @return array stdClass objects or associative arrays, based on $assoc
*/
public function getUserStats( $id_or_username, $assoc = false ) {
$endpoint = self::ENDPOINT_USERS . "/{$id_or_username}/stats";
$results = $this->_getDecodedJson( $endpoint, array(), 'stats', $assoc );
// IMPORTANT: Ensure this will always return an array
return ( empty( $results ) )
? array()
: $results;
} // getUserStats
/**
* Search projects, by these $params
*
* @param array $params if empty defaults to featured projects
* @param bool $assoc return objects will be converted to associative arrays
*
* @return array stdClass objects or associative arrays, based on $assoc
*/
public function searchProjects( array $params = array(), $assoc = false ) {
$endpoint = self::ENDPOINT_PROJECTS;
$results = $this->_getDecodedJson( $endpoint, $params, 'projects', $assoc );
// IMPORTANT: Ensure this will always return an array
return ( empty( $results ) )
? array()
: $results;
} // searchProjects
/**
* Search users, by these $params
*
* @param array $params if empty defaults to featured users
* @param bool $assoc return objects will be converted to associative arrays
*
* @return array stdClass objects or associative arrays, based on $assoc
*/
public function searchUsers( array $params = array(), $assoc = false ) {
$endpoint = self::ENDPOINT_USERS;
$results = $this->_getDecodedJson( $endpoint, $params, 'users', $assoc );
// IMPORTANT: Ensure this will always return an array
return ( empty( $results ) )
? array()
: $results;
} // searchUsers
/**
* Search works in progress, by these $params
*
* @param array $params if empty defaults to featured works in progress
* @param bool $assoc return objects will be converted to associative arrays
*
* @return array stdClass objects or associative arrays, based on $assoc
*/
public function searchWips( array $params = array(), $assoc = false ) {
$endpoint = self::ENDPOINT_WIPS;
$results = $this->_getDecodedJson( $endpoint, $params, 'wips', $assoc );
// IMPORTANT: Ensure this will always return an array
return ( empty( $results ) )
? array()
: $results;
} // searchWips
/**
* Search collections, by these $params
*
* @param array $params if empty defaults to featured collections
* @param bool $assoc return objects will be converted to associative arrays
*
* @return array stdClass objects or associative arrays, based on $assoc
*/
public function searchCollections( array $params = array(), $assoc = false ) {
$endpoint = self::ENDPOINT_COLLECTIONS;
$results = $this->_getDecodedJson( $endpoint, $params, 'collections', $assoc );
// IMPORTANT: Ensure this will always return an array
return ( empty( $results ) )
? array()
: $results;
} // searchCollections
/**
* Get creative fields
*
* @param bool $assoc return objects will be converted to associative arrays
*
* @return array stdClass objects or associative arrays, based on $assoc
*/
public function getFields( $assoc = false ) {
$endpoint = self::ENDPOINT_FIELDS;
$results = $this->_getDecodedJson( $endpoint, array(), '', $assoc );
// IMPORTANT: Ensure this will always return an array
return ( empty( $results ) )
? array()
: $results;
} // getFields
/**
* Automates retrieval data from $endpoint, using $query_params, and returns stdClass based on presence of $root_node
*
* @param string $endpoint API segment to retrieve
* @param array $query_params anything additional to add to the query string, in key => value form
* @param string $root_node first object property of JSON response object where the data is attached
* @param bool $assoc when TRUE, returned objects will be converted into associative array
*
* @return stdClass|bool
*/
protected function _getDecodedJson( $endpoint, array $query_params = array(), $root_node = '', $assoc = false ) {
$query_params['client_id'] = $this->_client_id;
$entity = $this->_get( $endpoint, $query_params );
if ( empty( $entity ) ) {
return false;
}
$entity = json_decode( $entity, $assoc );
if ( !$root_node ) {
return $entity;
}
if ( $assoc ) {
return ( empty( $entity[ $root_node ] ) )
? false
: $entity[ $root_node ];
} // if assoc
return ( empty( $entity->{$root_node} ) )
? false
: $entity->{$root_node};
} // _getDecodedJson
/**
* Performs a GET request, isolates caller from Apiexceptions
*
* @param string $endpoint just the segment of the API the request
* @param array $query_params anything additional to add to the query string, in key => value form
*
* @return string|bool response body on success, false on failure
*/
protected function _get( $endpoint, array $query_params = array() ) {
$full_url = $this->_makeFullURL( $endpoint, $query_params );
try {
return $this->_executeRequest( 'GET', $full_url );
} // try
catch( ApiException $e ) {
if ( $this->_debug ) {
echo( __CLASS__ . "::" . __LINE__ . ": " . $e->getMessage() );
}
} // catch ApiException
return false;
} // _get
/**
* Generates a fully-quality API url, with $endpoint + $query_params, automatically adds in app's key
*
* @param string $endpoint segment of the API being accessed
* @param array $query_params anything additional to add to the query string, in key => value form
*/
protected function _makeFullURL( $endpoint, array $query_params = array() ) {
$query_string = '?' . http_build_query( $query_params );
$full_url = self::API_ROOT . $endpoint . $query_string;
return $full_url;
} // _makeFullURL
/**
* Makes a remote request to $url
*
* @throws ApiException: on any non-200 response from request
*
* @param string $method HTTP verb of request (get|post|post|delete|head)
* @param string $url fully-qualified destination of request
* @param string|array $request_body sent as HTTP body of request
* @param array $curl_params parameters to override for cURL library (timeouts, user agent, etc)
*
* @return string response body
*/
protected function _executeRequest( $method, $url, $request_body = false, $curl_params = array() ) {
if ( !$this->_isCurlLoaded() ) {
throw new ApiException( 'cURL module is required' );
}
$user_agent = "Behance API/PHP (App {$this->_client_id})";
$default_curl_params = array(
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: multipart/form-data',
'Expect:'
),
CURLOPT_TIMEOUT => self::TIMEOUT_DEFAULT_SEC,
CURLOPT_USERAGENT => $user_agent,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => false,
CURLOPT_HTTPGET => true
);
$curl_params = array_replace_recursive( $default_curl_params, $curl_params );
$ch = curl_init( $url );
curl_setopt_array( $ch, $curl_params );
list( $response_body, $request_info, $response_code ) = $this->_executeCurl( $ch );
curl_close( $ch );
if ( (int)round( $response_code, -2 ) !== 200 ) {
throw new ApiException( "Unsuccessful Request, response ({$response_code}): " . ( empty( $response_body ) ? '' : ": {$response_body} " ) );
}
return $response_body;
} // _executeRequest
/**
* @codeCoverageIgnore
*
* @return boolean
*/
protected function _isCurlLoaded() {
return extension_loaded( 'curl' );
} // _isCurlLoaded
/**
* @codeCoverageIgnore
*
* @return array
*/
protected function _executeCurl( $ch ) {
$request_response = curl_exec( $ch );
$request_info = curl_getinfo( $ch );
$response_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
return array( $request_response, $request_info, $response_code );
} // _executeCurl
} // Client