hiqdev /
yii2-hiart
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * ActiveRecord for API |
||
| 4 | * |
||
| 5 | * @link https://github.com/hiqdev/yii2-hiart |
||
| 6 | * @package yii2-hiart |
||
| 7 | * @license BSD-3-Clause |
||
| 8 | * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace hiqdev\hiart\curl; |
||
| 12 | |||
| 13 | use hiqdev\hiart\AbstractRequest; |
||
| 14 | use hiqdev\hiart\RequestErrorException; |
||
| 15 | use yii\helpers\ArrayHelper; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Class Request represents request using cURL library. |
||
| 19 | */ |
||
| 20 | class Request extends AbstractRequest |
||
| 21 | { |
||
| 22 | protected $responseClass = Response::class; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array default cURL options |
||
| 26 | */ |
||
| 27 | public $defaultOptions = [ |
||
| 28 | CURLOPT_SSL_VERIFYPEER => false, |
||
| 29 | CURLOPT_RETURNTRANSFER => true, |
||
| 30 | CURLOPT_HEADER => true, |
||
| 31 | ]; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param array $options |
||
| 35 | * @throws RequestErrorException |
||
| 36 | * @return array|mixed |
||
| 37 | */ |
||
| 38 | 2 | public function send($options = []) |
|
| 39 | { |
||
| 40 | try { |
||
| 41 | 2 | $this->build(); |
|
| 42 | |||
| 43 | 2 | $curl = curl_init($this->getFullUri()); |
|
| 44 | 2 | curl_setopt_array($curl, $this->prepareCurlOptions($options)); |
|
| 45 | 2 | $response = curl_exec($curl); |
|
| 46 | 2 | $info = curl_getinfo($curl); |
|
| 47 | 2 | $error = curl_error($curl); |
|
| 48 | 2 | $errorCode = curl_errno($curl); |
|
| 49 | 2 | curl_close($curl); |
|
| 50 | } catch (RequestErrorException $e) { |
||
| 51 | throw $e; |
||
| 52 | } catch (\Exception $e) { |
||
| 53 | throw new RequestErrorException($e->getMessage(), $this, $e->getCode(), $e); |
||
| 54 | } |
||
| 55 | |||
| 56 | 2 | return new $this->responseClass($this, $response, $info, $error, $errorCode); |
|
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param array $options |
||
| 61 | * @throws RequestErrorException |
||
| 62 | * @return array |
||
| 63 | */ |
||
| 64 | 2 | protected function prepareCurlOptions($options) |
|
| 65 | { |
||
| 66 | 2 | $requestOptions = $this->buildMethodOptions(); |
|
| 67 | 2 | $requestOptions[CURLOPT_HTTPHEADER] = $this->buildHeaderLines(); |
|
| 68 | |||
| 69 | 2 | if ($this->getVersion() === '1.1') { |
|
| 70 | 2 | $requestOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1; |
|
| 71 | } elseif ($this->getVersion() === '1.0') { |
||
| 72 | $requestOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0; |
||
| 73 | } else { |
||
| 74 | throw new RequestErrorException('Request version "' . $this->getVersion() . '" is not support by cURL', $this); |
||
| 75 | } |
||
| 76 | |||
| 77 | 2 | return ArrayHelper::merge($this->defaultOptions, $this->getDb()->config, $requestOptions, $options); |
|
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | 2 | protected function buildMethodOptions() |
|
| 84 | { |
||
| 85 | 2 | $options = []; |
|
| 86 | |||
| 87 | 2 | if ($this->getMethod() === 'GET') { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 88 | 2 | return $options; |
|
| 89 | } |
||
| 90 | |||
| 91 | if (!empty($this->getBody())) { |
||
| 92 | $options[CURLOPT_POSTFIELDS] = $this->getBody(); |
||
| 93 | } |
||
| 94 | |||
| 95 | if ($this->getMethod() === 'POST') { |
||
|
0 ignored issues
–
show
|
|||
| 96 | $options[CURLOPT_POST] = true; |
||
| 97 | } else { |
||
| 98 | $options[CURLOPT_CUSTOMREQUEST] = $this->getMethod(); |
||
| 99 | } |
||
| 100 | |||
| 101 | return $options; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | 2 | protected function buildHeaderLines() |
|
| 108 | { |
||
| 109 | 2 | $result = []; |
|
| 110 | |||
| 111 | 2 | foreach ($this->getHeaders() as $name => $values) { |
|
| 112 | 2 | $name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $name))); |
|
| 113 | 2 | if (is_string($values)) { |
|
| 114 | 2 | $values = [$values]; |
|
| 115 | } |
||
| 116 | 2 | foreach ($values as $value) { |
|
| 117 | 2 | $result[] = "$name: $value"; |
|
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | 2 | return $result; |
|
| 122 | } |
||
| 123 | |||
| 124 | 3 | public static function isSupported() |
|
| 125 | { |
||
| 126 | 3 | return function_exists('curl_version'); |
|
| 127 | } |
||
| 128 | } |
||
| 129 |