4 private $_requestHeader = array();
15 private $_proxy = array();
24 foreach ($headers as $key => $value) {
25 $this->_requestHeader[$key] = $value;
36 public function auth($username, $password =
'') {
38 'Authorization' =>
'Basic '.base64_encode(
"$username:$password")
53 public function proxy($host, $port, $ssl =
false, $username =
'', $password =
'', $insecure =
false) {
54 $this->_proxy[
'host'] = $host;
55 $this->_proxy[
'port'] = (int) $port;
56 $this->_proxy[
'ssl'] = (bool) $ssl;
57 $this->_proxy[
'username'] = $username;
58 $this->_proxy[
'password'] = $password;
59 $this->_proxy[
'insecure'] = $insecure;
69 public function get($url, $additional_header = array()) {
73 'header' => $additional_header
85 public function post($url, $param = array(), $additional_header = array()) {
90 'header' => $additional_header
103 public function download($url, $target, $param = array(), $additional_header = array()) {
107 'download' => $target,
109 'header' => $additional_header
122 public function upload($url, $file, $param = array(), $additional_header = array()) {
128 'header' => $additional_header
140 foreach (array_merge($this->_requestHeader, $request[
'header']) as $key => $value) {
141 $header[] = $key.
": ".$value;
145 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
146 curl_setopt($ch, CURLOPT_URL, $request[
'url']);
147 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request[
'method']);
150 if ($request[
'download']) {
151 $fp = fopen($request[
'download'],
'w+');
152 curl_setopt($ch, CURLOPT_FILE, $fp);
153 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,
true);
154 curl_setopt($ch, CURLOPT_TIMEOUT, 900);
156 curl_setopt($ch, CURLOPT_HEADER,
true);
157 curl_setopt($ch, CURLOPT_RETURNTRANSFER,
true);
161 if (!$this->verifySSL) {
162 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,
false);
163 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,
false);
167 if ($request[
'upload']) {
168 curl_setopt($ch, CURLOPT_INFILE, $request[
'upload']);
169 curl_setopt($ch, CURLOPT_INFILESIZE, filesize($request[
'upload']));
173 if (!empty($request[
'param'])) {
174 assert($request[
'method'] ===
'POST',
"Makes only sense with POST.");
175 if ($this->use_http_build_query) {
176 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request[
'param']));
178 curl_setopt($ch, CURLOPT_POSTFIELDS, $request[
'param']);
183 if (!empty($this->_proxy)) {
184 curl_setopt($ch, CURLOPT_PROXY, $this->_proxy[
'host']);
185 curl_setopt($ch, CURLOPT_PROXYPORT, $this->_proxy[
'port']);
186 if ($this->_proxy[
'ssl']) {
187 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL,
true);
189 if (!empty($this->_proxy[
'username'])) {
190 curl_setopt($ch, CURLOPT_PROXYUSERPWD, implode(
':', array(
191 $this->_proxy[
'username'],
192 $this->_proxy[
'password']
195 if ($this->_proxy[
'insecure']) {
196 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,
false);
197 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,
false);
201 $response = curl_exec($ch);
203 $this->error = curl_error($ch);
208 if (!$request[
'download']) {
210 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
211 $headers = substr($response, 0, $header_size);
212 $content_orig = substr($response, $header_size);
213 $this->responseHeader = array();
214 foreach (explode(
"\n", $headers) as $value) {
215 $pos = strpos($value,
":");
216 $key = substr($value, 0, $pos);
217 $val = trim(substr($value, $pos + 1));
218 $this->responseHeader[$key] = $val;
222 $this->cookie = array();
223 if ($this->responseHeader[
'Set-Cookie']) {
224 $cookies = explode(
"\n", $this->responseHeader[
'Set-Cookie']);
225 foreach ($cookies as $line) {
226 $row = explode(
";", $line);
227 $row0 = array_shift($row);
228 $row0_keyval = explode(
"=", $row0);
229 $cookie = array(
'value' => $row0_keyval[1]);
230 foreach ($row as $s) {
231 $keyval = explode(
"=", $s);
232 $cookie[trim($keyval[0])] = $keyval[1];
234 $this->cookie[$row0_keyval[0]] =
$cookie;
239 $this->info = curl_getinfo($ch);
240 $this->responseTime = $this->info[
'total_time'];
241 $this->content = $this->responseHeader[
'Content-Encoding']==
'gzip' ? zlib_decode($content_orig) : $content_orig;
244 if (strpos($this->responseHeader[
'Content-Type'],
"application/json") !==
false) {
245 $this->json = json_decode($this->content);
250 $this->code = $this->info[
'http_code'];
post($url, $param=array(), $additional_header=array())
upload($url, $file, $param=array(), $additional_header=array())
auth($username, $password='')
download($url, $target, $param=array(), $additional_header=array())
proxy($host, $port, $ssl=false, $username='', $password='', $insecure=false)