curl命令

命令行操作

1
2
3
4
5
#get
curl http://localhost/index.php?a=1&b=2
#post
curl -d "a=1&b=2" http://localhost/index.php

php扩展操作

get请求

1
2
3
4
5
6
7
8
9
10
11
12
//创建curl资源
$curl = curl_init();
//设定url及相应选项
curl_setopt($curl, CURLOPT_URL, $url); //设定访问地址
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //1 将获取的信息以文件流形式返回 0直接输出
//抓取内容并返回
$contents = curl_exec($curl);
//关闭资源
curl_close($curl);

post请求

1
2
3
4
5
6
7
8
9
$curl = curl_init();
$post = 'name=lilei&age=18';
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1); //设定为POST请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $post); //POST数据
$contents = curl_exec($curl);

cookie操作

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
//获取cookie
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
$contents = curl_exec($curl); //返回抓取的内容
----------------------------------------------------------
//使用已有cookie
$curl = curl_init();
$timeout = 5;
$cookie_file = "./tmp.cookie";
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_COOKIEJAR,$cookie_file); //使用cookie文件
$contents = curl_exec($curl);
-----------------------------------------------------------
//使用已有cookie并存储 (建议直接使用这种写法,以免漏掉某些关键cookie信息)
$curl = curl_init();
$timeout = 5;
$cookie_file = "./tmp.cookie";
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_COOKIEJAR,$cookie_file); //使用cookie文件
curl_setopt($curl, CURLOPT_COOKIEFILE,$cookie_file); //存储COOKIE
$contents = curl_exec($curl);

https请求

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
$cacert = getcwd() . '/cacert.pem'; //CA根证书
$SSL = substr($url, 0, 8) == "https://" ? true : false;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout-2);
if ($SSL && $CA) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 只信任CA颁布的证书
curl_setopt($ch, CURLOPT_CAINFO, $cacert); // CA根证书(用来验证的网站证书是否是CA颁布)
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名,并且是否与提供的主机名匹配
} else if ($SSL && !$CA) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); // 检查证书中是否设置域名
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); //避免data数据过长问题
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); //data with URLEncode
$ret = curl_exec($ch);
//var_dump(curl_error($ch)); //查看报错信息
curl_close($ch);

应用

模拟验证码登陆

原地址

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
/**
* 模拟登录
*/
//初始化变量
$cookie_file = "tmp.cookie";
$login_url = "http://xxx.com/logon.php";
$verify_code_url = "http://xxx.com/verifyCode.php";
echo "正在获取COOKIE...\n";
$curlj = curl_init();
$timeout = 5;
curl_setopt($curl, CURLOPT_URL, $login_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_COOKIEJAR,$cookie_file); //获取COOKIE并存储
$contents = curl_exec($curl);
curl_close($curl);
echo "COOKIE获取完成,正在取验证码...\n";
//取出验证码
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $verify_code_url);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$img = curl_exec($curl);
curl_close($curl);
$fp = fopen("verifyCode.jpg","w");
fwrite($fp,$img);
fclose($fp);
echo "验证码取出完成,正在休眠,20秒内请把验证码填入code.txt并保存\n";
//停止运行20秒
sleep(20);
echo "休眠完成,开始取验证码...\n";
$code = file_get_contents("code.txt");
echo "验证码成功取出:$code\n";
echo "正在准备模拟登录...\n";
$post = "username=maben&pwd=hahahaha&verifycode=$code";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
$result=curl_exec($curl);
curl_close($curl);
//这一块根据自己抓包获取到的网站上的数据来做判断
if(substr_count($result,"登录成功")){
echo "登录成功\n";
}else{
echo "登录失败\n";
exit;
}
//OK,开始做你想做的事吧。。。。。

资料

官方文档