From e666bd11f63199757a2a98b478a765b310083a72 Mon Sep 17 00:00:00 2001 From: Thomas Hurst Date: Mon, 1 Apr 2024 21:23:09 +0000 Subject: [PATCH] Update RestClient Resolves a deprecation notice from PHP 8.2+. --- restclient.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/restclient.php b/restclient.php index d490ac2..01adb88 100755 --- a/restclient.php +++ b/restclient.php @@ -12,6 +12,7 @@ class RestClient implements Iterator, ArrayAccess { public $options; public $handle; // cURL resource handle. + public $url; // Populated after execution: public $response; // Response body. @@ -148,7 +149,7 @@ public function execute(string $url, string $method='GET', $parameters=[], array $headers = array_merge($client->options['headers'], $headers); foreach($headers as $key => $values){ foreach(is_array($values)? $values : [$values] as $value){ - $curlopt[CURLOPT_HTTPHEADER][] = sprintf("%s:%s", $key, $value); + $curlopt[CURLOPT_HTTPHEADER][] = sprintf("%s: %s", $key, $value); } } } @@ -187,9 +188,9 @@ public function execute(string $url, string $method='GET', $parameters=[], array } if($client->options['base_url']){ - if($client->url[0] !== '/' && substr($client->options['base_url'], -1) !== '/') - $client->url = '/' . $client->url; - $client->url = $client->options['base_url'] . $client->url; + $client->url = sprintf("%s/%s", + rtrim((string) $client->options['base_url'], '/'), + ltrim((string) $client->url, '/')); } $curlopt[CURLOPT_URL] = $client->url; @@ -201,7 +202,9 @@ public function execute(string $url, string $method='GET', $parameters=[], array } curl_setopt_array($client->handle, $curlopt); - $client->parse_response(curl_exec($client->handle)); + $response = curl_exec($client->handle); + if($response !== FALSE) + $client->parse_response($response); $client->info = (object) curl_getinfo($client->handle); $client->error = curl_error($client->handle);