Skip to content

Commit

Permalink
Merge pull request #19 from serpapi/update-restclient
Browse files Browse the repository at this point in the history
Update RestClient
  • Loading branch information
Freaky authored Apr 9, 2024
2 parents fc3a0f2 + e666bd1 commit 3a2b4cc
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions restclient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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;

Expand All @@ -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);

Expand Down

0 comments on commit 3a2b4cc

Please sign in to comment.