-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathACurl.php
executable file
·324 lines (298 loc) · 9.45 KB
/
ACurl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
Yii::import("packages.curl.*");
/**
* A wrapper that provides easy access to curl functions.
* @author Charles Pick
* @package packages.curl
*/
class ACurl extends CComponent {
/**
* Holds the options to use for this request
* @see getOptions
* @see setOptions
* @var ACurlOptions
*/
protected $_options;
/**
* Holds the curl handle
* @var resource
*/
protected $_handle;
/**
* Holds the cache key
* @var string
*/
protected $_cacheKey;
/**
* The caching duration
* @var integer
*/
protected $_cacheDuration;
/**
* The cache dependency
* @var CCacheDependency
*/
protected $_cacheDependency;
/**
* The cache component to use when caching results
* @var CCache
*/
protected $_cacheComponent;
/**
* Whether to cache the query result or not.
* Defaults to false.
* @var boolean
*/
protected $_cache = false;
/**
* Returns the CURL handle for this request
* @var resource
*/
public function getHandle() {
if ($this->_handle === null) {
$this->_handle = curl_init();
}
return $this->_handle;
}
/**
* Sets the curl handle for this request.
* @param resource $value The CURL handle
* @return ACurl $this with the handle set.
*/
public function setHandle($value) {
$this->_handle = $value;
return $this;
}
/**
* Gets the options to use for this request.
* @return ACurlOptions the options
*/
public function getOptions() {
if ($this->_options === null) {
$this->_options = new ACurlOptions(array(
"userAgent" => "Yii PHP Framework / ACurl",
"header" => true,
"followLocation" => true,
"returnTransfer" => true,
"timeout" => 30,
"encoding" => "gzip",
"ssl_verifypeer" => false,
));
}
return $this->_options;
}
/**
* Sets the options to the given value.
* @param mixed $value the options, either an array or an ACurlOptions object
* @return ACurl $this with the modified options
*/
public function setOptions($value) {
if (is_array($value)) {
$value = new ACurlOptions($value);
}
$this->_options = $value;
return $this;
}
/**
* Prepares the CURL request, applies the options to the handler.
*/
public function prepareRequest() {
$this->getOptions()->applyTo($this);
}
/**
* Sets the post data and the URL to post to and performs the request
* @param string $url The URL to post to.
* @param array $data The data to post key=>value
* @param boolean $execute whether to execute the request immediately or not, defaults to true
* @return ACurlResponse|ACurl the curl response, or $this if $execute is set to false
*/
public function post($url, $data = array(), $execute = true) {
$this->getOptions()->url = $url;
$this->getOptions()->postfields = is_string($data) ? $data : http_build_query($data);
$this->getOptions()->post = true;
$this->prepareRequest();
return $execute ? $this->exec() : $this;
}
/**
* Sets the PUT data and the URL to PUT to and performs the request
* @param string $url The URL to PUT to.
* @param array $data The data to PUT key=>value
* @param boolean $execute whether to execute the request immediately or not, defaults to true
* @return ACurlResponse|ACurl the curl response, or $this if $execute is set to false
*/
public function put($url, $data = array(), $execute = true) {
$this->getOptions()->url = $url;
$this->getOptions()->postfields = is_string($data) ? $data : http_build_query($data);
$this->getOptions()->post = true;
$this->getOptions()->customRequest = "PUT";
$this->prepareRequest();
return $execute ? $this->exec() : $this;
}
/**
* Sets the DELETE data and the URL to DELETE to and performs the request
* @param string $url The URL to DELETE to.
* @param boolean $execute whether to execute the request immediately or not, defaults to true
* @return ACurlResponse|ACurl the curl response, or $this if $execute is set to false
*/
public function delete($url, $execute = true) {
$this->getOptions()->url = $url;
$this->getOptions()->customRequest = "DELETE";
$this->prepareRequest();
return $execute ? $this->exec() : $this;
}
/**
* Sets the URL and performs the GET request
* perform the actual request.
* @param string $url The URL to get.
* @param boolean $execute whether to execute the request immediately or not, defaults to true
* @return ACurlResponse|ACurl the curl response, or $this if $execute is set to false
*/
public function get($url, $execute = true) {
$this->getOptions()->url = $url;
$this->getOptions()->post = false;
$this->prepareRequest();
return $execute ? $this->exec() : $this;
}
/**
* Sets the URL and performs the HEAD request
* @param string $url The URL to post to.
* @param boolean $execute whether to execute the request immediately or not, defaults to true
* @return ACurlResponse|ACurl the curl response, or $this if $execute is set to false
*/
public function head($url, $execute = true) {
$this->getOptions()->url = $url;
$this->getOptions()->nobody = true;
$this->prepareRequest();
return $execute ? $this->exec() : $this;
}
/**
* Executes the request and returns the response.
* @return ACurlResponse|false the wrapped curl response, or false if the request is stopped by beforeRequest()
* @throws ACurlException a curl exception if there was a probl
*/
public function exec() {
$response = new ACurlResponse;
$response->request = $this;
$data = false;
if (!$this->beforeRequest()) {
return false;
}
$cache = $this->_cache;
if ($this->getOptions()->itemAt("post") || $this->getOptions()->itemAt("customRequest")) {
$cache = false;
}
if ($cache) {
$data = $this->getCacheComponent()->get($this->getCacheKey());
}
if ($data === false) {
$data = curl_exec($this->getHandle());
if ($cache) {
$this->getCacheComponent()->set($this->getCacheKey(),$data,$this->_cacheDuration,$this->_cacheDependency);
}
}
$response->data = $data;
if ($this->getOptions()->header) {
$response->headers = mb_substr($response->data, 0, $response->info->header_size);
$response->data = mb_substr($response->data, $response->info->header_size);
if (mb_strlen($response->data) == 0) {
$response->data = false;
}
}
if ($response->getIsError() && $response->getLastHeaders() !== false) {
throw new ACurlException($response->getLastHeaders()->http_code,"Curl Error: ".$response->getLastHeaders()->http_code,$response);
}
if (curl_error($this->getHandle())) {
throw new ACurlException(curl_errno($this->getHandle()),curl_error($this->getHandle()), $response);
}
$this->afterRequest($response);
return $response;
}
/**
* Enables caching for curl requests
* @param integer $duration the caching duration
* @param CCacheDependency $dependency the cache dependency for this request
* @return ACurl $this with the cache setting applied
*/
public function cache($duration = 60, $dependency = null) {
$this->_cache = true;
$this->_cacheDuration = $duration;
$this->_cacheDependency = $dependency;
return $this;
}
/**
* Sets the cache component to use for this request
* @param CCache $cacheComponent the cache component
*/
public function setCacheComponent($cacheComponent) {
$this->_cacheComponent = $cacheComponent;
}
/**
* Gets the cache component for this curl request
* @return CCache the caching component to use for this request
*/
public function getCacheComponent() {
if ($this->_cacheComponent === null) {
$this->_cacheComponent = Yii::app()->getCache();
}
return $this->_cacheComponent;
}
/**
* Sets the cache key for this request
* @param string $cacheKey the cache key
* @return string the cache key
*/
public function setCacheKey($cacheKey) {
return $this->_cacheKey = $cacheKey;
}
/**
* Gets the cache key for this request
* @return string the cache key
*/
public function getCacheKey() {
if ($this->_cacheKey === null) {
$this->_cacheKey = "ACurl:cachedRequest:".sha1(serialize($this->getOptions()->toArray()));
}
return $this->_cacheKey;
}
/**
* This method is invoked before making a curl request.
* The default implementation raises the {@link onBeforeRequest} event.
* @return boolean whether the event is valid and the request should continue
*/
protected function beforeRequest() {
if($this->hasEventHandler('onBeforeRequest'))
{
$event=new CModelEvent($this);
$this->onBeforeRequest($event);
return $event->isValid;
}
else
return true;
}
/**
* This event is raised before the curl request is made
* @param CModelEvent $event the event parameter
*/
public function onBeforeRequest($event) {
$this->raiseEvent('onBeforeRequest',$event);
}
/**
* This method is invoked after a curl request.
* The default implementation raises the {@link onAfterRequest} event.
* @param ACurlResponse $response the curl response
*/
protected function afterRequest(ACurlResponse $response) {
if ($this->hasEventHandler('onAfterRequest')) {
$event = new CModelEvent();
$event->params['response'] = $response;
$this->onAfterRequest($event);
}
}
/**
* This event is raised after the curl request is made
* @param CModelEvent $event the event parameter
*/
public function onAfterRequest($event) {
$this->raiseEvent('onBeforeRequest',$event);
}
}