Skip to content
Snippets Groups Projects
Select Git revision
  • d6c43c7620e631cedad0d608e92d9766809481e6
  • master default protected
  • devel
  • feature/add-escaping-to-templates
  • feature/add-priority-sorting
  • 3.3.0
  • 3.2.1
  • 3.2.0
  • 3.1.0
  • 3.0.0
  • 2.4.12
  • 2.4.11
  • 2.4.10
  • 2.4.9
  • 2.4.8
  • 2.4.7
  • 2.4.6
  • 2.4.5
  • 2.4.4
  • 2.4.2
  • 2.4.1
  • 2.4.0
  • 2.3.2
  • 2.3.1
  • 2.3
25 results

phpcs.xml.dist

Blame
  • 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.