1
|
|
|
<?php |
2
|
|
|
namespace QiniuRtc; |
3
|
|
|
|
4
|
|
|
class HttpResponse |
5
|
|
|
{ |
6
|
|
|
private $code; |
7
|
|
|
private $raw_body; |
8
|
|
|
private $body; |
9
|
|
|
private $headers; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @param int $code response code of the cURL request |
13
|
|
|
* @param string $raw_body the raw body of the cURL response |
14
|
|
|
* @param string $headers raw header string from cURL response |
15
|
|
|
*/ |
16
|
|
|
public function __construct($code, $raw_body, $headers) |
17
|
|
|
{ |
18
|
|
|
$this->code = $code; |
19
|
|
|
$this->headers = $this->get_headers_from_curl_response($headers); |
20
|
|
|
$this->raw_body = $raw_body; |
21
|
|
|
$this->body = $raw_body; |
22
|
|
|
$json = json_decode($raw_body, true); |
23
|
|
|
if (json_last_error() == JSON_ERROR_NONE) { |
24
|
|
|
$this->body = $json; |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Return a property of the response if it exists. |
30
|
|
|
* Possibilities include: code, raw_body, headers, body (if the response is json-decodable) |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
|
|
public function __get($property) |
34
|
|
|
{ |
35
|
|
|
if (property_exists($this, $property)) { |
36
|
|
|
return $this->$property; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set the properties of this object |
42
|
|
|
* @param string $property the property name |
43
|
|
|
* @param mixed $value the property value |
44
|
|
|
*/ |
45
|
|
|
public function __set($property, $value) |
46
|
|
|
{ |
47
|
|
|
if (property_exists($this, $property)) { |
48
|
|
|
$this->$property = $value; |
49
|
|
|
} |
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retrieve the cURL response headers from the |
55
|
|
|
* header string and convert it into an array |
56
|
|
|
* @param string $headers header string from cURL response |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
private function get_headers_from_curl_response($headers) |
60
|
|
|
{ |
61
|
|
|
$headers = explode("\r\n", $headers); |
62
|
|
|
array_shift($headers); |
63
|
|
|
foreach ($headers as $line) { |
64
|
|
|
if (strstr($line, ': ')) { |
65
|
|
|
list($key, $value) = explode(': ', $line); |
66
|
|
|
$result[$key] = $value; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
return $result; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.