1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace vfalies\tmdb; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Tmdb wrapper core class |
7
|
|
|
*/ |
8
|
|
|
class Tmdb implements Interfaces\TmdbInterface |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
// Private variables |
12
|
|
|
private $api_key = null; // API Key |
13
|
|
|
private $language = 'fr-FR'; // Default language for API response |
14
|
|
|
public $base_api_url = 'https://api.themoviedb.org/3/'; // Base URL of the API |
15
|
|
|
private $include_adult = false; // Include adult content in search result |
16
|
|
|
private $page = 1; // API Page result |
17
|
|
|
// Protected variables |
18
|
|
|
protected $configuration = null; // API Configuration |
19
|
|
|
protected $genres = null; // API Genres |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor |
23
|
|
|
* @param string $api_key TMDB API Key |
24
|
|
|
*/ |
25
|
|
|
|
26
|
127 |
|
public function __construct(string $api_key) |
27
|
|
|
{ |
28
|
127 |
|
$this->api_key = $api_key; |
29
|
127 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Send cUrl request to TMDB API |
33
|
|
|
* @param Interfaces\HttpRequestInterface $http_request |
34
|
|
|
* @param string $action API action to request |
35
|
|
|
* @param string $query Query of the request (optional) |
36
|
|
|
* @param array $options Array of options of the request (optional) |
37
|
|
|
* @return \stdClass |
38
|
|
|
*/ |
39
|
5 |
|
public function sendRequest(Interfaces\HttpRequestInterface $http_request, string $action, string $query = null, array $options = array()): \stdClass |
40
|
|
|
{ |
41
|
|
|
// Url construction |
42
|
5 |
|
$url = $this->base_api_url . $action; |
43
|
|
|
|
44
|
|
|
// Parameters |
45
|
5 |
|
$params = []; |
46
|
5 |
|
$params['api_key'] = $this->api_key; |
47
|
5 |
|
if (!is_null($query)) |
48
|
|
|
{ |
49
|
1 |
|
$params['query'] = $query; |
50
|
|
|
} |
51
|
|
|
|
52
|
5 |
|
$params = array_merge($params, $options); |
53
|
|
|
|
54
|
|
|
// URL with paramters construction |
55
|
5 |
|
$url = $url . '?' . http_build_query($params); |
56
|
|
|
|
57
|
5 |
|
$http_request->setUrl($url); |
58
|
5 |
|
$http_request->setOption(CURLOPT_HEADER, 0); |
59
|
5 |
|
$http_request->setOption(CURLOPT_RETURNTRANSFER, true); |
|
|
|
|
60
|
5 |
|
$http_request->setOption(CURLOPT_MAXREDIRS, 10); |
61
|
5 |
|
$http_request->setOption(CURLOPT_ENCODING, ""); |
62
|
5 |
|
$http_request->setOption(CURLOPT_TIMEOUT, 30); |
63
|
5 |
|
$http_request->setOption(CURLINFO_HEADER_OUT, true); // To gets header in curl_getinfo() |
|
|
|
|
64
|
|
|
|
65
|
5 |
|
$result = $http_request->execute(); |
66
|
|
|
|
67
|
4 |
|
$http_code = $http_request->getInfo(CURLINFO_HTTP_CODE); |
68
|
|
|
|
69
|
4 |
|
if ($http_code !== 200) |
70
|
|
|
{ |
71
|
2 |
|
if ($http_code == 429) |
72
|
|
|
{ |
73
|
1 |
|
$message = new \stdClass(); |
74
|
1 |
|
$message->message = 'Request rate limit exceeded'; |
75
|
1 |
|
$header_out = $http_request->getInfo(CURLINFO_HEADER_OUT); |
76
|
1 |
|
$message->headers = var_export($header_out, true); |
77
|
|
|
|
78
|
1 |
|
throw new \Exception(json_encode($message), 1006); |
79
|
|
|
} |
80
|
1 |
|
throw new \Exception('Incorrect HTTP Code (' . $http_code . ') response : ' . var_export($http_request->getInfo(), true), 1005); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// cUrl closing |
84
|
2 |
|
$http_request->close(); |
85
|
|
|
|
86
|
2 |
|
$response = json_decode($result); |
87
|
2 |
|
if (is_null($response) || $response === false) |
88
|
|
|
{ |
89
|
1 |
|
throw new \Exception('Search failed : ' . var_export($result, true), 2001); |
90
|
|
|
} |
91
|
1 |
|
return $response; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get API Configuration |
96
|
|
|
* @return \stdClass |
97
|
|
|
*/ |
98
|
8 |
|
public function getConfiguration(): \stdClass |
99
|
|
|
{ |
100
|
|
|
try |
101
|
|
|
{ |
102
|
8 |
|
if (is_null($this->configuration)) |
103
|
|
|
{ |
104
|
8 |
|
$this->configuration = $this->sendRequest(new CurlRequest(), 'configuration'); |
105
|
|
|
} |
106
|
7 |
|
return $this->configuration; |
107
|
|
|
} |
108
|
1 |
|
catch (\Exception $ex) |
109
|
|
|
{ |
110
|
1 |
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Check options rules before send request |
116
|
|
|
* @param array $options Array of options to validate |
117
|
|
|
* @return array |
118
|
|
|
* @throws \Exception |
119
|
|
|
*/ |
120
|
119 |
|
public function checkOptions(array $options): array |
121
|
|
|
{ |
122
|
119 |
|
$params = []; |
123
|
|
|
// Set default options |
124
|
119 |
|
$params['language'] = $this->language; |
125
|
119 |
|
$params['include_adult'] = $this->include_adult; |
126
|
119 |
|
$params['page'] = $this->page; |
127
|
|
|
// Check options |
128
|
119 |
|
foreach ($options as $key => $value) |
129
|
|
|
{ |
130
|
|
|
switch ($key) |
131
|
|
|
{ |
132
|
48 |
|
case 'year': |
133
|
2 |
|
$params[$key] = $this->checkYear($value); |
134
|
1 |
|
break; |
135
|
47 |
|
case 'language': |
136
|
44 |
|
$params[$key] = $this->checkLanguage($value); |
137
|
43 |
|
break; |
138
|
4 |
|
case 'include_adult': |
139
|
1 |
|
$params[$key] = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
140
|
1 |
|
break; |
141
|
4 |
|
case 'page': |
142
|
1 |
|
$params[$key] = (int) $value; |
143
|
1 |
|
break; |
144
|
|
|
default: |
145
|
46 |
|
throw new \Exception('Unknown options'); |
146
|
|
|
} |
147
|
|
|
} |
148
|
114 |
|
return $params; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Check year format |
153
|
|
|
* @param mixed $year year to validate |
154
|
|
|
* @return int year validated |
155
|
|
|
* @throws \Exception |
156
|
|
|
*/ |
157
|
1 |
|
private function checkYear(int $year): int |
158
|
|
|
{ |
159
|
1 |
|
$year = (int) $year; |
160
|
1 |
|
return $year; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Check language |
165
|
|
|
* @param string $language Language string with format ISO 639-1 |
166
|
|
|
* @return string Language string validated |
167
|
|
|
* @throws \Exception |
168
|
|
|
*/ |
169
|
44 |
|
private function checkLanguage(string $language): string |
170
|
|
|
{ |
171
|
44 |
|
$check = preg_match("#([a-z]{2})-([A-Z]{2})#", $language); |
172
|
44 |
|
if ($check === 0 || $check === false) |
173
|
|
|
{ |
174
|
1 |
|
throw new \Exception("Incorrect language code : $language", 1001); |
175
|
|
|
} |
176
|
43 |
|
return $language; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: