-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcurl.php
328 lines (328 loc) · 10.1 KB
/
curl.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
325
326
327
328
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* CodeIgniter Curl Class
*
* Work with remote servers via cURL much easier than using the native PHP bindings.
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author Philip Sturgeon
* @license http://philsturgeon.co.uk/code/dbad-license
* @link http://philsturgeon.co.uk/code/codeigniter-curl
*/
class Curl {
protected $_ci; // CodeIgniter instance
protected $response = ''; // Contains the cURL response for debug
protected $session; // Contains the cURL handler for a session
protected $url; // URL of the session
protected $options = array(); // Populates curl_setopt_array
protected $headers = array(); // Populates extra HTTP headers
public $error_code; // Error code returned as an int
public $error_string; // Error message returned as a string
public $info; // Returned after request (elapsed time, etc)
function __construct($url = '')
{
$this->_ci = & get_instance();
log_message('debug', 'cURL Class Initialized');
if ( ! $this->is_enabled())
{
log_message('error', 'cURL Class - PHP was not built with cURL enabled. Rebuild PHP with --with-curl to use cURL.');
}
$url AND $this->create($url);
}
public function __call($method, $arguments)
{
if (in_array($method, array('simple_get', 'simple_post', 'simple_put', 'simple_delete', 'simple_patch')))
{
// Take off the "simple_" and past get/post/put/delete/patch to _simple_call
$verb = str_replace('simple_', '', $method);
array_unshift($arguments, $verb);
return call_user_func_array(array($this, '_simple_call'), $arguments);
}
}
/* =================================================================================
* SIMPLE METHODS
* Using these methods you can make a quick and easy cURL call with one line.
* ================================================================================= */
public function _simple_call($method, $url, $params = array(), $options = array())
{
// Get acts differently, as it doesnt accept parameters in the same way
if ($method === 'get')
{
// If a URL is provided, create new session
$this->create($url.($params ? '?'.http_build_query($params, NULL, '&') : ''));
}
else
{
// If a URL is provided, create new session
$this->create($url);
$this->{$method}($params);
}
// Add in the specific options provided
$this->options($options);
return $this->execute();
}
public function simple_ftp_get($url, $file_path, $username = '', $password = '')
{
// If there is no ftp:// or any protocol entered, add ftp://
if ( ! preg_match('!^(ftp|sftp)://! i', $url))
{
$url = 'ftp://' . $url;
}
// Use an FTP login
if ($username != '')
{
$auth_string = $username;
if ($password != '')
{
$auth_string .= ':' . $password;
}
// Add the user auth string after the protocol
$url = str_replace('://', '://' . $auth_string . '@', $url);
}
// Add the filepath
$url .= $file_path;
$this->option(CURLOPT_BINARYTRANSFER, TRUE);
$this->option(CURLOPT_VERBOSE, TRUE);
return $this->execute();
}
/* =================================================================================
* ADVANCED METHODS
* Use these methods to build up more complex queries
* ================================================================================= */
public function post($params = array(), $options = array())
{
// If its an array (instead of a query string) then format it correctly
if (is_array($params))
{
$params = http_build_query($params, NULL, '&');
}
// Add in the specific options provided
$this->options($options);
$this->http_method('post');
$this->option(CURLOPT_POST, TRUE);
$this->option(CURLOPT_POSTFIELDS, $params);
}
public function put($params = array(), $options = array())
{
// If its an array (instead of a query string) then format it correctly
if (is_array($params))
{
$params = http_build_query($params, NULL, '&');
}
// Add in the specific options provided
$this->options($options);
$this->http_method('put');
$this->option(CURLOPT_POSTFIELDS, $params);
// Override method, I think this overrides $_POST with PUT data but... we'll see eh?
$this->option(CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT'));
}
public function patch($params = array(), $options = array())
{
// If its an array (instead of a query string) then format it correctly
if (is_array($params))
{
$params = http_build_query($params, NULL, '&');
}
// Add in the specific options provided
$this->options($options);
$this->http_method('patch');
$this->option(CURLOPT_POSTFIELDS, $params);
// Override method, I think this overrides $_POST with PATCH data but... we'll see eh?
$this->option(CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PATCH'));
}
public function delete($params, $options = array())
{
// If its an array (instead of a query string) then format it correctly
if (is_array($params))
{
$params = http_build_query($params, NULL, '&');
}
// Add in the specific options provided
$this->options($options);
$this->http_method('delete');
$this->option(CURLOPT_POSTFIELDS, $params);
}
public function set_cookies($params = array())
{
if (is_array($params))
{
$params = http_build_query($params, NULL, '&');
}
$this->option(CURLOPT_COOKIE, $params);
return $this;
}
public function http_header($header, $content = NULL)
{
$this->headers[] = $content ? $header . ': ' . $content : $header;
return $this;
}
public function http_method($method)
{
$this->options[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
return $this;
}
public function http_login($username = '', $password = '', $type = 'any')
{
$this->option(CURLOPT_HTTPAUTH, constant('CURLAUTH_' . strtoupper($type)));
$this->option(CURLOPT_USERPWD, $username . ':' . $password);
return $this;
}
public function proxy($url = '', $port = 80)
{
$this->option(CURLOPT_HTTPPROXYTUNNEL, TRUE);
$this->option(CURLOPT_PROXY, $url . ':' . $port);
return $this;
}
public function proxy_login($username = '', $password = '')
{
$this->option(CURLOPT_PROXYUSERPWD, $username . ':' . $password);
return $this;
}
public function ssl($verify_peer = TRUE, $verify_host = 2, $path_to_cert = NULL)
{
if ($verify_peer)
{
$this->option(CURLOPT_SSL_VERIFYPEER, TRUE);
$this->option(CURLOPT_SSL_VERIFYHOST, $verify_host);
if (isset($path_to_cert)) {
$path_to_cert = realpath($path_to_cert);
$this->option(CURLOPT_CAINFO, $path_to_cert);
}
}
else
{
$this->option(CURLOPT_SSL_VERIFYPEER, FALSE);
$this->option(CURLOPT_SSL_VERIFYHOST, $verify_host);
}
return $this;
}
public function options($options = array())
{
// Merge options in with the rest - done as array_merge() does not overwrite numeric keys
foreach ($options as $option_code => $option_value)
{
$this->option($option_code, $option_value);
}
// Set all options provided
curl_setopt_array($this->session, $this->options);
return $this;
}
public function option($code, $value, $prefix = 'opt')
{
if (is_string($code) && !is_numeric($code))
{
$code = constant('CURL' . strtoupper($prefix) . '_' . strtoupper($code));
}
$this->options[$code] = $value;
return $this;
}
// Start a session from a URL
public function create($url)
{
// If no a protocol in URL, assume its a CI link
if ( ! preg_match('!^\w+://! i', $url))
{
$this->_ci->load->helper('url');
$url = site_url($url);
}
$this->url = $url;
$this->session = curl_init($this->url);
return $this;
}
// End a session and return the results
public function execute()
{
// Set two default options, and merge any extra ones in
if ( ! isset($this->options[CURLOPT_TIMEOUT]))
{
$this->options[CURLOPT_TIMEOUT] = 30;
}
if ( ! isset($this->options[CURLOPT_RETURNTRANSFER]))
{
$this->options[CURLOPT_RETURNTRANSFER] = TRUE;
}
if ( ! isset($this->options[CURLOPT_FAILONERROR]))
{
$this->options[CURLOPT_FAILONERROR] = TRUE;
}
// Only set follow location if not running securely
if ( ! ini_get('safe_mode') && ! ini_get('open_basedir'))
{
// Ok, follow location is not set already so lets set it to true
if ( ! isset($this->options[CURLOPT_FOLLOWLOCATION]))
{
$this->options[CURLOPT_FOLLOWLOCATION] = TRUE;
}
}
if ( ! empty($this->headers))
{
$this->option(CURLOPT_HTTPHEADER, $this->headers);
}
$this->options();
// Execute the request & and hide all output
$this->response = curl_exec($this->session);
$this->info = curl_getinfo($this->session);
// Request failed
if ($this->response === FALSE)
{
$errno = curl_errno($this->session);
$error = curl_error($this->session);
curl_close($this->session);
$this->set_defaults();
$this->error_code = $errno;
$this->error_string = $error;
return FALSE;
}
// Request successful
else
{
curl_close($this->session);
$this->last_response = $this->response;
$this->set_defaults();
return $this->last_response;
}
}
public function is_enabled()
{
return function_exists('curl_init');
}
public function debug()
{
echo "=============================================<br/>\n";
echo "<h2>CURL Test</h2>\n";
echo "=============================================<br/>\n";
echo "<h3>Response</h3>\n";
echo "<code>" . nl2br(htmlentities($this->last_response)) . "</code><br/>\n\n";
if ($this->error_string)
{
echo "=============================================<br/>\n";
echo "<h3>Errors</h3>";
echo "<strong>Code:</strong> " . $this->error_code . "<br/>\n";
echo "<strong>Message:</strong> " . $this->error_string . "<br/>\n";
}
echo "=============================================<br/>\n";
echo "<h3>Info</h3>";
echo "<pre>";
print_r($this->info);
echo "</pre>";
}
public function debug_request()
{
return array(
'url' => $this->url
);
}
public function set_defaults()
{
$this->response = '';
$this->headers = array();
$this->options = array();
$this->error_code = NULL;
$this->error_string = '';
$this->session = NULL;
}
}
/* End of file Curl.php */
/* Location: ./application/libraries/Curl.php */