Select Git revision
CurlClient.php
-
Piotr Potrebka authoredPiotr Potrebka authored
CurlClient.php 5.16 KiB
<?php
namespace WPDesk\HttpClient\Curl;
use WPDesk\HttpClient\HttpClient;
use WPDesk\HttpClient\HttpClientResponse;
use WPDesk\HttpClient\HttpClientRequestException;
class CurlClient implements HttpClient {
/** @var resource */
private $curlResource;
/** @var string|null|boolean */
private $rawResponse;
/** @var int */
private $httpResponseCode;
/** @var array */
private $headers = array();
/**
* @param string $url
* @param string $body
* @param array $headers
* @param int $timeOut
*
* @return HttpClientResponse
* @throws HttpClientRequestException
*/
public function get( $url, $body, array $headers, $timeOut ) {
return $this->send( $url, 'GET', $body, $headers, $timeOut );
}
/**
* @param string $url
* @param string $method
* @param string $body
* @param array $headers
* @param int $timeOut
*
* @return HttpClientResponse
* @throws HttpClientRequestException
*/
public function send( $url, $method, $body, array $headers, $timeOut ) {
$this->initResource();
try {
$this->prepareConnection( $url, $method, $body, $headers, $timeOut );
$this->sendRequest();
$this->throwExceptionIfError();
$this->closeConnection();
return $this->prepareResponse();
} catch ( \Exception $e ) {
$this->closeConnection();
if ( $e instanceof HttpClientRequestException ) {
throw $e;
}
throw CurlExceptionFactory::createDefaultException( $e->getMessage(), $e->getCode(), $e );
}
}
private function initResource() {
$this->curlResource = \curl_init();
}
/**
* Opens a new curl connection.
*
* @param string $url The endpoint to send the request to.
* @param string $method The request method.
* @param string $body The body of the request.
* @param array $headers The request headers.