1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class ResponseAbstract |
4
|
|
|
* |
5
|
|
|
* @filesource ResponseAbstract.php |
6
|
|
|
* @created 06.04.2016 |
7
|
|
|
* @package Response |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2015 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\TinyCurl\Response; |
14
|
|
|
|
15
|
|
|
use stdClass; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @property mixed body |
19
|
|
|
* @property mixed error |
20
|
|
|
* @property mixed headers |
21
|
|
|
* @property mixed info |
22
|
|
|
* @property mixed json |
23
|
|
|
* @property mixed json_array |
24
|
|
|
*/ |
25
|
|
|
abstract class ResponseAbstract implements ResponseInterface{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var resource |
29
|
|
|
*/ |
30
|
|
|
protected $curl; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \stdClass |
34
|
|
|
*/ |
35
|
|
|
protected $curl_info; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \stdClass |
39
|
|
|
*/ |
40
|
|
|
protected $response_headers; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \stdClass |
44
|
|
|
*/ |
45
|
|
|
protected $response_error; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var mixed |
49
|
|
|
*/ |
50
|
|
|
protected $response_body; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Response constructor. |
54
|
|
|
* |
55
|
|
|
* @param resource $curl |
56
|
|
|
* |
57
|
|
|
* @throws \chillerlan\TinyCurl\Response\ResponseException |
58
|
|
|
*/ |
59
|
|
|
public function __construct($curl){ |
60
|
|
|
|
61
|
|
|
if(!$curl){ |
62
|
|
|
throw new ResponseException('!$curl'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->curl = $curl; |
66
|
|
|
$this->curl_info = new stdClass; |
67
|
|
|
$this->response_error = new stdClass; |
68
|
|
|
$this->response_headers = new stdClass; |
69
|
|
|
|
70
|
|
|
$this->exec(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Farewell. |
75
|
|
|
*/ |
76
|
|
|
public function __destruct(){ |
77
|
|
|
if($this->curl){ |
78
|
|
|
curl_close($this->curl); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $property |
84
|
|
|
* |
85
|
|
|
* @return mixed |
86
|
|
|
* @throws \chillerlan\TinyCurl\Response\ResponseException |
87
|
|
|
*/ |
88
|
|
|
public function __get($property){ |
89
|
|
|
|
90
|
|
|
switch($property){ |
91
|
|
|
case 'body': |
92
|
|
|
return $this->getBody(); |
93
|
|
|
case 'info': |
94
|
|
|
return $this->curl_info; |
95
|
|
|
case 'json': |
96
|
|
|
return json_decode($this->response_body); |
97
|
|
|
case 'json_array': |
98
|
|
|
return json_decode($this->response_body, true); |
99
|
|
|
case 'error': |
100
|
|
|
return $this->response_error; |
101
|
|
|
case 'headers': |
102
|
|
|
return $this->response_headers; |
103
|
|
|
default: |
104
|
|
|
throw new ResponseException('!$property: '.$property); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* executes the cURL call, fills self::$response_body and calls self::getInfo() |
111
|
|
|
*/ |
112
|
|
|
abstract protected function exec(); |
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param resource $curl |
116
|
|
|
* @param string $header_line |
117
|
|
|
* |
118
|
|
|
* @return int |
119
|
|
|
* |
120
|
|
|
* @link http://php.net/manual/function.curl-setopt.php CURLOPT_HEADERFUNCTION |
121
|
|
|
*/ |
122
|
|
|
protected function headerLine(/** @noinspection PhpUnusedParameterInspection */$curl, $header_line){ |
123
|
|
|
|
124
|
|
|
if(substr($header_line, 0, 4) === 'HTTP'){ |
125
|
|
|
$status = explode(' ', $header_line, 3); |
126
|
|
|
|
127
|
|
|
$this->response_headers->httpversion = explode('/', $status[0], 2)[1]; |
128
|
|
|
$this->response_headers->statuscode = intval($status[1]); |
129
|
|
|
$this->response_headers->statustext = trim($status[2]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$h = explode(':', $header_line, 2); |
133
|
|
|
if(count($h) === 2){ |
134
|
|
|
$this->response_headers->{trim(strtolower($h[0]))} = trim($h[1]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return strlen($header_line); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return \stdClass |
142
|
|
|
*/ |
143
|
|
|
protected function getBody(){ |
144
|
|
|
$body = new stdClass; |
145
|
|
|
|
146
|
|
|
$body->content = $this->response_body; |
147
|
|
|
$body->length = strlen($this->response_body); |
148
|
|
|
|
149
|
|
|
if(isset($this->curl_info->content_type) && !empty($this->curl_info->content_type)){ |
150
|
|
|
$body->content_type = $this->curl_info->content_type; |
151
|
|
|
} |
152
|
|
|
// @codeCoverageIgnoreStart |
153
|
|
|
elseif(isset($this->response_headers->content_type) && !empty($this->response_headers->content_type)){ |
154
|
|
|
$body->content_type = $this->response_headers->content_type; |
155
|
|
|
} |
156
|
|
|
// @codeCoverageIgnoreEnd |
157
|
|
|
|
158
|
|
|
return $body; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* |
163
|
|
|
*/ |
164
|
|
|
protected function getInfo(){ |
165
|
|
|
$curl_info = curl_getinfo($this->curl); |
166
|
|
|
if(is_array($curl_info)){ |
167
|
|
|
foreach($curl_info as $key => $value){ |
168
|
|
|
$this->curl_info->{$key} = $value; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$this->response_error->code = curl_errno($this->curl); |
173
|
|
|
$this->response_error->message = curl_error($this->curl); |
174
|
|
|
$this->response_error->version = curl_version(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.