php 使用cURL 模拟注册

整个过程注意cookie,本案例注册过程的验证码是通过一个HTML页面手动输入的。


ini_set("display_errors", "On");

error_reporting(E_ALL | E_STRICT);
/*
由7200跳转到开户流程
*/
$cookie_jar = dirname(__FILE__)."/cookie.txt";
$step2 = '';

$ch = curl_init($step2);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
$contents = curl_exec($ch);
// dump($contents);
curl_close($ch);

/*
由开户流程跳转到申请开户
*/
$step3 = '';
$ch = curl_init($step3);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
$contents3 = curl_exec($ch);
// dump($contents3);
// echo "$contents3";
curl_close($ch);

/*
由申请开户跳转到问卷
*/
//初始化
$ch = curl_init();
$post_data = 'ck=on&brokerId=7200';
$url = "";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//执行结果是否被返回,0是返回,1是不返回
curl_setopt($ch, CURLOPT_HEADER, 0);//参数设置,是否显示头部信息,1为显示,0为不显示
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
if($output === FALSE)
{
echo "
","cUrl Error:".curl_error($ch);
}
// echo $output;
curl_close($ch);

//获取cookie

// cookieInfo=jsonencode( _COOKIE);
// var_dump($cookieInfo);

// file=C:\wamp\www/cookie.txt;// file_pointer = fopen( file,a);//fwrite( file_pointer, cookieInfo);//fclose( file_pointer);

/*
由问卷跳转到照片实例
填写问卷 跳转地址
*/
$cookie_jar = dirname(__FILE__)."/cookie.txt";
$ch = curl_init();
$post_data =
'one=C&two=C&three=B&four=C&five=D&six=B&severn=A&eight=D&nine=C&ten=C&eleven=A&twelve=A&thirteen=C&fourteen=A&brokerId=7200';
$url = "";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//执行结果是否被返回,0是返回,1是不返回
curl_setopt($ch, CURLOPT_HEADER, 0);//参数设置,是否显示头部信息,1为显示,0为不显示
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$content4 = curl_exec($ch);
if($content4 === FALSE)
{
echo "
","cUrl Error:".curl_error($ch);
}
// echo $content4;
curl_close($ch);

/*
由照片实例跳转到个人信息--
*/
$ch = curl_init();
$post_data = 'brokerId=7200';
$url = "";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//执行结果是否被返回,0是返回,1是不返回
curl_setopt($ch, CURLOPT_HEADER, 0);//参数设置,是否显示头部信息,1为显示,0为不显示
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$content5 = curl_exec($ch);
if($content5 === FALSE)
{
echo "
","cUrl Error:".curl_error($ch);
}
// echo $content5;die;
curl_close($ch);

//取出验证码
$verify_code_url = "";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $verify_code_url);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);
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);
// header();
$link = 'http://localhost/zhuce2.php';
header('location:' . $link);

zhuce2.php

<?php
verifycodeurl=; cookie_jar = dirname(FILE).”/cookie.txt”;

curl=curlinit();curlsetopt( curl, CURLOPT_URL, verifycodeurl);curlsetopt( curl, CURLOPT_COOKIEFILE, cookiejar);curlsetopt( curl, CURLOPT_HEADER, 0);
curl_setopt( curl,CURLOPTRETURNTRANSFER,1); img = curl_exec($curl);

curl_close($curl);

fp=fopen(verifyCode.jpg,w);fwrite( fp, img);fclose( fp);
?>









验证码:




zhuce3.php

/*
由个人信息跳转到—

*/
header(“Content-type: text/html; charset=utf-8”);
ini_set(‘display_errors’, ‘on’);
error_reporting(E_ALL);
yanzhengma= _GET[‘vcode’];

$cookie_jar = dirname(FILE).”/cookie.txt”;

//注: PHP 5.5.0起,文件上传建议使用CURLFile代替@
// var_dump($data);

$ch = curl_init();

// cfile=curlfilecreate(cats.jpg,image/jpeg,testname); url = ”;
$data = array(
‘name’ => ‘邬军胜’,
‘attach’ =>new CURLFile(‘D:\phpStudy\WWW/0.jpg’),
‘attachhou’ =>new CURLFile(‘D:\phpStudy\WWW/0.jpg’),
‘picyin’ =>new CURLFile(‘D:\phpStudy\WWW/0.jpg’),

'registeredPhoneNo' => '13033401116',
'cardType' => '1',
'cardNumber' => '533400199108119898',
'recommendBankCode' => '10',
'bankAccount' => '2344 3064 0020 1113',
'brokerId' => '7200',
'selectp' => '北京',
'address1' => '北京',
'address' => '北京',
'contactMan' => 'Tom',
'selectp' => '北京',
'type' => '3',
'sex' => '1',
'ContacterPhoneNo' => '13663012982',
'ReFreeAccount' => '23423434324230',
'ck' => 'on',
'yanzhengma' => $yanzhengma

);
curl_setopt( ch,CURLOPTURL, url);
curl_setopt( ch,CURLOPTRETURNTRANSFER,1);//01curlsetopt( ch, CURLOPT_HEADER, 0);//参数设置,是否显示头部信息,1为显示,0为不显示

curl_setopt( ch,CURLOPTCOOKIEFILE, cookie_jar);
curl_setopt( ch,CURLOPTREFERER,);//0curlsetopt( ch, CURLOPT_POST, 1);
curl_setopt( ch,CURLOPTPOSTFIELDS, data);
//执行并获取结果
add=curlexec( ch);
if( add===FALSE)echo,cUrlError:.curlerror($ch);//vardump( data);

// echo add;//cURLcurlclose( ch);
// var_dump($_POST);
// die;
//

/*
注册成功页

*/

ch=curlinit(); url = ”;
$cookie_jar = dirname(FILE).”/cookie.txt”;
data=array(//name=>,//firmID=>,//registeredPhoneNo=>13693600123,//cardType=>1,//cardNumber=>341182199407227603,//recommendBankCode=>10,//bankAccount=>2344546456541234,//brokerId=>7200,//selectp=>,//address1=>,//address=>,//contactMan=>Tom,//selectp=>,//type=>3,//email=>,//firmId=>,//ContacterPhoneNo=>13693600123,//ReFreeAccount=>23423434324230,ck=>on,//postCode=>); cookie_jar = dirname(FILE).”/cookie.txt”;
curl_setopt( ch,CURLOPTURL, url);
curl_setopt( ch,CURLOPTRETURNTRANSFER,0);//01curlsetopt( ch, CURLOPT_HEADER, 0);//参数设置,是否显示头部信息,1为显示,0为不显示
curl_setopt( ch,CURLOPTCOOKIEFILE, cookie_jar);
curl_setopt($ch, CURLOPT_REFERER,’http://www.wjybk.com‘);
curl_setopt( ch,CURLOPTPOST,1);curlsetopt( ch, CURLOPT_POSTFIELDS, data); content7 = curl_exec( ch);if( content7 === FALSE)
{
echo “
”,”cUrl Error:”.curl_error(ch);  
}  
// echo
content7;

curl_close($ch);
?>

fp=fopen(C(uploaddir)./okhtml/add..html,w+);fwrite( fp, add);fclose( fp);
在每一次curl获得结果时加这个,地址自己重改,这样就可以看每一步的结果。

你可能感兴趣的:(php 使用cURL 模拟注册)