1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class ResponseAbstract |
4
|
|
|
* |
5
|
|
|
* @filesource ResponseAbstract.php |
6
|
|
|
* @created 06.04.2016 |
7
|
|
|
* @package chillerlan\TinyCurl |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2015 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\TinyCurl; |
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\ResponseException |
58
|
|
|
*/ |
59
|
|
|
public function __construct($curl){ |
60
|
|
|
|
61
|
|
|
if(!is_resource($curl)){ |
62
|
|
|
throw new ResponseException('no cURL handle given'); |
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
|
|
|
* @param string $property |
75
|
|
|
* |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
|
|
public function __get($property){ |
79
|
|
|
|
80
|
|
|
switch($property){ |
81
|
|
|
case 'body': |
82
|
|
|
return $this->getBody(); |
83
|
|
|
case 'info': |
84
|
|
|
return $this->curl_info; |
85
|
|
|
case 'json': |
86
|
|
|
return json_decode($this->response_body); |
87
|
|
|
case 'json_array': |
88
|
|
|
return json_decode($this->response_body, true); |
89
|
|
|
case 'error': |
90
|
|
|
return $this->response_error; |
91
|
|
|
case 'headers': |
92
|
|
|
return $this->response_headers; |
93
|
|
|
default: |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* executes the cURL call, fills self::$response_body and calls self::getInfo() |
101
|
|
|
*/ |
102
|
|
|
abstract protected function exec(); |
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param resource $curl |
106
|
|
|
* @param string $header_line |
107
|
|
|
* |
108
|
|
|
* @return int |
109
|
|
|
* |
110
|
|
|
* @link http://php.net/manual/function.curl-setopt.php CURLOPT_HEADERFUNCTION |
111
|
|
|
*/ |
112
|
|
|
protected function headerLine(/** @noinspection PhpUnusedParameterInspection */$curl, $header_line){ |
113
|
|
|
$header = explode(':', $header_line, 2); |
114
|
|
|
|
115
|
|
|
if(count($header) === 2){ |
116
|
|
|
$this->response_headers->{trim(strtolower($header[0]))} = trim($header[1]); |
117
|
|
|
} |
118
|
|
|
elseif(substr($header_line, 0, 4) === 'HTTP'){ |
119
|
|
|
$status = explode(' ', $header_line, 3); |
120
|
|
|
|
121
|
|
|
$this->response_headers->httpversion = explode('/', $status[0], 2)[1]; |
122
|
|
|
$this->response_headers->statuscode = intval($status[1]); |
123
|
|
|
$this->response_headers->statustext = trim($status[2]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return strlen($header_line); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return \stdClass |
131
|
|
|
*/ |
132
|
|
|
protected function getBody(){ |
133
|
|
|
$body = new stdClass; |
134
|
|
|
|
135
|
|
|
$body->content = $this->response_body; |
136
|
|
|
$body->length = strlen($this->response_body); |
137
|
|
|
|
138
|
|
|
if(isset($this->curl_info->content_type) && !empty($this->curl_info->content_type)){ |
139
|
|
|
$body->content_type = $this->curl_info->content_type; |
140
|
|
|
} |
141
|
|
|
// @codeCoverageIgnoreStart |
142
|
|
|
elseif(isset($this->response_headers->content_type) && !empty($this->response_headers->content_type)){ |
143
|
|
|
$body->content_type = $this->response_headers->content_type; |
144
|
|
|
} |
145
|
|
|
// @codeCoverageIgnoreEnd |
146
|
|
|
|
147
|
|
|
return $body; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* |
152
|
|
|
*/ |
153
|
|
|
protected function getInfo(){ |
154
|
|
|
$curl_info = curl_getinfo($this->curl); |
155
|
|
|
|
156
|
|
|
if(is_array($curl_info)){ |
157
|
|
|
foreach($curl_info as $key => $value){ |
158
|
|
|
$this->curl_info->{$key} = $value; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$this->response_error->code = curl_errno($this->curl); |
163
|
|
|
$this->response_error->message = curl_error($this->curl); |
164
|
|
|
$this->response_error->version = curl_version(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
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.