Skip to content
Snippets Groups Projects
Commit 8d22f64b authored by Marcin Kolanko's avatar Marcin Kolanko
Browse files

Merge branch 'devel' into 'master'

Devel

See merge request !3
parents ad875763 2a9679ea
No related branches found
No related tags found
1 merge request!3Devel
Pipeline #5107 failed with stages
in 2 minutes and 17 seconds
= 1.0.2 - 2019-12-06 =
* Fixed issue with headers
= 1.0.1 - 2019-12-05 = = 1.0.1 - 2019-12-05 =
* Fixed issue with slicing resposne on CurlClient * Fixed issue with slicing resposne on CurlClient
......
<?php <?php
namespace WPDesk\HttpClient\Curl; namespace iFirmaVendor\WPDesk\HttpClient\Curl;
use WPDesk\HttpClient\HttpClient; use iFirmaVendor\WPDesk\HttpClient\HttpClient;
use WPDesk\HttpClient\HttpClientResponse; use iFirmaVendor\WPDesk\HttpClient\HttpClientResponse;
use WPDesk\HttpClient\HttpClientRequestException; use iFirmaVendor\WPDesk\HttpClient\HttpClientRequestException;
class CurlClient implements HttpClient class CurlClient implements \iFirmaVendor\WPDesk\HttpClient\HttpClient {
{ /** @var resource */
/** @var resource */ private $curlResource;
private $curlResource; /** @var string|null|boolean */
private $rawResponse;
/** @var string|null|boolean */ /** @var int */
private $rawResponse; private $httpResponseCode;
/** @var array */
/** @var int */ private $headers = array();
private $httpResponseCode;
/**
/** * @param string $url
* @param string $url * @param string $body
* @param string $body * @param array $headers
* @param array $headers * @param int $timeOut
* @param int $timeOut *
* @return HttpClientResponse * @return HttpClientResponse
* @throws HttpClientRequestException * @throws HttpClientRequestException
*/ */
public function get($url, $body, array $headers, $timeOut) public function get( $url, $body, array $headers, $timeOut ) {
{ return $this->send( $url, 'GET', $body, $headers, $timeOut );
return $this->send($url, 'GET', $body, $headers, $timeOut); }
}
/**
/** * @param string $url
* @param string $url * @param string $method
* @param string $method * @param string $body
* @param string $body * @param array $headers
* @param array $headers * @param int $timeOut
* @param int $timeOut *
* @return HttpClientResponse * @return HttpClientResponse
* @throws HttpClientRequestException * @throws HttpClientRequestException
*/ */
public function send($url, $method, $body, array $headers, $timeOut) public function send( $url, $method, $body, array $headers, $timeOut ) {
{ $this->initResource();
$this->initResource(); try {
try { $this->prepareConnection( $url, $method, $body, $headers, $timeOut );
$this->prepareConnection($url, $method, $body, $headers, $timeOut); $this->sendRequest();
$this->sendRequest(); $this->throwExceptionIfError();
$this->throwExceptionIfError(); $this->closeConnection();
$this->closeConnection();
return $this->prepareResponse();
return $this->prepareResponse(); } catch ( \Exception $e ) {
$this->closeConnection();
} catch (\Exception $e) { if ( $e instanceof \iFirmaVendor\WPDesk\HttpClient\HttpClientRequestException ) {
$this->closeConnection(); throw $e;
if ($e instanceof HttpClientRequestException) { }
throw $e; throw \iFirmaVendor\WPDesk\HttpClient\Curl\CurlExceptionFactory::createDefaultException( $e->getMessage(), $e->getCode(), $e );
} }
throw CurlExceptionFactory::createDefaultException($e->getMessage(), $e->getCode(), $e); }
}
} private function initResource() {
$this->curlResource = \curl_init();
private function initResource() }
{
$this->curlResource = curl_init(); /**
} * Opens a new curl connection.
*
/** * @param string $url The endpoint to send the request to.
* Opens a new curl connection. * @param string $method The request method.
* * @param string $body The body of the request.
* @param string $url The endpoint to send the request to. * @param array $headers The request headers.
* @param string $method The request method. * @param int $timeOut The timeout in seconds for the request.
* @param string $body The body of the request. */
* @param array $headers The request headers. private function prepareConnection( $url, $method, $body, array $headers, $timeOut ) {
* @param int $timeOut The timeout in seconds for the request.
*/ $options = [
private function prepareConnection($url, $method, $body, array $headers, $timeOut) \CURLOPT_CUSTOMREQUEST => $method,
{ \CURLOPT_HTTPHEADER => $this->compileRequestHeaders( $headers ),
$options = [ \CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => $method, \CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_HTTPHEADER => $this->compileRequestHeaders($headers), \CURLOPT_TIMEOUT => $timeOut,
CURLOPT_URL => $url, \CURLOPT_RETURNTRANSFER => \true,
CURLOPT_CONNECTTIMEOUT => 10, // Return response as string
CURLOPT_TIMEOUT => $timeOut, \CURLOPT_HEADER => \false,
CURLOPT_RETURNTRANSFER => true, // Return response as string // Enable header processing
CURLOPT_HEADER => true, // Enable header processing \CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYHOST => 2, \CURLOPT_SSL_VERIFYPEER => \true,
CURLOPT_SSL_VERIFYPEER => true, \CURLOPT_HEADERFUNCTION => function ( $curl, $header ) {
//CURLOPT_CAINFO => __DIR__ . '/certs/DigiCertHighAssuranceEVRootCA.pem', $len = strlen( $header );
]; $this->headers[] = trim( $header );
if ($method !== "GET") {
$options[CURLOPT_POSTFIELDS] = $body; return $len;
} }
];
curl_setopt_array($this->curlResource, $options); if ( $method !== "GET" ) {
} $options[ \CURLOPT_POSTFIELDS ] = $body;
}
/** \curl_setopt_array( $this->curlResource, $options );
* Compiles the request headers into a curl-friendly format. }
*
* @param array $headers The request headers. /**
* * Compiles the request headers into a curl-friendly format.
* @return array *
*/ * @param array $headers The request headers.
private function compileRequestHeaders(array $headers) *
{ * @return array
$return = []; */
foreach ($headers as $key => $value) { private function compileRequestHeaders( array $headers ) {
$return[] = $key . ': ' . $value; $return = [];
} foreach ( $headers as $key => $value ) {
return $return; $return[] = $key . ': ' . $value;
} }
/** return $return;
* Send the request and get the raw response from curl }
*/
private function sendRequest() /**
{ * Send the request and get the raw response from curl
$this->rawResponse = curl_exec($this->curlResource); */
$this->httpResponseCode = $this->getHttpResponseCode(); private function sendRequest() {
} $this->rawResponse = \curl_exec( $this->curlResource );
$this->httpResponseCode = $this->getHttpResponseCode();
/** @return int */ }
private function getHttpResponseCode()
{ /** @return int */
return intval(curl_getinfo($this->curlResource, CURLINFO_HTTP_CODE)); private function getHttpResponseCode() {
} return \intval( \curl_getinfo( $this->curlResource, \CURLINFO_HTTP_CODE ) );
}
private function throwExceptionIfError()
{ private function throwExceptionIfError() {
$errorNumber = curl_errno($this->curlResource); $errorNumber = \curl_errno( $this->curlResource );
if ($errorNumber === 0) { if ( $errorNumber === 0 ) {
return; return;
} }
$errorMessage = \curl_error( $this->curlResource );
$errorMessage = curl_error($this->curlResource); throw \iFirmaVendor\WPDesk\HttpClient\Curl\CurlExceptionFactory::createCurlException( $errorMessage, $errorNumber );
throw CurlExceptionFactory::createCurlException($errorMessage, $errorNumber); }
}
/**
/** * Closes an existing curl connection
* Closes an existing curl connection */
*/ private function closeConnection() {
private function closeConnection() \curl_close( $this->curlResource );
{ $this->curlResource = null;
curl_close($this->curlResource); }
$this->curlResource = null;
} private function prepareResponse() {
list( $rawHeaders, $rawBody ) = $this->extractResponseHeadersAndBody();
private function prepareResponse()
{ return new \iFirmaVendor\WPDesk\HttpClient\HttpClientResponse( $rawHeaders, $rawBody, $this->httpResponseCode );
list($rawHeaders, $rawBody) = $this->extractResponseHeadersAndBody(); }
return new HttpClientResponse($rawHeaders, $rawBody, $this->httpResponseCode);
} /**
* Extracts the headers and the body into a two-part array
/** *
* Extracts the headers and the body into a two-part array * @return array
* */
* @return array private function extractResponseHeadersAndBody() {
*/ $rawBody = \trim( $this->rawResponse );
private function extractResponseHeadersAndBody() $rawHeaders = \trim( implode( "\r\n", $this->headers ) );
{
$parts = explode("\r\n\r\n", $this->rawResponse, 2 ); return [ $rawHeaders, $rawBody ];
$rawBody = array_pop($parts); }
$rawHeaders = implode("\r\n\r\n", $parts);
return [trim($rawHeaders), trim($rawBody)]; /**
} * @param string $url
* @param string $body
/** * @param array $headers
* @param string $url * @param int $timeOut
* @param string $body *
* @param array $headers * @return HttpClientResponse
* @param int $timeOut * @throws HttpClientRequestException
* @return HttpClientResponse */
* @throws HttpClientRequestException public function post( $url, $body, array $headers, $timeOut ) {
*/ return $this->send( $url, 'POST', $body, $headers, $timeOut );
public function post($url, $body, array $headers, $timeOut) }
{
return $this->send($url, 'POST', $body, $headers, $timeOut); /**
} * @param string $url
* @param string $body
/** * @param array $headers
* @param string $url * @param int $timeOut
* @param string $body *
* @param array $headers * @return HttpClientResponse
* @param int $timeOut * @throws HttpClientRequestException
* @return HttpClientResponse */
* @throws HttpClientRequestException public function delete( $url, $body, array $headers, $timeOut ) {
*/ return $this->send( $url, 'DELETE', $body, $headers, $timeOut );
public function delete($url, $body, array $headers, $timeOut) }
{
return $this->send($url, 'DELETE', $body, $headers, $timeOut); /**
} * @param string $url
* @param string $body
/** * @param array $headers
* @param string $url * @param int $timeOut
* @param string $body *
* @param array $headers * @return HttpClientResponse
* @param int $timeOut * @throws HttpClientRequestException
* @return HttpClientResponse */
* @throws HttpClientRequestException public function put( $url, $body, array $headers, $timeOut ) {
*/ return $this->send( $url, 'PUT', $body, $headers, $timeOut );
public function put($url, $body, array $headers, $timeOut) }
{
return $this->send($url, 'PUT', $body, $headers, $timeOut);
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment