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