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\stream; |
12
|
|
|
|
13
|
|
|
use hiqdev\hiart\AbstractRequest; |
14
|
|
|
use hiqdev\hiart\RequestErrorException; |
15
|
|
|
use yii\helpers\ArrayHelper; |
16
|
|
|
use yii\helpers\Inflector; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* PHP stream request implementation. |
20
|
|
|
* |
21
|
|
|
* @author Andrii Vasyliev <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Request extends AbstractRequest |
24
|
|
|
{ |
25
|
|
|
protected $responseClass = Response::class; |
26
|
|
|
|
27
|
|
|
public $defaultOptions = [ |
28
|
|
|
'http' => [ |
29
|
|
|
'ignore_errors' => true, |
30
|
|
|
], |
31
|
|
|
'ssl' => [ |
32
|
|
|
'verify_peer' => false, |
33
|
|
|
], |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $options |
38
|
|
|
* @throws RequestErrorException |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
|
|
public function send($options = []) |
42
|
|
|
{ |
43
|
|
|
try { |
44
|
|
|
$this->build(); |
45
|
|
|
$context = stream_context_create($this->prepareContextOptions($options)); |
46
|
|
|
$stream = fopen($this->getFullUri(), 'rb', false, $context); |
47
|
|
|
$responseContent = stream_get_contents($stream); |
48
|
|
|
// see http://php.net/manual/en/reserved.variables.httpresponseheader.php |
49
|
|
|
$responseHeaders = $http_response_header; |
50
|
|
|
fclose($stream); |
51
|
|
|
} catch (\Exception $e) { |
52
|
|
|
throw new RequestErrorException($e->getMessage(), $this, $e->getCode(), $e); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return new $this->responseClass($this, $responseContent, $responseHeaders); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function prepareContextOptions($options) |
59
|
|
|
{ |
60
|
|
|
$requestOptions = [ |
61
|
|
|
'http' => [ |
62
|
|
|
'protocol_version' => $this->version, |
63
|
|
|
'method' => $this->method, |
64
|
|
|
'header' => static::composeHeaderLines($this->headers), |
65
|
|
|
], |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
if (isset($this->body)) { |
69
|
|
|
$requestOptions['http']['content'] = $this->body; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$dbOptions = $this->convertContextOptions($this->getDb()->config); |
73
|
|
|
$sendOptions = $this->convertContextOptions($options); |
74
|
|
|
|
75
|
|
|
return ArrayHelper::merge($this->defaultOptions, $dbOptions, $requestOptions, $sendOptions); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Composes stream context options from raw options. |
80
|
|
|
* @param array $options raw options |
81
|
|
|
* @return array stream context options |
82
|
|
|
*/ |
83
|
|
|
protected function convertContextOptions(array $options) |
84
|
|
|
{ |
85
|
|
|
$contextOptions = []; |
86
|
|
|
foreach ($options as $key => $value) { |
87
|
|
|
$section = 'http'; |
88
|
|
|
if (strpos($key, 'ssl') === 0) { |
89
|
|
|
$section = 'ssl'; |
90
|
|
|
$key = substr($key, 3); |
91
|
|
|
} |
92
|
|
|
$key = Inflector::underscore($key); |
93
|
|
|
$contextOptions[$section][$key] = $value; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $contextOptions; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public static function composeHeaderLines($headers) |
100
|
|
|
{ |
101
|
|
|
$result = []; |
102
|
|
|
foreach ($headers as $name => $values) { |
103
|
|
|
$name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $name))); |
104
|
|
|
if (is_string($values)) { |
105
|
|
|
$values = [$values]; |
106
|
|
|
} |
107
|
|
|
foreach ($values as $value) { |
108
|
|
|
$result[] = "$name: $value"; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $result; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public static function isSupported() |
116
|
|
|
{ |
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|