Alt text

teacher_curl可以做什么?

teacher_curl是一个PHP在网络请求的辅助方法!可以帮助我们更快捷的使用PHP自带的curl网络请求。
它由教书先生在码云上传!原项目地址

先奉上工具类代码:

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
function teacher_curl($url, $paras = array())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if (@$paras['Header']) {
$Header = $paras['Header'];
} else {
$Header[] = "Accept:*/*";
$Header[] = "Accept-Encoding:gzip,deflate,sdch";
$Header[] = "Accept-Language:zh-CN,zh;q=0.8";
$Header[] = "Connection:close";
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $Header);
if (@$paras['ctime']) { // 连接超时
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $paras['ctime']);
} else {
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
}
if (@$paras['rtime']) { // 读取超时
curl_setopt($ch, CURLOPT_TIMEOUT, $paras['rtime']);
}
if (@$paras['post']) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $paras['post']);
}
if (@$paras['header']) {
curl_setopt($ch, CURLOPT_HEADER, true);
}
if (@$paras['cookie']) {
curl_setopt($ch, CURLOPT_COOKIE, $paras['cookie']);
}
if (@$paras['refer']) {
if ($paras['refer'] == 1) {
curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
} else {
curl_setopt($ch, CURLOPT_REFERER, $paras['refer']);
}
}
if (@$paras['ua']) {
curl_setopt($ch, CURLOPT_USERAGENT, $paras['ua']);
} else {
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
}
if (@$paras['nobody']) {
curl_setopt($ch, CURLOPT_NOBODY, 1);
}
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (@$paras['GetCookie']) {
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
preg_match_all("/Set-Cookie: (.*?);/m", $result, $matches);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $headerSize); //状态码
$body = substr($result, $headerSize);
$ret = [
"Cookie" => json_encode($_COOKIE), "body" => $body, "header" => $header, 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE)
];
curl_close($ch);
return $ret;
}
$ret = curl_exec($ch);
if (@$paras['loadurl']) {
$Headers = curl_getinfo($ch);
$ret = $Headers['redirect_url'];
}
curl_close($ch);
return $ret;
}

teacher_curl怎么使用?

请看如下例子:

1
string teacher_curl(string $url ,array $paras){}

在一般情况下teacher_curl会给你一个字符串类型返回值(特殊需求可以强转

GET方法请求

1
echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com");

POST方法请求(两种方式

1
2
3
4
5
echo teacher_curl("https://api.oioweb.cn/api/beian.php",[    
'post'=>[
'url'=>'qq.com'
]
]);
1
2
3
echo teacher_curl("https://api.oioweb.cn/api/beian.php",[    
'post'=>'url=qq.com'
]);

携带Cookie访问

1
2
3
echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com",[    
'cookie'=>'cookie内容'
]);

在POST请求时携带Cookie

1
2
3
4
5
6
echo teacher_curl("https://api.oioweb.cn/api/beian.php",[
'cookie'=>'cookie content'
'post'=>[
'url'=>'qq.com'
]
]);

模拟访问来源Refer

1
2
3
echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com",[    
'refer'=>'
]);

模拟UA

1
2
3
echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com",[    
'ua'=>'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
]);

上传文件

1
2
3
4
5
echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com",[    
'post'=>[
'file'=>new CURLFile(realpath("Curl.jpg"))
]
]);
1
2
3
echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com",[    
'post'=>new CURLFile(realpath("Curl.jpg"))
]);

获取301跳转后地址

1
2
3
echo teacher_curl("https://mmbizurl.cn/s/RNHSo6Dek",[
'loadurl'=>1
]);

查看返回Header信息

1
2
3
echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com",[    
'header'=>1]
);

设置请求头信息

1
2
3
4
5
6
7
8
echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com",[    
'Header'=>[
'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9
cache-control: max-age=0'
]
]);

当我们需要模拟登陆的时候就需要获取返回cookie和header,为$paras添加GetCookie为1返回值则为header&cookie

获取POST以后返回的Header、Body、Cookie内容

1
2
3
4
5
6
7
echo teacher_curl("https://api.oioweb.cn/api/beian.php?url=qq.com",[    
'post'=>[
'user'=>123456,
'pwd'=>123
],
'GetCookie'=>1
]);