有的时候我们会用curl模拟提交一些ajax需要提交的数据,其中有的是他自己构建的一些方法,所以呢,就需要我们自己来构建header头来完全模拟,在本次demo中,有提到json,因为有的使用js或者jquery来转化为json的其中,中文不会转义,而代码会,这个参数是在php5.4之后才支持的,大家切记。还有就是这是一个模拟登陆aspx类型的网站,其实原理是一样的,最重要的就是分析好他需要什么参数和数据,而我们怎样构建、获得和组合,万变不离其宗,相信大家都可以的。。。
$cookie_file=dirname(__FILE__)."/cookie.tmp";
$cookie_files=dirname(__FILE__)."/cookies.tmp";
$headers_login=array("User-Agent"=>"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36");
$post_url="http://post.globrand.com/member/UserLogin.aspx";
$post=array("BaseTextBox1"=>"nmdr88","BaseTextBox2"=>"nmdr880217","__VIEWSTATE"=>"/wEPDwUJNjUzMTkwMTE0ZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAQUOTXlJbWFnZUJ1dHRvbjFg33EbGTscw5RfbydKFvsibpHfug==","__VIEWSTATEGENERATOR"=>"9B5805F3","MyImageButton1.x"=>0,"MyImageButton1.y"=>0);
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,"http://post.globrand.com/member/signin.html");
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_COOKIEJAR,$cookie_file);
curl_setopt($curl,CURLOPT_PROXY,'127.0.0.1:8888');
$contents=curl_exec($curl);
curl_close($curl);
var_dump($contents);die;
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,$post_url);
curl_setopt($curl,CURLOPT_HTTPHEADER,$headers_login);
curl_setopt($curl,CURLOPT_HEADER,0);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_TIMEOUT,30);
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($curl,CURLOPT_COOKIEJAR,$cookie_files);
curl_setopt($curl,CURLOPT_COOKIEFILE,$cookie_file);
curl_setopt($curl,CURLOPT_PROXY,'127.0.0.1:8888');
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
$result=curl_exec($curl);
curl_close($curl);
var_dump($result);die;
$add_url="http://post.globrand.com/ajaxpro/MemberAjax,App_Code.ashx";
$add["Infor"] =array("阿斯达",'
速度
$add_json=json_encode($add,JSON_UNESCAPED_UNICODE);
$headers=array();
$headers[] ='AjaxPro-Method: AddWare';
$headers[] ='Accept-Encoding: gzip, deflate';
$headers[] ='Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3';
$headers[] ='Cache-Control: no-cache';
$headers[] ='Content-Type: application/x-www-form-urlencoded; charset=utf-8';
$headers[] ='User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0';
$headers[] ='Connection: keep-alive';
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//模拟post提交数据进行登录
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,$add_url);
curl_setopt($curl,CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl,CURLOPT_HEADER,0);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_TIMEOUT,30);
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($curl,CURLOPT_COOKIEJAR,$cookie_file);
curl_setopt($curl,CURLOPT_COOKIEFILE,$cookie_files);
curl_setopt($curl,CURLOPT_PROXY,'127.0.0.1:8888');
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$add_json);
$result=curl_exec($curl);
$error=curl_error($curl);
curl_close($curl);
var_dump($error);