|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Buzz\Client; |
|
6
|
|
|
|
|
7
|
|
|
use Buzz\Configuration\ParameterBag; |
|
8
|
|
|
use Buzz\Exception\NetworkException; |
|
9
|
|
|
use Buzz\Message\HeaderConverter; |
|
10
|
|
|
use Buzz\Message\ResponseBuilder; |
|
11
|
|
|
use Psr\Http\Message\RequestInterface; |
|
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
13
|
|
|
|
|
14
|
|
|
class FileGetContents extends AbstractClient implements BuzzClientInterface |
|
15
|
|
|
{ |
|
16
|
118 |
|
public function sendRequest(RequestInterface $request, array $options = []): ResponseInterface |
|
17
|
|
|
{ |
|
18
|
118 |
|
$options = $this->validateOptions($options); |
|
19
|
117 |
|
$context = stream_context_create($this->getStreamContextArray($request, $options)); |
|
20
|
|
|
|
|
21
|
117 |
|
$level = error_reporting(0); |
|
22
|
117 |
|
$content = file_get_contents($request->getUri()->__toString(), false, $context); |
|
23
|
117 |
|
error_reporting($level); |
|
24
|
117 |
|
if (false === $content) { |
|
25
|
5 |
|
if ($error = error_get_last()) { |
|
26
|
5 |
|
throw new NetworkException($request, $error['message']); |
|
27
|
|
|
} |
|
28
|
|
|
throw new NetworkException($request, 'Failed to get contents from '.$request->getUri()->__toString()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
112 |
|
$requestBuilder = new ResponseBuilder($this->responseFactory); |
|
32
|
112 |
|
$requestBuilder->parseHttpHeaders($this->filterHeaders((array) $http_response_header)); |
|
33
|
112 |
|
$requestBuilder->writeBody($content); |
|
34
|
|
|
|
|
35
|
112 |
|
return $requestBuilder->getResponse(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Converts a request into an array for stream_context_create(). |
|
40
|
|
|
* |
|
41
|
|
|
* @param RequestInterface $request A request object |
|
42
|
|
|
* |
|
43
|
|
|
* @return array An array for stream_context_create() |
|
44
|
|
|
*/ |
|
45
|
118 |
|
protected function getStreamContextArray(RequestInterface $request, ParameterBag $options): array |
|
46
|
|
|
{ |
|
47
|
118 |
|
$headers = $request->getHeaders(); |
|
48
|
118 |
|
unset($headers['Host']); |
|
49
|
|
|
$context = [ |
|
50
|
|
|
'http' => [ |
|
51
|
|
|
// values from the request |
|
52
|
118 |
|
'method' => $request->getMethod(), |
|
53
|
118 |
|
'header' => implode("\r\n", HeaderConverter::toBuzzHeaders($headers)), |
|
54
|
118 |
|
'content' => $request->getBody()->__toString(), |
|
55
|
118 |
|
'protocol_version' => $request->getProtocolVersion(), |
|
56
|
|
|
|
|
57
|
|
|
// values from the current client |
|
58
|
|
|
'ignore_errors' => true, |
|
59
|
118 |
|
'follow_location' => $options->get('allow_redirects') && $options->get('max_redirects') > 0, |
|
60
|
118 |
|
'max_redirects' => $options->get('max_redirects') + 1, |
|
61
|
|
|
], |
|
62
|
|
|
'ssl' => [ |
|
63
|
118 |
|
'verify_peer' => $options->get('verify'), |
|
64
|
118 |
|
'verify_host' => $options->get('verify'), |
|
65
|
|
|
], |
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
118 |
|
if (0 < $options->get('timeout')) { |
|
69
|
5 |
|
$context['http']['timeout'] = $options->get('timeout'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
118 |
|
if (null !== $options->get('proxy')) { |
|
73
|
|
|
$context['http']['proxy'] = $options->get('proxy'); |
|
74
|
|
|
$context['http']['request_fulluri'] = true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
118 |
|
return $context; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
112 |
|
private function filterHeaders(array $headers): array |
|
81
|
|
|
{ |
|
82
|
112 |
|
$filtered = []; |
|
83
|
112 |
|
foreach ($headers as $header) { |
|
84
|
112 |
|
if (0 === stripos($header, 'http/')) { |
|
85
|
112 |
|
$filtered = []; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
112 |
|
$filtered[] = $header; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
112 |
|
return $filtered; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|