php中http请求
Easul Lv4
折叠代码块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
<?php
function httpRequest($url, $method, $headers, $body=NULL){
$ch = curl_init();

// 设置请求url
curl_setopt($ch, CURLOPT_URL, $url);
// 设置请求方法
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
// 设置请求体
switch ($method) {
case "POST":
if ($body != NULL){
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$bodyLength = strlen($body);
$headers[] = "content-length: $bodyLength";
}
default:
break;
}
// 设置请求头
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// 返回文件流
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 设置数据编码
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
// 返回响应头,false为不返回
curl_setopt($ch, CURLOPT_HEADER, TRUE);
// 返回响应体
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
// 连接结束后保存cookie信息到某个文件。
// curl_setopt($ch, CURLOPT_COOKIEJAR, "test.cookie");
// 请求时读取cookie信息的文件
// curl_setopt($ch, CURLOPT_COOKIEFILE, "test.cookie");
// 注意,毫秒超时一定要设置这个
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
// 超时毫秒,cURL 7.16.2中被加入。从PHP 5.2.3起可使用
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 300);
// 抓取https数据时设置如下两个选项,防止抓取失败
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
// 设置socks5代理
// curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
// curl_setopt($ch, CURLOPT_PROXY, PROXY_URL);
$resp = curl_exec($ch);

// 获取响应码
$respStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 获取响应头长度
$respHeaderSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
// 获取响应头
$respHeaders = substr($resp, 0, $respHeaderSize);
// 获取响应体
$respBody = substr($resp, $respHeaderSize);
// 错误码为0,则正常
curl_errno($ch);
curl_close($ch);

return array(
"status"=>$respStatus,
"headers"=> headerStrHandler($respHeaders),
"body"=>$respBody
);
}

function headerStrHandler($headerStr) {
$headerLines = explode("\n", $headerStr);
$headers = array();
foreach($headerLines as $headerLine) {
$header = explode(": ", $headerLine);
if (count($header) > 1) {
$headers[trim($header[0])] = trim($header[1]);
}
}
return $headers;
}

// 对于该链接http://localhost:3001/test.php?asdf=sadf&asd1=11#asd
// /test.php?asdf=sadf&asd1=11
// echo $_SERVER["REQUEST_URI"];
// 所有参数可以这样查看
// var_dump($_SERVER);
$url = "https://www.baidu.com";
$method = $_SERVER["REQUEST_METHOD"];
$headers = array();
foreach (getallheaders() as $key => $value) {
if (strtolower($key) == "accept") {
$headers["accept"] = $value;
}
if (strtolower($key) == "content-type") {
$headers[] = "content-type: $value";
}
}
$headers[] = "user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 UOS";
// 可以直接组装数据
// $body = "wd=hello&type=1";
// 也可以从文件流获取
$body = file_get_contents("php://input");

if ($method == "OPTIONS") {
http_response_code(200);
header("access-control-allow-headers: *");
header("access-control-allow-origin: *");
header("content-type: text/html");
die();
}

http_response_code(200);
header("access-control-allow-headers: *");
header("access-control-allow-origin: *");
header("content-type: application/json; charset=utf-8");
$response = httpRequest($url, $method, $headers, $body);

echo $response["body"];

常用函数参考

 评论
来发评论吧~
Powered By Valine
v1.5.2