-
Notifications
You must be signed in to change notification settings - Fork 4
/
Five00PxPubClient.php
168 lines (144 loc) · 3.88 KB
/
Five00PxPubClient.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
<?php
/**
* A simple PHP client to access the 500px API points
* that does is public (that does not require a user access token).
* To use the API with a 500px's user access token please refer to
* the official documentation
*
* Usage example to get photos:
* <code>
* // API options
* $options = array(
* 'key' => 'YOUR API KEY',
* 'secret' => 'YOUR SECRET KEY',
* );
*
* // Initialize Cient
* $five00Px = Five00PxPubClient::factory($options);
* $tags = array(
* // Tag 1
* // Tag 2
* );
*
* // Search Params for the Photo
* $apiParams = array (
* 'tag' => implode(',', $tags),
* 'term' => 'Search Term',
* 'rpp' => 40,
* );
*
* // Do the API call
* try {
* $photos = $five00Px->api('photos/search', $params);
* } catch(Exception $e) {
* echo 'Error in call ' . $e->getMessage();
* }
*
* var_dump($photos);
* </code>
*
* @see https://github.com/500px/api-documentation
* @requires php_curl
* @author Shih Oon Liong <[email protected]>
*/
class Five00PxPubClient {
const
HTTP_METHOD_GET = 'GET',
HTTP_METHOD_POST = 'POST',
HTTP_RESPONSE_SUCCESS = 200,
API_BASE_URL = 'https://api.500px.com';
var
$logger = null,
$version = 1,
$defaultParams = array();
/**
* Constructor
* @param array $options Initialization options. The array must contain the follow indexes
* 'key' => Your 500px Application Key
* 'secret' => Your 500px Secret Key
* 'version' => The version of the API. Default to 1
*/
public function __construct($options) {
// Set Logger
if ( array_key_exists('logger', $options)
&& ! empty ($options['logger']) ) {
$this->logger = $options['logger'];
}
$this->consumerKey = $options['key'];
$this->consumerSecret = $options['secret'];
$this->defaultParams = array(
'consumer_key' => $this->consumerKey,
'consumer_secret' => $this->consumerSecret,
);
// Set API version
if ( array_key_exists('version', $options) ) {
$this->version = $options['version'];
}
}
public static function factory($options=array()) {
$class = __CLASS__;
$instance = new $class($options);
return $instance;
}
/**
* Log messages
*/
private function logMessage() {
$logger = $this->logger;
if ($logger == null) {
return;
}
$arg_list = func_get_args();
call_user_func_array($logger, $arg_list);
}
/**
* Do an API call on 500px
* @param type $apiUrl
* @return mixed
* @throws Exception
*/
public function api($apiUrlCall, $userArgs=array(), $method='GET') {
// Set up Call Args
$this->logMessage('Default Args', $this->defaultParams);
$args = array_merge($userArgs, $this->defaultParams);
$this->logMessage('API Args', $args);
$apiUrl = self::API_BASE_URL . '/v' . $this->version . '/' . $apiUrlCall;
if ($method == self::HTTP_METHOD_GET) {
$apiUrl .= '?' . http_build_query($args);
}
$data = null;
try {
$ch = curl_init();
// Set CURL options
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$metadata = curl_getinfo($ch);
if ( $response === false ) {
throw new Exception('Invalid Response received');
}
// Check Response
if( array_key_exists('http_code', $metadata)
&& (int)$metadata['http_code'] == self::HTTP_RESPONSE_SUCCESS ) {
$data = json_decode($response);
curl_close($ch);
} else {
// Failed (non-success) Response received
$errorMsg = 'Response error - Error encountered: '
. '[' . $metadata['http_code'] . '] '
. '(Request: ' . $apiUrl . ') ';
if ( ! empty($response) ) {
$data = json_decode($response);
$errorMsg .= $data->error;
}
// Throw exception
throw new Exception($errorMsg);
}
} catch (Exception $e) {
// Failed to do curl call
throw new Exception($e->getMessage());
}
return $data;
}
}
?>