-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathNativeClient.php
More file actions
38 lines (29 loc) · 1.06 KB
/
NativeClient.php
File metadata and controls
38 lines (29 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
namespace App\HttpClient;
use App\HttpClient\Exception\ConnectionError;
class NativeClient implements Client
{
private $options;
public function __construct(?ClientOptions $options = null)
{
$this->options = $options ?? new ClientOptions();
}
public function request(Request $request, ?ClientOptions $options = null): Response
{
$options = $options ?? $this->options;
$streamContext = stream_context_create([
'http' => [
'method' => $request->getMethod(),
'header' => $request->getHeadersAsString(),
'content' => $request->getBody(),
'ignore_errors' => true,
'timeout' => $options->getTimeout(),
],
]);
$responseBody = @file_get_contents($request->getUri(), false, $streamContext);
if ($responseBody === false) {
throw new ConnectionError(error_get_last()['message']);
}
return new Response($responseBody, ...$http_response_header);
}
}