1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace tinymeng\Chinaums\Tools; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Http 请求类 |
7
|
|
|
*/ |
8
|
|
|
class Http |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* 发送一个POST请求 |
13
|
|
|
* @param string $url 请求URL |
14
|
|
|
* @param array $params 请求参数 |
15
|
|
|
* @param array $options 扩展参数 |
16
|
|
|
* @return mixed|string |
17
|
|
|
*/ |
18
|
|
|
public static function post($url, $params = [], $options = []) |
19
|
|
|
{ |
20
|
|
|
$req = self::sendRequest($url, $params, 'POST', $options); |
21
|
|
|
return $req['ret'] ? $req['msg'] : ''; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* 发送一个GET请求 |
26
|
|
|
* @param string $url 请求URL |
27
|
|
|
* @param array $params 请求参数 |
28
|
|
|
* @param array $options 扩展参数 |
29
|
|
|
* @return mixed|string |
30
|
|
|
*/ |
31
|
|
|
public static function get($url, $params = [], $options = []) |
32
|
|
|
{ |
33
|
|
|
$req = self::sendRequest($url, $params, 'GET', $options); |
34
|
|
|
return $req['ret'] ? $req['msg'] : ''; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* CURL发送Request请求,含POST和REQUEST |
39
|
|
|
* @param string $url 请求的链接 |
40
|
|
|
* @param mixed $params 传递的参数 |
41
|
|
|
* @param string $method 请求的方法 |
42
|
|
|
* @param mixed $options CURL的参数 |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public static function sendRequest($url, $params = [], $method = 'POST', $options = []) |
46
|
|
|
{ |
47
|
|
|
$method = strtoupper($method); |
48
|
|
|
$protocol = substr($url, 0, 5); |
49
|
|
|
$query_string = is_array($params) ? http_build_query($params) : $params; |
50
|
|
|
|
51
|
|
|
$ch = curl_init(); |
52
|
|
|
$defaults = []; |
53
|
|
|
if ('GET' == $method) { |
54
|
|
|
$geturl = $query_string ? $url . (stripos($url, "?") !== false ? "&" : "?") . $query_string : $url; |
55
|
|
|
$defaults[CURLOPT_URL] = $geturl; |
56
|
|
|
} else { |
57
|
|
|
$defaults[CURLOPT_URL] = $url; |
58
|
|
|
if ($method == 'POST') { |
59
|
|
|
$defaults[CURLOPT_POST] = 1; |
60
|
|
|
} else { |
61
|
|
|
$defaults[CURLOPT_CUSTOMREQUEST] = $method; |
62
|
|
|
} |
63
|
|
|
$defaults[CURLOPT_POSTFIELDS] = $params; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$defaults[CURLOPT_HEADER] = false; |
67
|
|
|
$defaults[CURLOPT_USERAGENT] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36"; |
68
|
|
|
$defaults[CURLOPT_FOLLOWLOCATION] = true; |
69
|
|
|
$defaults[CURLOPT_RETURNTRANSFER] = true; |
70
|
|
|
$defaults[CURLOPT_CONNECTTIMEOUT] = 3; |
71
|
|
|
$defaults[CURLOPT_TIMEOUT] = 3; |
72
|
|
|
|
73
|
|
|
// disable 100-continue |
74
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); |
75
|
|
|
|
76
|
|
|
if ('https' == $protocol) { |
77
|
|
|
$defaults[CURLOPT_SSL_VERIFYPEER] = false; |
78
|
|
|
$defaults[CURLOPT_SSL_VERIFYHOST] = false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
curl_setopt_array($ch, (array)$options + $defaults); |
82
|
|
|
$ret = curl_exec($ch); |
83
|
|
|
$err = curl_error($ch); |
84
|
|
|
if (false === $ret || !empty($err)) { |
85
|
|
|
$errno = curl_errno($ch); |
86
|
|
|
$info = curl_getinfo($ch); |
87
|
|
|
curl_close($ch); |
88
|
|
|
return [ |
89
|
|
|
'ret' => false, |
90
|
|
|
'errno' => $errno, |
91
|
|
|
'msg' => $err, |
92
|
|
|
'info' => $info, |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
curl_close($ch); |
97
|
|
|
|
98
|
|
|
return [ |
99
|
|
|
'ret' => true, |
100
|
|
|
'msg' => $ret, |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* 异步发送一个请求 |
106
|
|
|
* @param string $url 请求的链接 |
107
|
|
|
* @param mixed $params 请求的参数 |
108
|
|
|
* @param string $method 请求的方法 |
109
|
|
|
* @return boolean TRUE |
110
|
|
|
*/ |
111
|
|
|
public static function sendAsyncRequest($url, $params = [], $method = 'POST') |
112
|
|
|
{ |
113
|
|
|
$method = strtoupper($method); |
114
|
|
|
$method = $method == 'POST' ? 'POST' : 'GET'; |
115
|
|
|
//构造传递的参数 |
116
|
|
|
if (is_array($params)) { |
117
|
|
|
$post_params = []; |
118
|
|
|
foreach ($params as $k => &$v) { |
119
|
|
|
if (is_array($v)) { |
120
|
|
|
$v = implode(',', $v); |
121
|
|
|
} |
122
|
|
|
$post_params[] = $k . '=' . urlencode($v); |
123
|
|
|
} |
124
|
|
|
$post_string = implode('&', $post_params); |
125
|
|
|
} else { |
126
|
|
|
$post_string = $params; |
127
|
|
|
} |
128
|
|
|
$parts = parse_url($url); |
129
|
|
|
//构造查询的参数 |
130
|
|
|
if ($method == 'GET' && $post_string) { |
131
|
|
|
$parts['query'] = isset($parts['query']) ? $parts['query'] . '&' . $post_string : $post_string; |
132
|
|
|
$post_string = ''; |
133
|
|
|
} |
134
|
|
|
$parts['query'] = isset($parts['query']) && $parts['query'] ? '?' . $parts['query'] : ''; |
135
|
|
|
//发送socket请求,获得连接句柄 |
136
|
|
|
$fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $errno, $errstr, 3); |
137
|
|
|
if (!$fp) { |
|
|
|
|
138
|
|
|
return false; |
139
|
|
|
} |
140
|
|
|
//设置超时时间 |
141
|
|
|
stream_set_timeout($fp, 3); |
142
|
|
|
$out = "{$method} {$parts['path']}{$parts['query']} HTTP/1.1\r\n"; |
143
|
|
|
$out .= "Host: {$parts['host']}\r\n"; |
144
|
|
|
$out .= "Content-Type: application/x-www-form-urlencoded\r\n"; |
145
|
|
|
$out .= "Content-Length: " . strlen($post_string) . "\r\n"; |
146
|
|
|
$out .= "Connection: Close\r\n\r\n"; |
147
|
|
|
if ($post_string !== '') { |
148
|
|
|
$out .= $post_string; |
149
|
|
|
} |
150
|
|
|
fwrite($fp, $out); |
151
|
|
|
//不用关心服务器返回结果 |
152
|
|
|
//echo fread($fp, 1024); |
153
|
|
|
fclose($fp); |
154
|
|
|
return true; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* 发送文件到客户端 |
159
|
|
|
* @param string $file |
160
|
|
|
* @param bool $delaftersend |
161
|
|
|
* @param bool $exitaftersend |
162
|
|
|
*/ |
163
|
|
|
public static function sendToBrowser($file, $delaftersend = true, $exitaftersend = true) |
164
|
|
|
{ |
165
|
|
|
if (file_exists($file) && is_readable($file)) { |
166
|
|
|
header('Content-Description: File Transfer'); |
167
|
|
|
header('Content-Type: application/octet-stream'); |
168
|
|
|
header('Content-Disposition: attachment;filename = ' . basename($file)); |
169
|
|
|
header('Content-Transfer-Encoding: binary'); |
170
|
|
|
header('Expires: 0'); |
171
|
|
|
header('Cache-Control: must-revalidate, post-check = 0, pre-check = 0'); |
172
|
|
|
header('Pragma: public'); |
173
|
|
|
header('Content-Length: ' . filesize($file)); |
174
|
|
|
ob_clean(); |
175
|
|
|
flush(); |
176
|
|
|
readfile($file); |
177
|
|
|
if ($delaftersend) { |
178
|
|
|
unlink($file); |
179
|
|
|
} |
180
|
|
|
if ($exitaftersend) { |
181
|
|
|
exit; |
|
|
|
|
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|