add ability to get response code
This commit is contained in:
@@ -45,18 +45,29 @@ class RestClient {
|
|||||||
*/
|
*/
|
||||||
private $cookieFilePath;
|
private $cookieFilePath;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string|null The HTTP response code from the most recent request.
|
||||||
|
*/
|
||||||
|
private string $responseCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool Whether to throw exceptions on HTTP errors.
|
||||||
|
*/
|
||||||
|
private bool $throwExceptionsOnHttpError;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RestClient constructor.
|
* RestClient constructor.
|
||||||
*
|
*
|
||||||
* @param bool $useCookies Whether to use cookies for maintaining session (default: false).
|
* @param bool $useCookies Whether to use cookies for maintaining session (default: false).
|
||||||
*/
|
*/
|
||||||
public function __construct($cookieFilePath = '/tmp/rest_client_cookies.txt') {
|
public function __construct($cookieFilePath = '/tmp/rest_client_cookies.txt', $throwExceptionsOnHttpError = true) {
|
||||||
$this->headers = [
|
$this->headers = [
|
||||||
'Content-Type: application/json',
|
'Content-Type: application/json',
|
||||||
// Add any other headers you need
|
// Add any other headers you need
|
||||||
];
|
];
|
||||||
$this->useCookies = false;
|
$this->useCookies = false;
|
||||||
$this->cookieFilePath = $cookieFilePath;
|
$this->cookieFilePath = $cookieFilePath;
|
||||||
|
$this->throwExceptionsOnHttpError = $throwExceptionsOnHttpError;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -234,10 +245,12 @@ class RestClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
|
||||||
|
$this->responseCode = $httpCode;
|
||||||
|
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
|
||||||
if ($httpCode >= 400) {
|
if ($httpCode >= 400 && $this->throwExceptionsOnHttpError) {
|
||||||
throw new Exception('HTTP error: ' . $httpCode);
|
throw new Exception('HTTP error: ' . $httpCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,4 +280,13 @@ class RestClient {
|
|||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the HTTP response code from the most recent request.
|
||||||
|
*
|
||||||
|
* @return string The HTTP response code.
|
||||||
|
*/
|
||||||
|
public function getResponseCode() {
|
||||||
|
return $this->responseCode;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user