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