103 |
- |
1 |
<?php
|
|
|
2 |
namespace GuzzleHttp;
|
|
|
3 |
|
|
|
4 |
use GuzzleHttp\Handler\CurlHandler;
|
|
|
5 |
use GuzzleHttp\Handler\CurlMultiHandler;
|
|
|
6 |
use GuzzleHttp\Handler\Proxy;
|
|
|
7 |
use GuzzleHttp\Handler\StreamHandler;
|
|
|
8 |
|
|
|
9 |
/**
|
|
|
10 |
* Expands a URI template
|
|
|
11 |
*
|
|
|
12 |
* @param string $template URI template
|
|
|
13 |
* @param array $variables Template variables
|
|
|
14 |
*
|
|
|
15 |
* @return string
|
|
|
16 |
*/
|
|
|
17 |
function uri_template($template, array $variables)
|
|
|
18 |
{
|
|
|
19 |
if (extension_loaded('uri_template')) {
|
|
|
20 |
// @codeCoverageIgnoreStart
|
|
|
21 |
return \uri_template($template, $variables);
|
|
|
22 |
// @codeCoverageIgnoreEnd
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
static $uriTemplate;
|
|
|
26 |
if (!$uriTemplate) {
|
|
|
27 |
$uriTemplate = new UriTemplate();
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
return $uriTemplate->expand($template, $variables);
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Debug function used to describe the provided value type and class.
|
|
|
35 |
*
|
|
|
36 |
* @param mixed $input
|
|
|
37 |
*
|
|
|
38 |
* @return string Returns a string containing the type of the variable and
|
|
|
39 |
* if a class is provided, the class name.
|
|
|
40 |
*/
|
|
|
41 |
function describe_type($input)
|
|
|
42 |
{
|
|
|
43 |
switch (gettype($input)) {
|
|
|
44 |
case 'object':
|
|
|
45 |
return 'object(' . get_class($input) . ')';
|
|
|
46 |
case 'array':
|
|
|
47 |
return 'array(' . count($input) . ')';
|
|
|
48 |
default:
|
|
|
49 |
ob_start();
|
|
|
50 |
var_dump($input);
|
|
|
51 |
// normalize float vs double
|
|
|
52 |
return str_replace('double(', 'float(', rtrim(ob_get_clean()));
|
|
|
53 |
}
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Parses an array of header lines into an associative array of headers.
|
|
|
58 |
*
|
|
|
59 |
* @param array $lines Header lines array of strings in the following
|
|
|
60 |
* format: "Name: Value"
|
|
|
61 |
* @return array
|
|
|
62 |
*/
|
|
|
63 |
function headers_from_lines($lines)
|
|
|
64 |
{
|
|
|
65 |
$headers = [];
|
|
|
66 |
|
|
|
67 |
foreach ($lines as $line) {
|
|
|
68 |
$parts = explode(':', $line, 2);
|
|
|
69 |
$headers[trim($parts[0])][] = isset($parts[1])
|
|
|
70 |
? trim($parts[1])
|
|
|
71 |
: null;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
return $headers;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Returns a debug stream based on the provided variable.
|
|
|
79 |
*
|
|
|
80 |
* @param mixed $value Optional value
|
|
|
81 |
*
|
|
|
82 |
* @return resource
|
|
|
83 |
*/
|
|
|
84 |
function debug_resource($value = null)
|
|
|
85 |
{
|
|
|
86 |
if (is_resource($value)) {
|
|
|
87 |
return $value;
|
|
|
88 |
} elseif (defined('STDOUT')) {
|
|
|
89 |
return STDOUT;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
return fopen('php://output', 'w');
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Chooses and creates a default handler to use based on the environment.
|
|
|
97 |
*
|
|
|
98 |
* The returned handler is not wrapped by any default middlewares.
|
|
|
99 |
*
|
|
|
100 |
* @throws \RuntimeException if no viable Handler is available.
|
|
|
101 |
* @return callable Returns the best handler for the given system.
|
|
|
102 |
*/
|
|
|
103 |
function choose_handler()
|
|
|
104 |
{
|
|
|
105 |
$handler = null;
|
|
|
106 |
if (function_exists('curl_multi_exec') && function_exists('curl_exec')) {
|
|
|
107 |
$handler = Proxy::wrapSync(new CurlMultiHandler(), new CurlHandler());
|
|
|
108 |
} elseif (function_exists('curl_exec')) {
|
|
|
109 |
$handler = new CurlHandler();
|
|
|
110 |
} elseif (function_exists('curl_multi_exec')) {
|
|
|
111 |
$handler = new CurlMultiHandler();
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
if (ini_get('allow_url_fopen')) {
|
|
|
115 |
$handler = $handler
|
|
|
116 |
? Proxy::wrapStreaming($handler, new StreamHandler())
|
|
|
117 |
: new StreamHandler();
|
|
|
118 |
} elseif (!$handler) {
|
|
|
119 |
throw new \RuntimeException('GuzzleHttp requires cURL, the '
|
|
|
120 |
. 'allow_url_fopen ini setting, or a custom HTTP handler.');
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
return $handler;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Get the default User-Agent string to use with Guzzle
|
|
|
128 |
*
|
|
|
129 |
* @return string
|
|
|
130 |
*/
|
|
|
131 |
function default_user_agent()
|
|
|
132 |
{
|
|
|
133 |
static $defaultAgent = '';
|
|
|
134 |
|
|
|
135 |
if (!$defaultAgent) {
|
|
|
136 |
$defaultAgent = 'GuzzleHttp/' . Client::VERSION;
|
|
|
137 |
if (extension_loaded('curl') && function_exists('curl_version')) {
|
|
|
138 |
$defaultAgent .= ' curl/' . \curl_version()['version'];
|
|
|
139 |
}
|
|
|
140 |
$defaultAgent .= ' PHP/' . PHP_VERSION;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
return $defaultAgent;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Returns the default cacert bundle for the current system.
|
|
|
148 |
*
|
|
|
149 |
* First, the openssl.cafile and curl.cainfo php.ini settings are checked.
|
|
|
150 |
* If those settings are not configured, then the common locations for
|
|
|
151 |
* bundles found on Red Hat, CentOS, Fedora, Ubuntu, Debian, FreeBSD, OS X
|
|
|
152 |
* and Windows are checked. If any of these file locations are found on
|
|
|
153 |
* disk, they will be utilized.
|
|
|
154 |
*
|
|
|
155 |
* Note: the result of this function is cached for subsequent calls.
|
|
|
156 |
*
|
|
|
157 |
* @return string
|
|
|
158 |
* @throws \RuntimeException if no bundle can be found.
|
|
|
159 |
*/
|
|
|
160 |
function default_ca_bundle()
|
|
|
161 |
{
|
|
|
162 |
static $cached = null;
|
|
|
163 |
static $cafiles = [
|
|
|
164 |
// Red Hat, CentOS, Fedora (provided by the ca-certificates package)
|
|
|
165 |
'/etc/pki/tls/certs/ca-bundle.crt',
|
|
|
166 |
// Ubuntu, Debian (provided by the ca-certificates package)
|
|
|
167 |
'/etc/ssl/certs/ca-certificates.crt',
|
|
|
168 |
// FreeBSD (provided by the ca_root_nss package)
|
|
|
169 |
'/usr/local/share/certs/ca-root-nss.crt',
|
|
|
170 |
// OS X provided by homebrew (using the default path)
|
|
|
171 |
'/usr/local/etc/openssl/cert.pem',
|
|
|
172 |
// Google app engine
|
|
|
173 |
'/etc/ca-certificates.crt',
|
|
|
174 |
// Windows?
|
|
|
175 |
'C:\\windows\\system32\\curl-ca-bundle.crt',
|
|
|
176 |
'C:\\windows\\curl-ca-bundle.crt',
|
|
|
177 |
];
|
|
|
178 |
|
|
|
179 |
if ($cached) {
|
|
|
180 |
return $cached;
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
if ($ca = ini_get('openssl.cafile')) {
|
|
|
184 |
return $cached = $ca;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
if ($ca = ini_get('curl.cainfo')) {
|
|
|
188 |
return $cached = $ca;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
foreach ($cafiles as $filename) {
|
|
|
192 |
if (file_exists($filename)) {
|
|
|
193 |
return $cached = $filename;
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
throw new \RuntimeException(<<< EOT
|
|
|
198 |
No system CA bundle could be found in any of the the common system locations.
|
|
|
199 |
PHP versions earlier than 5.6 are not properly configured to use the system's
|
|
|
200 |
CA bundle by default. In order to verify peer certificates, you will need to
|
|
|
201 |
supply the path on disk to a certificate bundle to the 'verify' request
|
|
|
202 |
option: http://docs.guzzlephp.org/en/latest/clients.html#verify. If you do not
|
|
|
203 |
need a specific certificate bundle, then Mozilla provides a commonly used CA
|
|
|
204 |
bundle which can be downloaded here (provided by the maintainer of cURL):
|
|
|
205 |
https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt. Once
|
|
|
206 |
you have a CA bundle available on disk, you can set the 'openssl.cafile' PHP
|
|
|
207 |
ini setting to point to the path to the file, allowing you to omit the 'verify'
|
|
|
208 |
request option. See http://curl.haxx.se/docs/sslcerts.html for more
|
|
|
209 |
information.
|
|
|
210 |
EOT
|
|
|
211 |
);
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
/**
|
|
|
215 |
* Creates an associative array of lowercase header names to the actual
|
|
|
216 |
* header casing.
|
|
|
217 |
*
|
|
|
218 |
* @param array $headers
|
|
|
219 |
*
|
|
|
220 |
* @return array
|
|
|
221 |
*/
|
|
|
222 |
function normalize_header_keys(array $headers)
|
|
|
223 |
{
|
|
|
224 |
$result = [];
|
|
|
225 |
foreach (array_keys($headers) as $key) {
|
|
|
226 |
$result[strtolower($key)] = $key;
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
return $result;
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
/**
|
|
|
233 |
* Returns true if the provided host matches any of the no proxy areas.
|
|
|
234 |
*
|
|
|
235 |
* This method will strip a port from the host if it is present. Each pattern
|
|
|
236 |
* can be matched with an exact match (e.g., "foo.com" == "foo.com") or a
|
|
|
237 |
* partial match: (e.g., "foo.com" == "baz.foo.com" and ".foo.com" ==
|
|
|
238 |
* "baz.foo.com", but ".foo.com" != "foo.com").
|
|
|
239 |
*
|
|
|
240 |
* Areas are matched in the following cases:
|
|
|
241 |
* 1. "*" (without quotes) always matches any hosts.
|
|
|
242 |
* 2. An exact match.
|
|
|
243 |
* 3. The area starts with "." and the area is the last part of the host. e.g.
|
|
|
244 |
* '.mit.edu' will match any host that ends with '.mit.edu'.
|
|
|
245 |
*
|
|
|
246 |
* @param string $host Host to check against the patterns.
|
|
|
247 |
* @param array $noProxyArray An array of host patterns.
|
|
|
248 |
*
|
|
|
249 |
* @return bool
|
|
|
250 |
*/
|
|
|
251 |
function is_host_in_noproxy($host, array $noProxyArray)
|
|
|
252 |
{
|
|
|
253 |
if (strlen($host) === 0) {
|
|
|
254 |
throw new \InvalidArgumentException('Empty host provided');
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
// Strip port if present.
|
|
|
258 |
if (strpos($host, ':')) {
|
|
|
259 |
$host = explode($host, ':', 2)[0];
|
|
|
260 |
}
|
|
|
261 |
|
|
|
262 |
foreach ($noProxyArray as $area) {
|
|
|
263 |
// Always match on wildcards.
|
|
|
264 |
if ($area === '*') {
|
|
|
265 |
return true;
|
|
|
266 |
} elseif (empty($area)) {
|
|
|
267 |
// Don't match on empty values.
|
|
|
268 |
continue;
|
|
|
269 |
} elseif ($area === $host) {
|
|
|
270 |
// Exact matches.
|
|
|
271 |
return true;
|
|
|
272 |
} else {
|
|
|
273 |
// Special match if the area when prefixed with ".". Remove any
|
|
|
274 |
// existing leading "." and add a new leading ".".
|
|
|
275 |
$area = '.' . ltrim($area, '.');
|
|
|
276 |
if (substr($host, -(strlen($area))) === $area) {
|
|
|
277 |
return true;
|
|
|
278 |
}
|
|
|
279 |
}
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
return false;
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
/**
|
|
|
286 |
* Wrapper for json_decode that throws when an error occurs.
|
|
|
287 |
*
|
|
|
288 |
* @param string $json JSON data to parse
|
|
|
289 |
* @param bool $assoc When true, returned objects will be converted
|
|
|
290 |
* into associative arrays.
|
|
|
291 |
* @param int $depth User specified recursion depth.
|
|
|
292 |
* @param int $options Bitmask of JSON decode options.
|
|
|
293 |
*
|
|
|
294 |
* @return mixed
|
|
|
295 |
* @throws \InvalidArgumentException if the JSON cannot be decoded.
|
|
|
296 |
* @link http://www.php.net/manual/en/function.json-decode.php
|
|
|
297 |
*/
|
|
|
298 |
function json_decode($json, $assoc = false, $depth = 512, $options = 0)
|
|
|
299 |
{
|
|
|
300 |
$data = \json_decode($json, $assoc, $depth, $options);
|
|
|
301 |
if (JSON_ERROR_NONE !== json_last_error()) {
|
|
|
302 |
throw new \InvalidArgumentException(
|
|
|
303 |
'json_decode error: ' . json_last_error_msg());
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
return $data;
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
/**
|
|
|
310 |
* Wrapper for JSON encoding that throws when an error occurs.
|
|
|
311 |
*
|
|
|
312 |
* @param mixed $value The value being encoded
|
|
|
313 |
* @param int $options JSON encode option bitmask
|
|
|
314 |
* @param int $depth Set the maximum depth. Must be greater than zero.
|
|
|
315 |
*
|
|
|
316 |
* @return string
|
|
|
317 |
* @throws \InvalidArgumentException if the JSON cannot be encoded.
|
|
|
318 |
* @link http://www.php.net/manual/en/function.json-encode.php
|
|
|
319 |
*/
|
|
|
320 |
function json_encode($value, $options = 0, $depth = 512)
|
|
|
321 |
{
|
|
|
322 |
$json = \json_encode($value, $options, $depth);
|
|
|
323 |
if (JSON_ERROR_NONE !== json_last_error()) {
|
|
|
324 |
throw new \InvalidArgumentException(
|
|
|
325 |
'json_encode error: ' . json_last_error_msg());
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
return $json;
|
|
|
329 |
}
|