-
Notifications
You must be signed in to change notification settings - Fork 3
/
Purl.php
322 lines (271 loc) · 8.28 KB
/
Purl.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
<?php
/**
* Load cUrl constants and functions definitions
*/
require_once 'constants.php';
require_once 'functions.php';
/**
* Wrapper for file_get_contents, which aims very basic PHP curl functionality
*/
class Purl
{
/**
* Curl options container
*
* @var type
*/
private $_options = array();
/**
* Request parameters
*
* @var array
*/
private $_params = array();
/**
* Headers list
*
* @var array
*/
private $_headers = array();
/**
* Request notifications log
*
* @var array
*/
private $_logs = array();
/**
* Last error message
*
* @var type
*/
private $_error = 0;
/**
* Last error number
*
* @var number
*/
private $_errorno = '';
/**
* URL to call
*
* @var string
*/
private $_url = '';
/**
* Response data
*
* @var string
*/
private $_result = '';
/**
* Request method
*
* @var string
*/
private $_method = 'GET';
/**
* Default user agent
*
* @var string
*/
private $_agent = 'pUrl PHP Client V 0.1';
/**
* Info container
*
* @var array
*/
private $_info = array();
private static $_infoMap = array(
CURLINFO_HTTP_CODE => 'http_code',
CURLINFO_HEADER_OUT => 'request_header'
);
/**
* Setter for curl options
*
* @param int $option CURLOPT constant
* @param mixed $value option value to set
* @return void
*/
public function setOption($option, $value)
{
$this->_options[$option] = $value;
switch ($option) {
case CURLOPT_POSTFIELDS:
$this->_params = $value;
$this->_method = 'POST';
break;
case CURLOPT_HTTPHEADER:
$this->_headers = $value;
break;
case CURLOPT_URL:
$this->_url = $value;
break;
case CURLOPT_POST:
$this->_method = $value ? 'POST' : $this->_method;
break;
case CURLOPT_PUT:
$this->_method = $value ? 'POST' : $this->_method;
break;
case CURLOPT_CUSTOMREQUEST:
$this->_method = $value;
break;
case CURLOPT_USERAGENT:
$this->_agent = $value;
break;
}
return true;
}
/**
* Get last error number
*
* @return int
*/
public function getErrorNumber()
{
return $this->_errorno;
}
/**
* Get last error message
*
* @return type
*/
public function getError()
{
return $this->_error;
}
/**
*
*
* @param type $opt
* @return type
*/
public function getInfo($opt = 0)
{
if ($opt === 0) {
return $this->_info;
}
if (isset(self::$_infoMap[$opt])) {
return $this->_info[self::$_infoMap[$opt]];
}
trigger_error(__FUNCTION__ . PURL_NOT_SUPPORTED_MSG, PURL_ERROR_TYPE);
}
/**
*
*
* @param type $opt
* @return type
*/
public function getLog()
{
return $this->_logs;
}
/**
* Execute "curl" call
*
* @return type
*/
public function execute()
{
$headers = $query = '';
if (!empty($this->_params)) {
if (is_array($this->_params)) {
$query = http_build_query($this->_params, null, '&');
} else {
$query = $this->_params;
}
}
// add cookie header
if (isset($this->_options[CURLOPT_COOKIE])) {
$this->_headers['Cookie'] = $this->_options[CURLOPT_COOKIE];
}
if ($this->_headers) {
foreach ($this->_headers as $name => $value) {
$headers .= $name . ': ' . $value . "\r\n";
}
}
if (!isset($this->_headers['Content-type'])) $headers .= "Content-type: " . "application/x-www-form-urlencoded"."\r\n";
return $this->_call($query, $this->_info['request_header'] = $headers);
}
/**
* Make request using file_get_contents.
* Trying to set as much curl original settings as possible
*
* @param string $query GET query string
* @param array $headers headers to send along with request
* @return boolean|string
*/
protected function _call($query, $headers)
{
$options =
array('http'=>
array(
'method' => $this->_method,
'header' => $headers,
'ignore_errors' => true,
'user_agent' => $this->_agent,
)
);
// HTTP context settings
if (isset($this->_options[CURLOPT_TIMEOUT])) $options['http']['timeout'] = $this->_options[CURLOPT_TIMEOUT];
if (isset($this->_options[CURLOPT_FOLLOWLOCATION])) $options['http']['follow_location'] = $this->_options[CURLOPT_FOLLOWLOCATION];
if (isset($this->_options[CURLOPT_MAXREDIRS])) $options['http']['max_redirects'] = $this->_options[CURLOPT_MAXREDIRS];
if (isset($this->_options[CURLOPT_PROXY])) {
$options['http']['proxy'] = $this->_options[CURLOPT_PROXY];
$options['http']['request_fulluri'] = true;
}
// SSL context settings
if (isset($this->_options[CURLOPT_SSL_VERIFYPEER])) $options['ssl']['verify_peer'] = $this->_options[CURLOPT_SSL_VERIFYPEER];
if (isset($this->_options[CURLOPT_CAINFO])) $options['ssl']['cafile'] = $this->_options[CURLOPT_CAINFO];
if ($this->_method === 'POST') {
$options['http']['content'] = $query;
} elseif (!empty($query)) {
$this->_url .= '?' . $query;
}
$context = stream_context_create($options);
stream_context_set_params($context, array('notification' => function() {
// not working yet :/
$this->_logs[] = func_get_args();
}));
$reporting = error_reporting(0);
if (!empty($this->_options[CURLOPT_NOBODY])) {
$handler = fopen($this->_url, 'r', false, $context);
} else {
$this->_result = file_get_contents($this->_url, false, $context);
}
$this->_info['http_code'] = explode(' ', $http_response_header[0])[1];
error_reporting($reporting);
// on failure
if ($this->_result === false || (isset($handler) && $handler === false)) {
$this->_errorHandler();
return false;
}
if (!empty($this->_options[CURLOPT_HEADER])) {
$this->_result = implode($http_response_header, "\r\n") . "\r\n\r\n" . $this->_result;
}
return isset($this->_options[CURLOPT_RETURNTRANSFER]) ? $this->_result : true;
}
/**
* Handle request errors
* Set error message and message number
*
* @return void
*/
private function _errorHandler()
{
$error = error_get_last();
$parts = parse_url($this->_url);
if (strpos($error['message'], 'php_network_getaddresses')) {
$this->_error = "Couldn't resolve host '" . $parts['host'] . "'";
$this->_errorno = 6;
} elseif (strpos($error['message'], 'No such file or directory')) {
$this->_error = "Protocol " . $parts['scheme'] . " not supported or disabled in libcurl";
$this->_errorno = 1;
} elseif (strpos($error['message'], 'failed to open stream')) {
$this->_error = "couldn't connect to host";
$this->_errorno = 7;
} else {
$this->_error = "Unknown Purl error. PHP error: " . $error['message'];
$this->_errorno = 100;
}
}
}