php_curl模拟登录有验证码实例

<?php
/**
* @author 追逐__something
* @version $id
*/
define('SCRIPT_ROOT',dirname(__FILE__).'/');
$act = trim($_REQUEST['act']);
switch($act)
{
case 'login':
// 获取验证码
$code = trim($_REQUEST['code']);

// $loginParams为curl模拟登录时post的参数
$loginParams['act'] = 'login';
$loginParams['password'] = '111111';
$loginParams['username'] = 'wlpadmin';
$loginParams['vcode'] = $code;

// $cookieFile 为加载验证码时保存的cookie文件名
$cookieFile = SCRIPT_ROOT.'cookie.tmp';

// $targetUrl curl 提交的目标地址
$targetUrl = 'http://local.xx.com/admin/login.php';

// 参数重置
$content = curlLogin($targetUrl, $cookieFile, $loginParams);
echo $content;
break;
case 'authcode':
// Content-Type 验证码的图片类型
header('Content-Type:image/png');
showAuthcode('http://local.xx.com/admin/vcode.php?r=0.03192671708666017');
exit;
break;
}

/**
* 模拟登录
* @param string $url 提交到的地址
* @param string $cookieFile 保存cookie的文件
* @param string $loginParams 提交时要post的参数
* @return string $content 返回的内容
*/
function curlLogin($url, $cookieFile, $loginParams)
{
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_COOKIEFILE, $cookieFile); //同时发送Cookie
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $loginParams); //提交查询信息
$content = curl_exec($ch);
curl_close($ch);
return $content;
}

/**
* 加载目标网站图片验证码
* @param string $authcode_url 目标网站验证码地址
*/
function showAuthcode( $authcode_url )
{
$cookieFile = SCRIPT_ROOT.'cookie.tmp';
$ch = curl_init($authcode_url);
curl_setopt($ch,CURLOPT_COOKIEJAR, $cookieFile); // 把返回来的cookie信息保存在文件中
curl_exec($ch);
curl_close($ch);
}
?>
<iframe src="?act=authcode" style='width: 100px; height:40px ' frameborder=0 ></iframe>
<form>
<input type="hidden" name="act" value="login">
<input type="text" name="code" />
<input type="submit" name="submit" >
</form>

 

本文提供了一个程序实例,思路就是获取到验证码之后把验证码存储为一个图片,然后程序休眠20秒,在20秒之后由用户手动查看图片,并把验证码填写到code.txt文件中,20秒休眠完成后,程序会读code.txt的验证码,这样再带着验证码进行登录操作。具体代码如下:

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,开始做你想做的事吧。。。。。

 

posted @ 2015-12-08 13:11  爱你爱自己  阅读(308)  评论(0编辑  收藏  举报