Files

70 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2024-12-30 01:51:28 +02:00
<?php
namespace App\Service\TMDBApi;
use CodeIgniter\HTTP\CURLRequest;
use Illuminate\Support\Facades\Http;
abstract class AbstractApiService
{
const BASE_URL = 'https://api.themoviedb.org/3/';
/** @var CURLRequest */
protected $client;
public function __construct()
{
// new guzzle client
// $this->client = service('curlrequest', options: [
// 'timeout' => 10.0,
// 'connect_timeout' => 150,
// 'debug' => false,
// 'verify' => false,
// ]);
}
public function get(string $path, array $parameters = [], array $headers = []): array
{
$path = self::BASE_URL.$path.'?api_key=49a4c5ff00d832668aa505feaf197955';
$headers['Authorization'] = 'Bearer 49a4c5ff00d832668aa505feaf197955';
$headers['accept'] = 'application/json';
try {
$r = Http::withHeaders($headers)->get($path);
// $r = $this->client->request('GET', $path, [
// 'headers' => $headers
// ]);
} catch (\Throwable $e) {
var_dump($e->getMessage());
die;
}
return $this->decodeResponse(
$r
);
}
private function decodeResponse($response)
{
try {
if ($response->body()) {
return json_decode($response->body(), true, 512, JSON_THROW_ON_ERROR);
}
return [];
} catch (JsonException $e) {
throw new UnexpectedResponseException(
sprintf(
'Unable to decode response with body "%s", %s.',
(string)$response->body(),
json_last_error_msg()
),
$response->status(),
$e
);
}
}
}