-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathCurl.php
More file actions
213 lines (177 loc) · 6.15 KB
/
Curl.php
File metadata and controls
213 lines (177 loc) · 6.15 KB
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
<?php
namespace Hypernode;
/**
* Because it is one-on-one from curl/curl library, added locally because magerun doesn't support vendor including in modules
* Uses his own bootstrap
* @codeCoverageIgnore
*
* @package Hypernode
*/
class Curl
{
// The HTTP authentication method(s) to use.
const AUTH_BASIC = CURLAUTH_BASIC;
const AUTH_DIGEST = CURLAUTH_DIGEST;
const AUTH_GSSNEGOTIATE = CURLAUTH_GSSNEGOTIATE;
const AUTH_NTLM = CURLAUTH_NTLM;
const AUTH_ANY = CURLAUTH_ANY;
const AUTH_ANYSAFE = CURLAUTH_ANYSAFE;
const USER_AGENT = 'PHP Curl/1.1 (+https://github.com/mod-php/curl)';
private $_cookies = array();
private $_headers = array();
public $curl;
public $error = false;
public $error_code = 0;
public $error_message = null;
public $curl_error = false;
public $curl_error_code = 0;
public $curl_error_message = null;
public $http_error = false;
public $http_status_code = 0;
public $http_error_message = null;
public $request_headers = null;
public $response_headers = null;
public $response = null;
public function __construct()
{
if (!extension_loaded('curl')) {
throw new \ErrorException('cURL library is not loaded');
}
$this->init();
}
public function get($url, $data = array())
{
if (count($data) > 0) {
$this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
} else {
$this->setopt(CURLOPT_URL, $url);
}
$this->setopt(CURLOPT_HTTPGET, true);
$this->_exec();
}
public function post($url, $data = array())
{
$this->setopt(CURLOPT_URL, $url);
$this->setopt(CURLOPT_POST, true);
if (is_array($data) || is_object($data))
{
$data = http_build_query($data);
}
$this->setopt(CURLOPT_POSTFIELDS, $data);
$this->_exec();
}
public function put($url, $data = array())
{
$this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
$this->setopt(CURLOPT_CUSTOMREQUEST, 'PUT');
$this->_exec();
}
public function patch($url, $data = array())
{
$this->setopt(CURLOPT_URL, $url);
$this->setopt(CURLOPT_CUSTOMREQUEST, 'PATCH');
$this->setopt(CURLOPT_POSTFIELDS, $data);
$this->_exec();
}
public function delete($url, $data = array())
{
$this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
$this->setopt(CURLOPT_CUSTOMREQUEST, 'DELETE');
$this->_exec();
}
public function setBasicAuthentication($username, $password)
{
$this->setHttpAuth(self::AUTH_BASIC);
$this->setopt(CURLOPT_USERPWD, $username . ':' . $password);
}
protected function setHttpAuth($httpauth)
{
$this->setOpt(CURLOPT_HTTPAUTH, $httpauth);
}
public function setHeader($key, $value)
{
$this->_headers[$key] = $key . ': ' . $value;
$this->setopt(CURLOPT_HTTPHEADER, array_values($this->_headers));
}
public function setUserAgent($user_agent)
{
$this->setopt(CURLOPT_USERAGENT, $user_agent);
}
public function setReferrer($referrer)
{
$this->setopt(CURLOPT_REFERER, $referrer);
}
public function setCookie($key, $value)
{
$this->_cookies[$key] = $value;
$this->setopt(CURLOPT_COOKIE, http_build_query($this->_cookies, '', '; '));
}
public function setOpt($option, $value)
{
return curl_setopt($this->curl, $option, $value);
}
public function verbose($on = true)
{
$this->setopt(CURLOPT_VERBOSE, $on);
}
public function close()
{
if (is_resource($this->curl)) {
curl_close($this->curl);
}
}
public function reset()
{
$this->close();
$this->_cookies = array();
$this->_headers = array();
$this->error = false;
$this->error_code = 0;
$this->error_message = null;
$this->curl_error = false;
$this->curl_error_code = 0;
$this->curl_error_message = null;
$this->http_error = false;
$this->http_status_code = 0;
$this->http_error_message = null;
$this->request_headers = null;
$this->response_headers = null;
$this->response = null;
$this->init();
}
public function _exec()
{
$this->response = curl_exec($this->curl);
$this->curl_error_code = curl_errno($this->curl);
$this->curl_error_message = curl_error($this->curl);
$this->curl_error = !($this->curl_error_code === 0);
$this->http_status_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
$this->http_error = in_array(floor($this->http_status_code / 100), array(4, 5));
$this->error = $this->curl_error || $this->http_error;
$this->error_code = $this->error ? ($this->curl_error ? $this->curl_error_code : $this->http_status_code) : 0;
$this->request_headers = preg_split('/\r\n/', curl_getinfo($this->curl, CURLINFO_HEADER_OUT), null, PREG_SPLIT_NO_EMPTY);
$this->response_headers = '';
if (!(strpos($this->response, "\r\n\r\n") === false)) {
list($response_header, $this->response) = explode("\r\n\r\n", $this->response, 2);
while (strtolower(trim($response_header)) === 'http/1.1 100 continue') {
list($response_header, $this->response) = explode("\r\n\r\n", $this->response, 2);
}
$this->response_headers = preg_split('/\r\n/', $response_header, null, PREG_SPLIT_NO_EMPTY);
}
$this->http_error_message = $this->error ? (isset($this->response_headers['0']) ? $this->response_headers['0'] : '') : '';
$this->error_message = $this->curl_error ? $this->curl_error_message : $this->http_error_message;
return $this->error_code;
}
public function __destruct()
{
$this->close();
}
private function init()
{
$this->curl = curl_init();
$this->setUserAgent(self::USER_AGENT);
$this->setopt(CURLINFO_HEADER_OUT, true);
$this->setopt(CURLOPT_HEADER, true);
$this->setopt(CURLOPT_RETURNTRANSFER, true);
}
}