-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.php
161 lines (152 loc) · 5.23 KB
/
http.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
<?php
class http {
private static $instance = NULL;//实例对象
private $host;//地址
private $port = 80;//端口号
private $fp = NULL;//socket链接资源
private $timeout = 120;//socket请求超时时间(s)
private $header = array();//请求头部信息
private $httpVer = 'HTTP/1.0';//请求标准
private $crlf = "\r\n";//请求分隔符
private $accept = "*/*";
private $agent = 'Mozilla/5.0 (Windows NT 6.1; rv:20.0) Gecko/20100101 Firefox/20.0';//伪装浏览器
private $maxLineLength = 4096;//最大行数长度
private $maxLength = 1024;//最大数据长度
private $resultHeader;//返回头部
private $resultData;//返回内容
/**
* 获取实例单例
* @return object
*/
public static function getInstance() {
if (is_null(self::$instance)) {
self::$instance = new http();
}
return self::$instance;
}
/**
* 隐藏构造函数
*/
private function __construct() {
}
/**
* 设置请求头部信息
* @param array $header 请求头部信息数组
*/
public function setHeader($header) {
$this->header = $header;
}
/**
* 发送请求
* @param string $url 请求链接
* @param array $data post请求数据
* @param string $method 请求方法post/get
*/
public function sendRequest($url, $data=array(), $method='post') {
$urlArr = parse_url($url);
if (isset($urlArr['port'])) {
$this->port = $urlArr['port'];
}
switch (strtolower($urlArr['scheme'])) {
case 'http':
$this->host = $urlArr['host'];
break;
case 'https':
$this->host = 'ssl://' . $urlArr['host'];
$this->port = 443;
break;
default:
echo "Error: wrong url!";
exit;
break;
}
$this->connect();
if (strtolower($method) == 'post') {
$method = 'POST';
} else {
$method = 'GET';
}
$path = isset($urlArr['path']) ? $urlArr['path'] : '/';
if (isset($urlArr['query'])) {
$path .= '?' . $urlArr['query'];
}
$httpVer = isset($this->header['httpVer']) ? $this->header['httpVer'] : $this->httpVer;
$headerStr = "{$method} {$path} {$httpVer}{$this->crlf}";
$host = isset($urlArr['port']) ? $urlArr['host'] . ':' . $urlArr['port'] : $urlArr['host'];
$headerStr .= "Host: {$host}{$this->crlf}";
$accept = isset($this->header['Accept']) ? $this->header['Accept'] : $this->accept;
$headerStr .= "Accept: {$accept}{$this->crlf}";
if (isset($this->header['Referer'])) {
$headerStr .= "Referer: {$this->header['Referer']}{$this->crlf}";
}
$agent = isset($this->header['User-Agent']) ? $this->header['User-Agent'] : $this->agent;
$headerStr .= "User-Agent: {$agent}{$this->crlf}";
if (isset($this->header['Cookie'])) {
$headerStr .= "Cookie: {$this->header['Cookie']}{$this->crlf}";
}
$dataLength = 0;
if ($method == 'POST') {
if (!empty($data)) {
$dataStr = http_build_query($data);
$dataLength = strlen($dataStr);
}
$headerStr .= "Content-Type: application/x-www-form-urlencoded{$this->crlf}";
$headerStr .= "Cache-Control: no-cache{$this->crlf}";
$headerStr .= "Pragma: no-cache{$this->crlf}";
$headerStr .= "Content-Length: {$dataLength}{$this->crlf}";
}
$headerStr .= $this->crlf;
if ($dataLength > 0) {
$headerStr .= $dataStr;
}
fwrite($this->fp, $headerStr, strlen($headerStr));
$this->resultHeader = '';
while ($curContent = fgets($this->fp, $this->maxLineLength)) {
if ($curContent == $this->crlf) {
break;
}
$this->resultHeader .= $curContent;
}
$this->resultData = '';
do {
$curContent = fread($this->fp, $this->maxLength);
if (strlen($curContent) == 0) {
break;
}
$this->resultData .= $curContent;
} while(TRUE);
$this->disconnect();
unset($this->fp);
}
/**
* 获取返回的头部信息
* @return string
*/
public function getResultHeader() {
return $this->resultHeader;
}
/**
* 获取返回的数据信息
* @return string
*/
public function getResultData() {
return $this->resultData;
}
/**
* 发起请求
*/
private function connect() {
$this->fp = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
if (!$this->fp) {
echo "Error: {$errno}--{$errstr}";
exit;
}
}
/**
* 结束请求
*/
private function disconnect() {
return fclose($this->fp);
}
}
?>