1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rogierw\RwAcme\Http; |
4
|
|
|
|
5
|
|
|
class Client |
6
|
|
|
{ |
7
|
|
|
/** @var string */ |
8
|
|
|
private $baseUrl; |
9
|
|
|
|
10
|
|
|
/** @var int */ |
11
|
|
|
private $timeout; |
12
|
|
|
|
13
|
|
|
public function __construct(string $baseUrl, int $timeout = 10) |
14
|
|
|
{ |
15
|
|
|
$this->baseUrl = $baseUrl; |
16
|
|
|
$this->timeout = $timeout; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function head(string $url) |
20
|
|
|
{ |
21
|
|
|
return $this->makeCurlRequest('head', $url); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function get(string $url, array $headers = [], array $arguments = []) |
25
|
|
|
{ |
26
|
|
|
return $this->makeCurlRequest('get', $url, $headers, $arguments); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function post(string $url, array $payload = [], array $headers = []) |
30
|
|
|
{ |
31
|
|
|
$headers = array_merge(['Content-Type: application/jose+json'], $headers); |
32
|
|
|
|
33
|
|
|
return $this->makeCurlRequest('post', $url, $headers, $payload); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function makeCurlRequest(string $httpVerb, string $fullUrl, array $headers = [], array $payload = []): Response |
37
|
|
|
{ |
38
|
|
|
$headers = array_merge([ |
39
|
|
|
'Content-Type: ' . ($httpVerb === 'post') ? 'application/jose+json' : 'application/json', |
40
|
|
|
], $headers); |
41
|
|
|
|
42
|
|
|
$curlHandle = $this->getCurlHandle($fullUrl, $headers); |
43
|
|
|
|
44
|
|
|
switch ($httpVerb) { |
45
|
|
|
case 'head': |
46
|
|
|
curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'HEAD'); |
|
|
|
|
47
|
|
|
curl_setopt($curlHandle, CURLOPT_NOBODY, true); |
48
|
|
|
break; |
49
|
|
|
|
50
|
|
|
case 'get': |
51
|
|
|
curl_setopt($curlHandle, CURLOPT_URL, $fullUrl . '?' . http_build_query($payload)); |
52
|
|
|
break; |
53
|
|
|
|
54
|
|
|
case 'post': |
55
|
|
|
curl_setopt($curlHandle, CURLOPT_POST, true); |
56
|
|
|
$this->attachRequestPayload($curlHandle, $payload); |
57
|
|
|
break; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$rawResponse = curl_exec($curlHandle); |
61
|
|
|
$headerSize = curl_getinfo($curlHandle, CURLINFO_HEADER_SIZE); |
62
|
|
|
$headers = curl_getinfo($curlHandle); |
63
|
|
|
$error = curl_error($curlHandle); |
64
|
|
|
|
65
|
|
|
$rawHeaders = mb_substr($rawResponse, 0, $headerSize); |
66
|
|
|
$rawBody = mb_substr($rawResponse, $headerSize); |
67
|
|
|
$body = $rawBody; |
68
|
|
|
|
69
|
|
|
if ($headers['content_type'] === 'application/json') { |
70
|
|
|
$body = json_decode($rawBody, true); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return new Response($rawHeaders, $headers, $body, $error); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function attachRequestPayload(&$curlHandle, array $data) |
77
|
|
|
{ |
78
|
|
|
$encoded = json_encode($data); |
79
|
|
|
|
80
|
|
|
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $encoded); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function getCurlHandle(string $fullUrl, array $headers = []) |
84
|
|
|
{ |
85
|
|
|
$curlHandle = curl_init(); |
86
|
|
|
|
87
|
|
|
curl_setopt($curlHandle, CURLOPT_URL, $fullUrl); |
|
|
|
|
88
|
|
|
|
89
|
|
|
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array_merge([ |
90
|
|
|
'Accept: application/json', |
91
|
|
|
], $headers)); |
92
|
|
|
|
93
|
|
|
curl_setopt($curlHandle, CURLOPT_USERAGENT, 'rogierw/rw-acme-client'); |
94
|
|
|
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); |
95
|
|
|
curl_setopt($curlHandle, CURLOPT_TIMEOUT, $this->timeout); |
96
|
|
|
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, true); |
97
|
|
|
curl_setopt($curlHandle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); |
98
|
|
|
curl_setopt($curlHandle, CURLOPT_ENCODING, ''); |
99
|
|
|
curl_setopt($curlHandle, CURLOPT_HEADER, true); |
100
|
|
|
|
101
|
|
|
return $curlHandle; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|