From 777df5f34577a10d864e927ac91ae7654f32ba71 Mon Sep 17 00:00:00 2001 From: Andrii Vasyliev Date: Sat, 28 Jan 2017 16:56:04 +0000 Subject: [PATCH] removed curl transport to yii2-hiart-curl --- src/RequestInterface.php | 4 +- src/curl/Request.php | 123 ------------------------------ src/curl/Response.php | 158 --------------------------------------- 3 files changed, 1 insertion(+), 284 deletions(-) delete mode 100644 src/curl/Request.php delete mode 100644 src/curl/Response.php diff --git a/src/RequestInterface.php b/src/RequestInterface.php index 7dca9e9..91d76e0 100644 --- a/src/RequestInterface.php +++ b/src/RequestInterface.php @@ -10,14 +10,12 @@ namespace hiqdev\hiart; -use hiqdev\hiart\curl\Response; - interface RequestInterface extends \Serializable { /** * @param array $options * @throws RequestErrorException - * @return Response + * @return ResponseInterface */ public function send($options = []); diff --git a/src/curl/Request.php b/src/curl/Request.php deleted file mode 100644 index 18a2eb2..0000000 --- a/src/curl/Request.php +++ /dev/null @@ -1,123 +0,0 @@ - false, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_HEADER => true, - ]; - - /** - * @param array $options - * @throws RequestErrorException - * @return array|mixed - */ - public function send($options = []) - { - try { - $this->build(); - - $curl = curl_init($this->getFullUri()); - curl_setopt_array($curl, $this->prepareCurlOptions($options)); - $response = curl_exec($curl); - $info = curl_getinfo($curl); - $error = curl_error($curl); - $errorCode = curl_errno($curl); - curl_close($curl); - } catch (RequestErrorException $e) { - throw $e; - } catch (\Exception $e) { - throw new RequestErrorException($e->getMessage(), $this, $e->getCode(), $e); - } - - return new $this->responseClass($this, $response, $info, $error, $errorCode); - } - - /** - * @param array $options - * @throws RequestErrorException - * @return array - */ - protected function prepareCurlOptions($options) - { - $requestOptions = $this->buildMethodOptions(); - $requestOptions[CURLOPT_HTTPHEADER] = $this->buildHeaderLines(); - - if ($this->getVersion() === '1.1') { - $requestOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1; - } elseif ($this->getVersion() === '1.0') { - $requestOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0; - } else { - throw new RequestErrorException('Request version "' . $this->getVersion() . '" is not support by cURL', $this); - } - - return ArrayHelper::merge($this->defaultOptions, $this->getDb()->config, $requestOptions, $options); - } - - /** - * @return array - */ - protected function buildMethodOptions() - { - $options = []; - - if ($this->getMethod() === 'GET') { - return $options; - } - - if (!empty($this->getBody())) { - $options[CURLOPT_POSTFIELDS] = $this->getBody(); - } - - if ($this->getMethod() === 'POST') { - $options[CURLOPT_POST] = true; - } else { - $options[CURLOPT_CUSTOMREQUEST] = $this->getMethod(); - } - - return $options; - } - - /** - * @return array - */ - protected function buildHeaderLines() - { - $result = []; - - foreach ($this->getHeaders() as $name => $values) { - $name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $name))); - if (is_string($values)) { - $values = [$values]; - } - foreach ($values as $value) { - $result[] = "$name: $value"; - } - } - - return $result; - } -} diff --git a/src/curl/Response.php b/src/curl/Response.php deleted file mode 100644 index 019387f..0000000 --- a/src/curl/Response.php +++ /dev/null @@ -1,158 +0,0 @@ -request = $request; - - $this->checkTransportError($error, $errorCode); - - $parsedResponse = $this->parseRawResponse($rawBody, $info); - $this->headers = $parsedResponse['headers']; - $this->statusCode = $parsedResponse['statusCode']; - $this->rawData = $parsedResponse['data']; - $this->reasonPhrase = $parsedResponse['reasonPhrase']; - } - - /** - * @return mixed|string - */ - public function getRawData() - { - return $this->rawData; - } - - /** - * @param string $name the header name - * @return array|null - */ - public function getHeader($name) - { - return isset($this->headers[$name]) ? $this->headers[$name] : null; - } - - /** - * {@inheritdoc} - */ - public function getStatusCode() - { - return $this->statusCode; - } - - /** - * {@inheritdoc} - */ - public function getReasonPhrase() - { - return $this->reasonPhrase; - } - - /** - * Parses raw response and returns parsed information. - * - * @param string $data the raw response - * @param array $info the curl information (result of `gurl_getinfo` call) - * @return array array with the following keys will be returned: - * - data: string, response data; - * - headers: array, response headers; - * - statusCode: string, the response status-code; - * - reasonPhrase: string, the response reason phrase (OK, NOT FOUND, etc) - */ - protected function parseRawResponse($data, $info) - { - $result = []; - - $headerSize = $info['header_size']; - $result['data'] = substr($data, $headerSize); - - $rawHeaders = explode("\r\n", substr($data, 0, $headerSize)); - // First line is status-code HTTP/1.1 200 OK - list(, $result['statusCode'], $result['reasonPhrase']) = explode(' ', array_shift($rawHeaders), 3); - foreach ($rawHeaders as $line) { - if ($line === '') { - continue; - } - - list($key, $value) = explode(': ', $line); - $result['headers'][$key][] = $value; - } - - return $result; - } - - /** - * Checks $error and $errorCode for transport errors. - * - * @param string $error the cURL error message, if present. Empty string otherwise. - * @param int $errorCode the cURL error code, if present. Integer `0` otherwise. - * @throws ResponseErrorException when the error is present - */ - protected function checkTransportError($error, $errorCode) - { - if ($error !== '' || $errorCode !== 0) { - throw new ResponseErrorException($error, $this, $errorCode); - } - } - - /** - * Returns array of all headers. - * Key - Header name - * Value - array of header values. For example:. - * - * ```php - * ['Location' => ['http://example.com'], 'Expires' => ['Thu, 01 Jan 1970 00:00:00 GMT']] - * @return array - */ - public function getHeaders() - { - return $this->headers; - } -}