|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EasyHttp\Model; |
|
4
|
|
|
|
|
5
|
|
|
use EasyHttp\Enums\CurlInfo; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class HttpResponse |
|
9
|
|
|
* |
|
10
|
|
|
* @link https://github.com/shahradelahi/easy-http |
|
11
|
|
|
* @author Shahrad Elahi (https://github.com/shahradelahi) |
|
12
|
|
|
* @license https://github.com/shahradelahi/easy-http/blob/master/LICENSE (MIT License) |
|
13
|
|
|
* |
|
14
|
|
|
* |
|
15
|
|
|
* @method getBody() This method returns the body of the response. |
|
16
|
|
|
* @method setBody($body) This method sets the body of the response. |
|
17
|
|
|
* @method setHeaderSize($size) This method sets the header size of the response. |
|
18
|
|
|
* @method getHeaderSize() This method returns the header size of the response. |
|
19
|
|
|
* @method getHeaders() This method returns the headers of the response. |
|
20
|
|
|
* @method getStatusCode() This method returns the status code of the response. |
|
21
|
|
|
* @method setStatusCode($code) This method sets the status code of the response. |
|
22
|
|
|
* @method getError() This method returns the error of the response. |
|
23
|
|
|
* @method setError($error) This method sets the error of the response. |
|
24
|
|
|
* @method getErrorCode() This method returns the error code of the response. |
|
25
|
|
|
* @method setErrorCode($code) This method sets the error code of the response. |
|
26
|
|
|
*/ |
|
27
|
|
|
class HttpResponse |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The Handler |
|
32
|
|
|
* |
|
33
|
|
|
* @var \CurlHandle |
|
34
|
|
|
*/ |
|
35
|
|
|
private \CurlHandle $curlHandle; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var int |
|
39
|
|
|
*/ |
|
40
|
|
|
private int $status; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var int |
|
44
|
|
|
*/ |
|
45
|
|
|
private int $headerSize; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
private array $headers; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var ?string |
|
54
|
|
|
*/ |
|
55
|
|
|
private ?string $body; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var mixed |
|
59
|
|
|
*/ |
|
60
|
|
|
private mixed $info; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
private string $error; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* The Error Code |
|
69
|
|
|
* |
|
70
|
|
|
* @var int |
|
71
|
|
|
*/ |
|
72
|
|
|
private int $errorCode; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Set the curl handle |
|
76
|
|
|
* |
|
77
|
|
|
* @param \CurlHandle $curlHandle |
|
78
|
|
|
* @return HttpResponse |
|
79
|
|
|
*/ |
|
80
|
|
|
public function setCurlHandle(\CurlHandle $curlHandle): HttpResponse |
|
81
|
|
|
{ |
|
82
|
|
|
$this->curlHandle = $curlHandle; |
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get info from the curl handle |
|
88
|
|
|
* |
|
89
|
|
|
* @return mixed |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getCurlInfo(): CurlInfo |
|
92
|
|
|
{ |
|
93
|
|
|
if (empty($this->curlHandle)) return false; |
|
|
|
|
|
|
94
|
|
|
return new CurlInfo(curl_getinfo($this->curlHandle)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set the headers of the response |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $headers |
|
101
|
|
|
* @return HttpResponse |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setHeaders(string $headers): HttpResponse |
|
104
|
|
|
{ |
|
105
|
|
|
$result = []; |
|
106
|
|
|
$lines = explode("\r\n", $headers); |
|
107
|
|
|
foreach ($lines as $line) { |
|
108
|
|
|
if (str_contains($line, ':')) { |
|
109
|
|
|
$parts = explode(':', $line); |
|
110
|
|
|
$result[trim($parts[0])] = trim($parts[1]); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
$this->headers = $result; |
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get a key from the response headers |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $key |
|
121
|
|
|
* @return mixed |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getHeaderLine(string $key): mixed |
|
124
|
|
|
{ |
|
125
|
|
|
return array_change_key_case($this->headers, CASE_LOWER)[strtolower($key)] ?? null; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string $name |
|
130
|
|
|
* @param array $arguments |
|
131
|
|
|
* @return mixed |
|
132
|
|
|
*/ |
|
133
|
|
|
public function __call(string $name, array $arguments): mixed |
|
134
|
|
|
{ |
|
135
|
|
|
if (method_exists($this, $name)) { |
|
136
|
|
|
return $this->{$name}(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if (str_starts_with($name, 'get')) { |
|
140
|
|
|
$property = lcfirst(substr($name, 3)); |
|
141
|
|
|
return $this->{$property} ?? null; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if (str_starts_with($name, 'set')) { |
|
145
|
|
|
$property = lcfirst(substr($name, 3)); |
|
146
|
|
|
$this->{$property} = $arguments[0] ?? null; |
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
throw new \BadMethodCallException("Method $name does not exist"); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
} |