-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPixabayAPI.php
More file actions
394 lines (350 loc) · 14.5 KB
/
Copy pathPixabayAPI.php
File metadata and controls
394 lines (350 loc) · 14.5 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
<?php namespace pixabayapi;
/**
* Pixabay API Client
*
* Provides a wrapper to the Pixabay Image API and requires an API key to use.
* For API details please see https://pixabay.com/api/docs/.
*
* @author Martyr2
* @copyright 2021 Martyr2
* @link https://www.coderslexicon.com
*
*/
use pixabayapi\lib\CurlRequests;
use pixabayapi\lib\PixabayException;
class PixabayAPI {
private string $imageSearchUrl = 'https://pixabay.com/api/';
private string $videoSearchUrl = 'https://pixabay.com/api/videos/';
private string $apiKey;
private array $rateLimitInfo = [];
private $errorObj = null;
public function __construct(string $apiKey)
{
if (trim($apiKey) === '') {
throw new PixabayException('Please specify a Pixabay API key.');
}
$this->apiKey = $apiKey;
$this->imageSearchUrl = $this->imageSearchUrl . "?key={$this->apiKey}";
$this->videoSearchUrl = $this->videoSearchUrl . "?key={$this->apiKey}";
}
/**
* Executes a search of Pixabay images using the query options specified.
*
* @param array $queryOptions - An array of query options supported by the Pixabay API
* @return array|stdClass Returns an array of PixabayImage objects if successful or a stdClass if a failure/callback option is set.
* @throws PixabayException If invalid query option value is detected or unable to communicate with the API.
*/
public function searchImages(array $queryOptions)
{
$this->validateQueryOptions($queryOptions);
$response = $this->queryResults($this->buildQueryUrl($this->imageSearchUrl, $queryOptions));
if (($response->status_code !== 200) || (array_key_exists('callback', $queryOptions))) {
return $response;
}
return $this->parseResponse($response, 'PixabayImage');
}
/**
* Executes a search of Pixabay videos using the query options specified.
*
* @param array $queryOptions - An array of query options supported by the Pixabay API
* @return array|stdClass Returns an array of PixabayVideo objects if successful or a stdClass if a failure/callback option is set.
* @throws PixabayException If invalid query option value is detected or unable to communicate with the API.
*/
public function searchVideos(array $queryOptions)
{
$this->validateQueryOptions($queryOptions, 'video');
$response = $this->queryResults($this->buildQueryUrl($this->videoSearchUrl, $queryOptions));
if (($response->status_code !== 200) || (array_key_exists('callback', $queryOptions))) {
return $response;
}
return $this->parseResponse($response, 'PixabayVideo');
}
/**
* Returns the error number if an error contacting the API was encountered.
*
* @return int|null The error number if an error occurred, otherwise null.
*/
public function getErrorNumber()
{
return !is_null($this->errorObj) ? $this->errorObj->errorno : null;
}
/**
* Returns the error message if an error contacting the API was encountered.
*
* @return int|null The error message if an error occurred, otherwise null.
*/
public function getErrorMessage()
{
return !is_null($this->errorObj) ? $this->errorObj->errormsg : null;
}
/**
* Returns rate limit information from the Pixabay API response headers
*
* @return array An array of rate limit information based on last query made.
*/
public function getRateLimitInfo()
{
return $this->rateLimitInfo;
}
/**
* Private helper methods
*/
/**
* Parses the response body and builds the result list if hits were detected.
*
* @param stdClass $response - API response object that needs to be parsed
* @param string $resultType - The 'PixabayImage' or 'PixabayVideo' result type to be built
* @return array An array of instances of the appropriate result type or empty if no hits were seen.
*/
private function parseResponse($response, string $resultType = 'PixabayImage')
{
$resultList = [];
$results = json_decode($response->content);
if (!is_null($results) && property_exists($results, 'hits')) {
foreach ($results->hits as $result) {
$classname = 'pixabayapi\\lib\\' . $resultType;
$resultList[] = new $classname($result);
}
}
return $resultList;
}
/**
* Queries the Pixabay API for results based on the query URL passed in.
*
* @param string $queryUrl - URL to call on the API to get results
* @return stdClass A standard class instance with a status code and content property.
* @throws PixabayException An exception is thrown if there was a problem contacting the API.
*/
private function queryResults(string $queryUrl)
{
$this->errorObj = null;
$searchResponse = CurlRequests::get($queryUrl);
$this->populateRateHeaders(CurlRequests::$responseHeaders);
if (property_exists($searchResponse, 'errorno')) {
$this->errorObj = $searchResponse;
throw new PixabayException('Error establishing a connection to the Pixabay API.');
}
return $searchResponse;
}
/**
* Builds an API query URL based on the query options
*
* @param string $url - Base URL (for images or videos... key already included)
* @param array $queryOptions - An array of query options to build the string with
* @return string Returns a URL string
*/
private function buildQueryUrl(string $url, array $queryOptions) {
$query = !empty($queryOptions) ? '&' . http_build_query($queryOptions) : '';
return $url . $query;
}
/**
* Populates the rateLimitInfo property
*
* @param array $responseHeaders - Headers that came from the Pixabay API response
* @return void
*/
private function populateRateHeaders(array $responseHeaders)
{
$this->rateLimitInfo['x-ratelimit-limit'] = isset($responseHeaders['x-ratelimit-limit'][0]) ? $responseHeaders['x-ratelimit-limit'][0] : 0;
$this->rateLimitInfo['x-ratelimit-remaining'] = isset($responseHeaders['x-ratelimit-remaining'][0]) ? $responseHeaders['x-ratelimit-remaining'][0] : 0;
$this->rateLimitInfo['x-ratelimit-reset'] = isset($responseHeaders['x-ratelimit-reset'][0]) ? $responseHeaders['x-ratelimit-reset'][0] : 0;
}
/**
* Validate query options. This will throw a PixabayException if any option validation fails.
*
* @param array $queryOptions - Array of query options to validate
* @param string $queryType - Value of 'image' or 'video' to determine which options are possible to validate.
* @return void
* @throws PixabayException Throws an exception if any query options fail validation.
*/
private function validateQueryOptions(array $queryOptions, string $queryType = 'image')
{
if (array_key_exists('pretty', $queryOptions)) {
throw new PixabayException('Pretty print is not supported.');
}
if (array_key_exists('q', $queryOptions) && strlen($queryOptions['q']) > 100) {
throw new PixabayException('Query (q) cannot exceed 100 characters.');
}
$verifyOptions = [
'lang' => 'validateLanguage',
'category' => 'validateCategory',
'min_width' => 'validateMinWidth',
'min_height' => 'validateMinHeight',
'colors' => 'validateColors',
'editors_choice' => 'validateEditorsChoice',
'safesearch' => 'validateSafeSearch',
'order' => 'validateOrder',
'page' => 'validatePage',
'per_page' => 'validatePerPage'
];
if ($queryType === 'image') {
$verifyOptions['image_type'] = 'validateImageType';
$verifyOptions['orientation'] = 'validateOrientation';
} else {
$verifyOptions['video_type'] = 'validateVideoType';
}
// Throw an exception if any specified option doesn't meet requirements.
foreach ($verifyOptions as $name => $functionName) {
$this->validateOption($queryOptions, $name, [$this, $functionName]);
}
}
/**
* Validates a single query option and throws an exception if it fails.
*
* @param array $queryOptions - Array of query options being specified
* @param string $optionName - Name of the option to verify
* @param callable $func - Callable function to call to do the validation
* @return void
* @throws PixabayException Throws a PixabayException if any option fails its validation
*/
private function validateOption(array $queryOptions, string $optionName, callable $func)
{
if (array_key_exists($optionName, $queryOptions) && !$func($queryOptions[$optionName])) {
throw new PixabayException("Invalid value for option '{$optionName}' specified.");
}
}
/**
* Validates the given language is in the range of acceptable languages
*
* @param string $lang - Language code to check
* @return bool True if it is valid, false otherwise.
*/
private function validateLanguage(string $lang)
{
return in_array($lang, [
'cs', 'da', 'de', 'en', 'es', 'fr', 'id', 'it', 'hu', 'nl', 'no', 'pl',
'pt', 'ro', 'sk', 'fi', 'sv', 'tr', 'vi', 'th', 'bg', 'ru', 'el', 'ja',
'ko', 'zh'
]);
}
/**
* Validates the given image type is in the range of acceptable image typos
*
* @param string $imageType - Type of image to search for
* @return bool True if it is valid, false otherwise.
*/
private function validateImageType(string $imageType)
{
return in_array($imageType, ['all', 'photo', 'illustration', 'vector']);
}
/**
* Validates the given video type is in the range of acceptable video typos
*
* @param string $videoType - Type of video to search for
* @return bool True if it is valid, false otherwise.
*/
private function validateVideoType(string $videoType)
{
return in_array($videoType, ['all', 'film', 'animation', 'vector']);
}
/**
* Validates an image orientation is in the range of valid orientations
*
* @param string $orientation - Orientation "all", "horizontal" or "vertical"
* @return bool True if it is valid, false otherwise.
*/
private function validateOrientation(string $orientation)
{
return in_array($orientation, ['all', 'horizontal', 'vertical']);
}
/**
* Validate a catagory is in the range of valid catagories
*
* @param string $category - Name of the catagory to search
* @return bool True if it is valid, false otherwise.
*/
private function validateCategory(string $category)
{
return in_array($category, [
'backgrounds', 'fashion', 'nature', 'science', 'education', 'feelings',
'health', 'people', 'religion', 'places', 'animals', 'industry', 'computer',
'food', 'sports', 'transportation', 'travel', 'buildings', 'business', 'music'
]);
}
/**
* Validate minimum width
*
* @param string|int $minWidth - The minimum width of the image or video that should be returned
* @return bool True if it is valid, false otherwise.
*/
private function validateMinWidth($minWidth)
{
$validMinWidth = filter_var($minWidth, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]);
return ($validMinWidth !== false);
}
/**
* Validate minimum height
*
* @param string|int $minheight - The minimum height of the image or video that should be returned
* @return bool True if it is valid, false otherwise.
*/
private function validateMinHeight($minHeight)
{
$validMinHeight = filter_var($minHeight, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]);
return ($validMinHeight !== false);
}
/**
* Validates that at least one color is specified in the range of all valid colors
*
* @param string $colors - Comma separated string of colors the images should contain (invalid colors are ignored by the API)
* @return bool True if it is valid, false otherwise.
*/
private function validateColors(string $colors)
{
$acceptedColors = ['grayscale', 'transparent', 'red', 'orange', 'yellow', 'green', 'turquoise', 'blue', 'lilac', 'pink', 'white', 'gray', 'black', 'brown'];
$specifiedColors = array_map('trim', explode(',', $colors));
return count(array_intersect($specifiedColors, $acceptedColors)) > 0;
}
/**
* Validate the editors choice option (must be "true" or "false")
*
* @param string $choice - String representing true or false if editor's choice is enabled
* @return bool True if it is valid, false otherwise.
*/
private function validateEditorsChoice(string $choice)
{
return in_array($choice, ["true", "false"]);
}
/**
* Validate the safe search option (must be "true" or "false")
*
* @param string $safeSearch - String representing true or false if safe search is enabled
* @return bool True if it is valid, false otherwise.
*/
private function validateSafeSearch(string $safeSearch)
{
return in_array($safeSearch, ["true", "false"]);
}
/**
* Validates the order category search option
*
* @param string $order - Order being 'popular' or 'latest'
* @return bool True if it is valid, false otherwise.
*/
private function validateOrder(string $order)
{
return in_array($order, ['popular', 'latest']);
}
/**
* Validate page option
*
* @param string|int $pageNumber - The page number in paged results (1 minimum)
* @return bool True if it is valid, false otherwise.
*/
private function validatePage($pageNumber)
{
$validPageNumber = filter_var($pageNumber, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
return ($validPageNumber !== false);
}
/**
* Validate per_page option
*
* @param string|int $perPage - The number of results per page in the range of 3 - 200
* @return bool True if it is valid, false otherwise.
*/
private function validatePerPage($perPage)
{
$validPerPage = filter_var($perPage, FILTER_VALIDATE_INT, ['options' => ['min_range' => 3, 'max_range' => 200]]);
return ($validPerPage !== false);
}
}