Curl is now returning response body even on error

It is better to switch off CURLOPT_FAILONERROR and check response status manually by HTTP response code
This commit is contained in:
Petr Bechyně 2017-02-17 11:06:52 +01:00
parent 5c0bf914e3
commit baf32c1350
2 changed files with 11 additions and 18 deletions

View File

@ -62,7 +62,7 @@ class Curl
curl_setopt_array($curlSession, [ curl_setopt_array($curlSession, [
CURLOPT_ENCODING => '', CURLOPT_ENCODING => '',
CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FAILONERROR => true, CURLOPT_FAILONERROR => false,
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_HEADER => true, CURLOPT_HEADER => true,
@ -147,6 +147,8 @@ class Curl
$headerSize = curl_getinfo($curlSession, CURLINFO_HEADER_SIZE); $headerSize = curl_getinfo($curlSession, CURLINFO_HEADER_SIZE);
$httpResponseCode = curl_getinfo($curlSession, CURLINFO_HTTP_CODE); $httpResponseCode = curl_getinfo($curlSession, CURLINFO_HTTP_CODE);
$httpResponseContentType = curl_getinfo($curlSession, CURLINFO_CONTENT_TYPE);; $httpResponseContentType = curl_getinfo($curlSession, CURLINFO_CONTENT_TYPE);;
$responseBody = substr($executeSoapCallResponse, $headerSize);
$responseHeaders = substr($executeSoapCallResponse, 0, $headerSize);
preg_match('/HTTP\/(1\.[0-1]+) ([0-9]{3}) (.*)/', $executeSoapCallResponse, $httpResponseMessages); preg_match('/HTTP\/(1\.[0-1]+) ([0-9]{3}) (.*)/', $executeSoapCallResponse, $httpResponseMessages);
$httpResponseMessage = trim(array_pop($httpResponseMessages)); $httpResponseMessage = trim(array_pop($httpResponseMessages));
$curlErrorMessage = sprintf( $curlErrorMessage = sprintf(
@ -156,7 +158,7 @@ class Curl
$location $location
); );
if ($executeSoapCallResponse === false) { if (!is_integer($httpResponseCode) || $httpResponseCode >= 400) {
return new CurlResponse( return new CurlResponse(
$httpRequestHeadersAsString, $httpRequestHeadersAsString,
@ -164,6 +166,8 @@ class Curl
$httpResponseMessage, $httpResponseMessage,
$httpResponseContentType, $httpResponseContentType,
self::CURL_FAILED, self::CURL_FAILED,
$responseHeaders,
$responseBody,
$curlErrorMessage $curlErrorMessage
); );
} }
@ -174,9 +178,8 @@ class Curl
$httpResponseMessage, $httpResponseMessage,
$httpResponseContentType, $httpResponseContentType,
self::CURL_SUCCESS, self::CURL_SUCCESS,
null, $responseHeaders,
substr($executeSoapCallResponse, 0, $headerSize), $responseBody
substr($executeSoapCallResponse, $headerSize)
); );
} }

View File

@ -19,9 +19,9 @@ class CurlResponse
$httpResponseStatusMessage, $httpResponseStatusMessage,
$httpResponseContentType, $httpResponseContentType,
$curlStatus, $curlStatus,
$curlErrorMessage = null, $responseHeader,
$responseHeader = null, $responseBody,
$responseBody = null $curlErrorMessage = null
) { ) {
$this->httpRequestHeaders = $httpRequestHeaders; $this->httpRequestHeaders = $httpRequestHeaders;
$this->httpResponseStatusCode = $httpResponseStatusCode; $this->httpResponseStatusCode = $httpResponseStatusCode;
@ -78,21 +78,11 @@ class CurlResponse
return $this->curlErrorMessage; return $this->curlErrorMessage;
} }
public function hasResponseHeader()
{
return $this->responseHeader !== null;
}
public function getResponseHeader() public function getResponseHeader()
{ {
return $this->responseHeader; return $this->responseHeader;
} }
public function hasResponseBody()
{
return $this->responseBody !== null;
}
public function getResponseBody() public function getResponseBody()
{ {
return $this->responseBody; return $this->responseBody;