目标站:http://218.25.58.44/searchsys/我的源码如下:现在问题是:返回结果“验证码错误”,也就是验证码失效了。求指点!
我在CSDN上查阅了相关资料,但还是弄不太明白。说是应该带cookie一起post 。我主要对这个流程没太懂,如何来保证我取到的验证码是与cookie一致?目前验证码识别问题我已经成功了。
多谢高手指点!
我在CSDN上查阅了相关资料,但还是弄不太明白。说是应该带cookie一起post 。我主要对这个流程没太懂,如何来保证我取到的验证码是与cookie一致?目前验证码识别问题我已经成功了。
多谢高手指点!
至于cookie 打开页面的时候读取保存 发送的时候带上 具体的可以看 curl的相关参数.
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
$cookiefile:保存cookie的文件名发送数据到目标页时,需要携带cookie
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
因为验证码是保存在 session 中的,并将 sessionid 放在 cookie 里提交时绝不可以再去读cookie,读了将可能改变 sessionid
而必须将先前读到的cookie发送回去
跟你遇到过一样的问题 验证码识别函数每次都会再次读取一下那个验证码生成的地址 导致 提交的不一致..... 你试试当页面载入后的验证码 保存到临时文件夹 在调用临时文件夹里面识别验证码 在提交 试试
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
$output = curl_exec($ch) ;
echo $output;
curl_close($ch);
$cookiefile = realpath('123.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
$output = curl_exec($ch) 后,文件 123.txt 中保存有取回的 cookie
以后再 curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); 就可以将 cookie 传回去了我想我说的再清楚不过了
getGJWZResult('小型汽车','辽AQ007B','4300');//查询违章结果function get_yzm(){
$valite = new valite();
$valite->setImage("http://218.25.58.44/searchsys/img.jsp");
$valite->getHec();
$valite->filterInfo();
$valite->dealwithData();
$data = $valite->run();
$data = implode("",$data);
return $data;
}function getGJWZResult($car_type = '',$car_no = '',$car_checkcode = '',$x='',$y='') {
header("Content-type:text/html;charset=utf-8");
$remote_server = 'http://218.25.58.44/search/wzcxrs.action';
$post_string = 'wzcx.autotype='.$car_type.'&wzcx.autono='.$car_no.'&wzcx.memo='.$car_checkcode.'&rand='. get_yzm().'$x='.$x.'$y='.$y;
$output = post_by_curl($remote_server, $post_string);
echo $output;
//return trim(getcontentbetween('<td align="center" ><font size="5">','</font>',$output));
}function get_by_curl($remote_server){
$cookiefile = realpath('cookie.txt');
$ch = curl_init($remote_server);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);//存储cookies
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
$data = curl_exec($ch);
curl_close($ch);
//return $data;
}function post_by_curl($remote_server, $post_string) {
$cookiefile = realpath('cookie.txt');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); //使用获取的cookies
curl_setopt($ch, CURLOPT_URL, $remote_server);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
提交时再把这个cookie发回去(一并提交)就是说 http://218.25.58.44/searchsys/img.jsp 的cookie要得到
1、查询页面 http://218.25.58.44/searchsys/ 已设置了 sessionid (Set-Cookie: ....; Path=/)
所以你需要先访问此 url,并获取 cookie
2、验证码图片 http://218.25.58.44/searchsys/img.jsp
从这个 url 获取图片流时,需将第1步读取的 cookie 发过去,不然将因为没有收到 sessionid 而产生新的 sessionid
3、表单处理 http://218.25.58.44/searchsys/search/wzcxrs.action
向此 url 发送数据时,一定要将第1步读取的 cookie 一并发过去
echo $yzm = get_yzm(get_yzm_with_cookie("http://218.25.58.44/searchsys/img.jsp"));getGJWZResult('小型汽车','辽AQ007B','4300',$yzm);//查询违章结果function get_yzm_with_cookie($remote_server){
$cookiefile = realpath('cookie.txt');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); //使用获取的cookies
curl_setopt($ch, CURLOPT_URL, $remote_server);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}function get_yzm(){
$valite = new valite();
$valite->setImage("http://218.25.58.44/searchsys/img.jsp");
$valite->getHec();
$valite->filterInfo();
$valite->dealwithData();
$data = $valite->run();
$data = implode("",$data);
return $data;
}function getGJWZResult($car_type = '',$car_no = '',$car_checkcode = '',$yzm='') {
header("Content-type:text/html;charset=utf-8");
$remote_server = 'http://218.25.58.44/search/wzcxrs.action';
$post_string = 'wzcx.autotype='.$car_type.'&wzcx.autono='.$car_no.'&wzcx.memo='.$car_checkcode.'&rand='. $yzm;
$output = post_by_curl($remote_server, $post_string);
echo $output;
//return trim(getcontentbetween('<td align="center" ><font size="5">','</font>',$output));
}function get_by_curl($remote_server){
$cookiefile = realpath('cookie.txt');
$ch = curl_init($remote_server);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);//存储cookies
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
$data = curl_exec($ch);
curl_close($ch);
//return $data;
}
echo $yzm = get_yzm(get_yzm_with_cookie("http://218.25.58.44/searchsys/img.jsp"));getGJWZResult('小型汽车','辽AQ007B','4300',$yzm);//查询违章结果function get_yzm_with_cookie($remote_server){
$cookiefile = realpath('cookie.txt');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); //使用获取的cookies
curl_setopt($ch, CURLOPT_URL, $remote_server);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}function get_yzm($yzm){
$valite = new valite();
$valite->setImage($yzm);
$valite->getHec();
$valite->filterInfo();
$valite->dealwithData();
$data = $valite->run();
$data = implode("",$data);
return $data;
}function getGJWZResult($car_type = '',$car_no = '',$car_checkcode = '',$yzm='') {
header("Content-type:text/html;charset=utf-8");
$remote_server = 'http://218.25.58.44/search/wzcxrs.action';
$post_string = 'wzcx.autotype='.$car_type.'&wzcx.autono='.$car_no.'&wzcx.memo='.$car_checkcode.'&rand='. $yzm;
$output = post_by_curl($remote_server, $post_string);
echo $output;
//return trim(getcontentbetween('<td align="center" ><font size="5">','</font>',$output));
}function get_by_curl($remote_server){
$cookiefile = realpath('cookie.txt');
$ch = curl_init($remote_server);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);//存储cookies
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
$data = curl_exec($ch);
curl_close($ch);
//return $data;
}function post_by_curl($remote_server, $post_string) {
$cookiefile = realpath('cookie.txt');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); //使用获取的cookies
curl_setopt($ch, CURLOPT_URL, $remote_server);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
但从 $valite->setImage("http://218.25.58.44/searchsys/img.jsp"); 就可知道是又发起了一次独立的 http 请求,因此 sessionid 就被改变了
从这个 url 获取图片流时,需将第1步读取的 cookie 发过去”
让他可以接受图片数据,而不是 url
我的识别验证码的函数的参数应该是个url或path+filename,但我不知道怎么用curl取得验证码(我用curl get http://218.25.58.44/searchsys/img.jsp根本就得不到图片类型的验证码)并得到cookie
那么为什么抓取图片时就没有呢
{
public function setImage($Image)
{
$this->ImagePath = $Image;
} public function getData()
{
return $this->data;
}