| 1 |
<?php |
| 2 |
class Http |
| 3 |
{ |
| 4 |
protected $error; |
| 5 |
|
| 6 |
protected $info; |
| 7 |
|
| 8 |
protected $responseHeaders = array(); |
| 9 |
|
| 10 |
protected $authType; |
| 11 |
|
| 12 |
protected $username; |
| 13 |
|
| 14 |
protected $password; |
| 15 |
|
| 16 |
public function request($url) |
| 17 |
{ |
| 18 |
$ch = curl_init(); |
| 19 |
|
| 20 |
curl_setopt($ch, CURLOPT_URL, $url); |
| 21 |
curl_setopt($ch, CURLOPT_USERAGENT, 'HttpHeadersTool/1.0'); |
| 22 |
curl_setopt($ch, CURLOPT_REFERER, 'https://zinoui.com/'); |
| 23 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 24 |
curl_setopt($ch, CURLOPT_HEADER, false); |
| 25 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
| 26 |
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); |
| 27 |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
| 28 |
curl_setopt($ch, CURLOPT_NOBODY, true); |
| 29 |
curl_setopt($ch, CURLOPT_MAXREDIRS, 3); |
| 30 |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); |
| 31 |
curl_setopt($ch, CURLOPT_TIMEOUT, 15); |
| 32 |
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'getHeader')); |
| 33 |
|
| 34 |
if (!empty($this->username) && !empty($this->password)) |
| 35 |
{ |
| 36 |
switch ($this->authType) |
| 37 |
{ |
| 38 |
case 'basic': |
| 39 |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
| 40 |
break; |
| 41 |
case 'digest': |
| 42 |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); |
| 43 |
break; |
| 44 |
case 'ntlm': |
| 45 |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); |
| 46 |
break; |
| 47 |
case 'gss': |
| 48 |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE); |
| 49 |
break; |
| 50 |
default: |
| 51 |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); |
| 52 |
} |
| 53 |
|
| 54 |
curl_setopt($ch, CURLOPT_USERPWD, sprintf('%s:%s', $this->username, $this->password)); |
| 55 |
} |
| 56 |
|
| 57 |
$response = curl_exec($ch); |
| 58 |
$this->info = curl_getinfo($ch); |
| 59 |
if ($response === false) |
| 60 |
{ |
| 61 |
$this->error = curl_error($ch); |
| 62 |
} |
| 63 |
curl_close($ch); |
| 64 |
|
| 65 |
return $this; |
| 66 |
} |
| 67 |
|
| 68 |
protected function getHeader($ch, $header) |
| 69 |
{ |
| 70 |
$i = strpos($header, ':'); |
| 71 |
if (!empty($i)) |
| 72 |
{ |
| 73 |
$key = strtolower(substr($header, 0, $i)); |
| 74 |
$value = trim(substr($header, $i + 2)); |
| 75 |
$this->responseHeaders[$key] = $value; |
| 76 |
} |
| 77 |
return strlen($header); |
| 78 |
} |
| 79 |
|
| 80 |
public function getError() |
| 81 |
{ |
| 82 |
return $this->error; |
| 83 |
} |
| 84 |
|
| 85 |
public function getResponseHeaders() |
| 86 |
{ |
| 87 |
return $this->responseHeaders; |
| 88 |
} |
| 89 |
|
| 90 |
public function getHttpCode() |
| 91 |
{ |
| 92 |
return isset($this->info['http_code']) ? $this->info['http_code'] : null; |
| 93 |
} |
| 94 |
|
| 95 |
public function setAuthType($type) |
| 96 |
{ |
| 97 |
$type = strtolower($type); |
| 98 |
if (in_array($type, array('basic', 'digest', 'gss', 'ntlm'))) |
| 99 |
{ |
| 100 |
$this->authType = $type; |
| 101 |
} |
| 102 |
return $this; |
| 103 |
} |
| 104 |
|
| 105 |
public function setUsername($username) |
| 106 |
{ |
| 107 |
$this->username = $username; |
| 108 |
return $this; |
| 109 |
} |
| 110 |
|
| 111 |
public function setPassword($password) |
| 112 |
{ |
| 113 |
$this->password = $password; |
| 114 |
return $this; |
| 115 |
} |
| 116 |
} |
| 117 |
?> |