|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
4
|
|
|
* file that was distributed with this source code. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Nikita Vershinin <[email protected]> |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace OpenStackStorage; |
|
10
|
|
|
|
|
11
|
|
|
use Resty\Resty; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* HTTP-request client object. |
|
15
|
|
|
*/ |
|
16
|
|
|
class Client extends Resty |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
const GET = 'GET'; |
|
20
|
|
|
const PUT = 'PUT'; |
|
21
|
|
|
const POST = 'POST'; |
|
22
|
|
|
const DELETE = 'DELETE'; |
|
23
|
|
|
const HEAD = 'HEAD'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Request timeout. |
|
27
|
|
|
* |
|
28
|
|
|
* @var integer |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $timeout = 240; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Class constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $opts |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct($opts = null) |
|
38
|
|
|
{ |
|
39
|
|
|
parent::__construct($opts); |
|
40
|
|
|
|
|
41
|
|
|
if (is_array($opts) && array_key_exists('timeout', $opts)) { |
|
42
|
|
|
$this->timeout = intval($opts['timeout']); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $url |
|
50
|
|
|
* @param string $method |
|
51
|
|
|
* @param array $querydata |
|
52
|
|
|
* @param array $headers |
|
53
|
|
|
* @param array $options |
|
54
|
|
|
* @return array |
|
55
|
|
|
* @throws \OpenStackStorage\Exceptions\ResponseError |
|
56
|
|
|
* @throws \OpenStackStorage\Exceptions\Error |
|
57
|
|
|
*/ |
|
58
|
|
|
public function sendRequest( |
|
59
|
|
|
$url, |
|
60
|
|
|
$method = 'GET', |
|
61
|
|
|
$querydata = null, |
|
62
|
|
|
$headers = null, |
|
63
|
|
|
$options = null |
|
64
|
|
|
) { |
|
65
|
|
|
if (null === $headers) { |
|
66
|
|
|
$headers = array(); |
|
67
|
|
|
} elseif (!is_array($headers)) { |
|
68
|
|
|
$headers = (array) $headers; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (null === $options) { |
|
72
|
|
|
$options = array(); |
|
73
|
|
|
} elseif (!is_array($options)) { |
|
74
|
|
|
$options = (array) $options; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if (!array_key_exists('timeout', $options)) { |
|
78
|
|
|
$options['timeout'] = $this->timeout; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ($method == self::PUT && !array_key_exists('Content-Length', $headers)) { |
|
82
|
|
|
if (empty($querydata)) { |
|
83
|
|
|
$headers['Content-Length'] = 0; |
|
84
|
|
|
} else { |
|
85
|
|
|
$headers['Content-Length'] = is_string($querydata) |
|
86
|
|
|
? strlen($querydata) |
|
87
|
|
|
: strlen(http_build_query($querydata)); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$response = parent::sendRequest($url, $method, $querydata, $headers, $options); |
|
92
|
|
|
|
|
93
|
|
|
if (array_key_exists('error_msg', $response) && (null !== $response['error_msg'])) { |
|
94
|
|
|
throw new Exceptions\Error(substr($response['error_msg'], 0, strpos($response['error_msg'], ';'))); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if ($response['status'] >= 400) { |
|
98
|
|
|
throw new Exceptions\ResponseError($response['status']); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$response['headers'] = new Client\Headers($response['headers']); |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
return $response; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
* Overridden method to decode JSON to array instead of stdClass. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $resp |
|
111
|
|
|
* @return object|string |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function processResponseBody($resp) |
|
114
|
|
|
{ |
|
115
|
|
|
if ($this->parse_body === true) { |
|
116
|
|
|
if (isset($resp['headers']['Content-Type'])) { |
|
117
|
|
|
$contentType = preg_split('/[;\s]+/', $resp['headers']['Content-Type']); |
|
118
|
|
|
$contentType = $contentType[0]; |
|
119
|
|
|
} else { |
|
120
|
|
|
$contentType = null; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if ((null !== $contentType) && !empty($contentType)) { |
|
124
|
|
|
if (in_array($contentType, self::$JSON_TYPES) || strpos($contentType, '+json') !== false) { |
|
125
|
|
|
$this->log('Response body is JSON'); |
|
126
|
|
|
|
|
127
|
|
|
$resp['body_raw'] = $resp['body']; |
|
128
|
|
|
$resp['body'] = json_decode($resp['body'], true); |
|
129
|
|
|
|
|
130
|
|
|
return $resp; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
parent::processResponseBody($resp); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$this->log('Response body not parsed'); |
|
138
|
|
|
|
|
139
|
|
|
return $resp; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: