1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the jyggen/curl library |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) Jonas Stendahl <[email protected]> |
9
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
10
|
|
|
* @link https://jyggen.com/projects/jyggen-curl Documentation |
11
|
|
|
* @link https://packagist.org/packages/jyggen/curl Packagist |
12
|
|
|
* @link https://github.com/jyggen/curl GitHub |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
namespace Jyggen\Curl; |
17
|
|
|
|
18
|
|
|
use Jyggen\Curl\Exception\CurlErrorException; |
19
|
|
|
use Jyggen\Curl\Exception\ProtectedOptionException; |
20
|
|
|
use Jyggen\Curl\HeaderBag; |
21
|
|
|
use Jyggen\Curl\Response; |
22
|
|
|
use Jyggen\Curl\RequestInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Request |
26
|
|
|
* |
27
|
|
|
* This class acts as a wrapper around cURL functions. |
28
|
|
|
*/ |
29
|
|
|
class Request implements RequestInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* A container of headers. |
33
|
|
|
* |
34
|
|
|
* @var \Jyggen\Curl\HeaderBag |
35
|
|
|
*/ |
36
|
|
|
public $headers; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The raw response body. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $content; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Protected default values that can't be changed. |
47
|
|
|
* These are required for this library to work correctly. |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $defaults = [ |
52
|
|
|
CURLOPT_RETURNTRANSFER => true, |
53
|
|
|
CURLOPT_HEADER => true, |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* If this request has been executed. |
58
|
|
|
* |
59
|
|
|
* @var boolean |
60
|
|
|
*/ |
61
|
|
|
protected $executed = false; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The cURL resource attached. |
65
|
|
|
* |
66
|
|
|
* @var resource |
67
|
|
|
*/ |
68
|
|
|
protected $handle; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Response object. |
72
|
|
|
* |
73
|
|
|
* @var \Jyggen\Curl\Response |
74
|
|
|
*/ |
75
|
|
|
protected $response; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Create a new Request instance. |
79
|
|
|
* |
80
|
|
|
* @param string $url |
81
|
|
|
*/ |
82
|
|
|
public function __construct($url) |
83
|
|
|
{ |
84
|
|
|
$this->handle = curl_init($url); |
85
|
|
|
$this->headers = new HeaderBag([], $this); |
86
|
|
|
|
87
|
|
|
foreach ($this->defaults as $option => $value) { |
88
|
|
|
curl_setopt($this->handle, $option, $value); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Shutdown sequence. |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function __destruct() |
97
|
|
|
{ |
98
|
|
|
curl_close($this->handle); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Retrieve the latest error. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getErrorMessage() |
107
|
|
|
{ |
108
|
|
|
$error = curl_error($this->handle); |
109
|
|
|
return ($error === '') ? null : $error; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Retrieve the cURL handle. |
114
|
|
|
* |
115
|
|
|
* @return resource |
116
|
|
|
*/ |
117
|
|
|
public function getHandle() |
118
|
|
|
{ |
119
|
|
|
return $this->handle; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get information regarding the request. |
124
|
|
|
* |
125
|
|
|
* @param int $key null |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
public function getInfo($key = null) |
129
|
|
|
{ |
130
|
|
|
if ($key === null) { // If no key is supplied return all available information. |
131
|
|
|
return curl_getinfo($this->handle); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return curl_getinfo($this->handle, $key); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the raw response. |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public function getRawResponse() |
143
|
|
|
{ |
144
|
|
|
return $this->content; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the response. |
149
|
|
|
* |
150
|
|
|
* @return Response |
151
|
|
|
*/ |
152
|
|
|
public function getResponse() |
153
|
|
|
{ |
154
|
|
|
if ($this->response === null and $this->isExecuted()) { |
155
|
|
|
$this->response = Response::forge($this); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this->response; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set an option for the request. |
163
|
|
|
* |
164
|
|
|
* @param mixed $option |
165
|
|
|
* @param mixed $value null |
166
|
|
|
* @return RequestInterface |
167
|
|
|
*/ |
168
|
|
|
public function setOption($option, $value = null) |
169
|
|
|
{ |
170
|
|
|
if (is_array($option)) { // If it's an array, loop through each option and call this method recursively. |
171
|
|
|
foreach ($option as $opt => $val) { |
172
|
|
|
$this->setOption($opt, $val); |
173
|
|
|
} |
174
|
|
|
return; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if (array_key_exists($option, $this->defaults)) { // If it is a default value. |
178
|
|
|
throw new ProtectedOptionException(sprintf('Unable to set protected option #%u', $option)); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if (curl_setopt($this->handle, $option, $value) === false) { |
182
|
|
|
throw new CurlErrorException(sprintf('Couldn\'t set option #%u', $option)); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Execute the request. |
188
|
|
|
* |
189
|
|
|
* @return void |
190
|
|
|
*/ |
191
|
|
|
public function execute() |
192
|
|
|
{ |
193
|
|
|
$this->content = curl_exec($this->handle); |
194
|
|
|
|
195
|
|
|
// If the execution wasn't successful, throw an exception. |
196
|
|
|
if ($this->isSuccessful() === false) { |
197
|
|
|
throw new CurlErrorException($this->getErrorMessage()); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$this->executed = true; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* If the request has been executed. |
205
|
|
|
* |
206
|
|
|
* @return boolean |
207
|
|
|
*/ |
208
|
|
|
public function isExecuted() |
209
|
|
|
{ |
210
|
|
|
return ($this->executed) ? true : false; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* If the request was successful. |
215
|
|
|
* |
216
|
|
|
* @return boolean |
217
|
|
|
*/ |
218
|
|
|
public function isSuccessful() |
219
|
|
|
{ |
220
|
|
|
return ($this->getErrorMessage() === null) ? true : false; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function setRawResponse($content) |
224
|
|
|
{ |
225
|
|
|
$this->executed = true; |
226
|
|
|
$this->content = $content; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|