带验证码的curl登陆,总是报验证码错误;
目前有一个需求是去本地政府的养老保险系统查询抓取数据,但是这个系统是带验证码和隐藏域的,目前的思路是首先抓取登陆页的内容,把隐藏域的数据抓出来保存起来,验证码抓出来保存到本地,然后构建前台表单把隐藏域的内容和验证码绑定到前台页,最后提交模拟查询,但是每次都会报验证码错误。
抓数据的地址:http://www.massi.gov.cn:82/jdcy/ylcy/login.asp
前台html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>查询</title>
</head>
<body>
<form name="login" method="post" action="/chaxuntest/index.php/home/index/res" onsubmit="return _check();">
  <foreach item="input" name="input"  key="k">
            <input type="hidden" name="{$k}" value="{$input}">
        </foreach>
姓名:<input name="name" value=""  type="text" /><br/>
身份证号:<input name="idcard" value=""  type="text" /><br/>
验证码:<input name="code" type="text" /><br/>
<img src="/masokweixin/{$codename}"  >
<input type="submit" value="开始查  ">
</form>
</body>
</html>
后台:
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function Chaxun() {
$url = "http://www.massi.gov.cn:82/jdcy/ylcy/login.asp";
$content = get_curl ( $url, '', '' );
$input = jiequ ( $content, '<form name="login" method="post" action="query.asp?queryid=" onsubmit="return _check();">' );
foreach ( $input as $k => $v ) {
$input [$k] = iconv ( "gb2312", "UTF-8", $v );
}
unset ( $input ['aac003'] );
unset ( $input ['aac002'] );
unset ( $input ['checkid'] );
//var_dump ( $input );
$this->assign ( 'input', $input );
$url = "http://www.massi.gov.cn:82/jdcy/ylcy/code.asp";
$code = get_curl ( $url, '', '' );
$codename=uniqid().".jpg";
file_put_contents($codename,$code);
$this->assign('codename',$codename);
$this->display ();
}
public function res(){
$post['aac003'] = $_POST['name'];
$post['aac002'] = $_POST['idcard'];
$post['checkid'] = $_POST['code'];
$post['login'] = $_POST['login'];
                $post['reput'] = $_POST['reput'];
$post['todayquery'] = $_POST['todayquery'];
$post['allquery'] = $_POST['allquery'];
$post['modifytime'] = $_POST['modifytime'];
       $content1 = get_curl ( $url, $p, '' );
$u = U ( 'chaxun' );
if (strpos ( $content1, iconv ( "UTF-8", "gb2312", '验证码' ) )) {
echo "<script language=JavaScript>alert('验证码错误!');location.href='{$u}';</script>";
exit ();
}
if (strpos ( $content1, iconv ( "UTF-8", "gb2312", '没有' ) )) {
echo "<script language=JavaScript>alert('抱歉,没有找到您的信息,请检查是否输入正确的姓名及身份证号');location.href='{$u}';</script>";
exit ();

}
}
方法jiequ和get_curl
<?php
function jiequ($content,$form){ $str = explode($form, $content);
$str = explode("</form>", $str[1]);
$str = explode("<input", $str[0]);
unset($str[0]);
foreach($str as $key=>$value){
$cache = explode('name="', $value);
$cache = explode('"', $cache[1]);
$cache2 = explode('value="', $value);
$cache2 = explode('"', $cache2[1]);
$post[$cache[0]] = $cache2[0];
}
return $post;
}function get_curl($url,$post,$cookiefile,$jilu){
if($cookiefile != ''){
//echo $cookiefile;exit;
$openid = explode('/',$cookiefile);
$openid = explode('.txt',$openid['1']);
$openid = $openid['0'];
$cookie = M('cookie');
if($openid != ''){
$data = $cookie->where("openid='%s'",array($openid))->find(); if($data){
$cookies = $data['value'];
}else{
$data['openid'] = $openid;
$cookie->add($data);
}
}
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
if($post != ''){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
}
if($cookiefile != ''){
curl_setopt($ch,CURLOPT_COOKIE,$cookies);
//curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch,CURLOPT_REFERER,'http://jjzd.mas.gov.cn:8090/frmVehVio.aspx');
curl_setopt($ch, CURLOPT_HEADER, 1);
$content = curl_exec($ch);
curl_close($ch);
$a = explode("\r\n\r\n", $content);
$header = $a['0']; preg_match_all("/set\-cookie:([^\r\n]*)/is", $header, $matches);
$cookie_str = join('; ',$matches[1]);
unset($a['0']);
foreach($a as $k=>$v){
$body .= $v;
}
//echo $body;exit; if($cookie_str != '' and $jilu == ''){
$data['value'] = $cookie_str;
$cookie = M('cookie');
$cookie->save($data);
} //echo $cookie_str;exit;
//echo $content;exit;
return $body;
}else{
curl_setopt($ch, CURLOPT_HEADER, 0);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}}