1 | <?php |
||
19 | class ClientStream implements Client |
||
20 | { |
||
21 | private $options = [ |
||
22 | 'headers' => [], |
||
23 | 'timeout' => 5, |
||
24 | 'ignore_errors' => true, |
||
25 | 'protocol_version' => 1.1 |
||
26 | ]; |
||
27 | |||
28 | /** |
||
29 | */ |
||
30 | public $responseHeaders = null; |
||
31 | /** |
||
32 | */ |
||
33 | public $requestHeaders = null; |
||
34 | /** |
||
35 | */ |
||
36 | public $verbs = [ |
||
37 | 'GET' => true, |
||
38 | 'POST' => true, |
||
39 | 'PUT' => true, |
||
40 | 'DELETE' => true, |
||
41 | 'OPTIONS' => true, |
||
42 | 'HEAD' => true, |
||
43 | 'TRACE' => true |
||
44 | ]; |
||
45 | /** |
||
46 | * Merges header string and headers array to single string with all headers |
||
47 | * @return string |
||
48 | */ |
||
49 | 16 | private function mergeHeaders() { |
|
50 | 16 | $args = func_get_args(); |
|
51 | 16 | $result = ''; |
|
52 | 16 | foreach ( $args as $headers ) { |
|
53 | 16 | if (is_array($headers) || $headers instanceof \ArrayObject ) { |
|
54 | 2 | $result .= array_reduce( (array) $headers, function($carry, $entry) { |
|
55 | 2 | return $carry . "\r\n" . $entry; |
|
56 | 2 | }, ''); |
|
57 | } else { |
||
58 | 16 | $result .= (string) $headers; |
|
59 | } |
||
60 | } |
||
61 | 16 | if (substr($result, -2)!="\r\n") { |
|
62 | 16 | $result .= "\r\n"; |
|
63 | } |
||
64 | 16 | return $result; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Send a HTTP request and return the response |
||
69 | * @param string $type The method to use, GET, POST, etc. |
||
70 | * @param string $url The URL to request |
||
71 | * @param array|string $request The query string |
||
72 | * @param array $options Any of the HTTP stream context options, e.g. extra headers. |
||
73 | * @return string |
||
74 | */ |
||
75 | 16 | public function request( $type, $url, $request = null, $options = [] ) |
|
76 | { |
||
77 | 16 | $url = \arc\url::url( (string) $url); |
|
78 | 16 | if ($type == 'GET' && $request) { |
|
79 | 2 | $url->query->import( $request); |
|
80 | 2 | $request = null; |
|
81 | } |
||
82 | $options = [ |
||
83 | 16 | 'method' => $type, |
|
84 | 16 | 'content' => $request |
|
85 | 16 | ] + (array) $options; |
|
86 | |||
87 | 16 | $options['headers'] = $this->mergeHeaders( |
|
88 | 16 | \arc\hash::get('header', $this->options), |
|
89 | 16 | \arc\hash::get('headers', $this->options), |
|
90 | 16 | \arc\hash::get('header', $options), |
|
91 | 16 | \arc\hash::get('headers', $options) |
|
92 | ); |
||
93 | |||
94 | 16 | $options += (array) $this->options; |
|
95 | |||
96 | 16 | $context = stream_context_create( [ 'http' => $options ] ); |
|
97 | 16 | $result = @file_get_contents( (string) $url, false, $context ); |
|
98 | 16 | $this->responseHeaders = isset($http_response_header) ? $http_response_header : null; //magic php variable set by file_get_contents. |
|
99 | 16 | $this->requestHeaders = isset($options['headers']) ? explode("\r\n",$options['headers']) : []; |
|
100 | |||
101 | 16 | return $result; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param array $options Any of the HTTP stream context options, e.g. extra headers. |
||
106 | */ |
||
107 | 20 | public function __construct( $options = [] ) |
|
111 | |||
112 | 14 | public function __call( $name, $args ) |
|
122 | |||
123 | |||
124 | /** |
||
125 | * Adds headers for subsequent requests |
||
126 | * @param mixed $headers The headers to add, either as a string or an array of headers. |
||
127 | * @return $this |
||
128 | */ |
||
129 | 2 | public function headers($headers) |
|
145 | } |
||
146 |
If you suppress an error, we recommend checking for the error condition explicitly: