Heim >Backend-Entwicklung >PHP-Tutorial >php模拟登陆163邮箱失败,求解
<?phpfunction curl($url,$cookiefile=0,$cookiejar=0,$httpheader=0,$referer="http://baidu.com",$follow=0,$header=1,$postdata=0,$cookiearr=ARRAY(),$proxy=0,$outtime=20000,$UA=0){ $ch = curl_init(); curl_setopt ( $ch, CURLOPT_NOSIGNAL,true);//开启毫秒超时 curl_setopt($ch, CURLOPT_TIMEOUT_MS, $outtime);//10s超时 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_URL,$url) ; if($UA==0){ curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36'); }else{ curl_setopt($ch, CURLOPT_USERAGENT,$UA); } curl_setopt($ch, CURLOPT_REFERER, $referer); //伪装REFERER curl_setopt($ch, CURLOPT_HEADER, $header); if($httpheader){ curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ( $ch, CURLOPT_COOKIESESSION, true ); if($follow){ curl_setopt($ch, CURLOPT_FOLLOWLOCATION,$follow);//跟随重定向 } //读取cookie //foreach($cookiearr as $c){ //echo $c; // curl_setopt($ch,CURLOPT_COOKIE,$cookiearr[0]); //} if($cookiefile){ curl_setopt($ch, CURLOPT_COOKIEFILE,$cookiefile); } if($postdata){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); }else{ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); } curl_setopt($ch, CURLINFO_HEADER_OUT, true);//开启返回请求头查看 //保存cookie if($cookiejar){ curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar); } //代理 if($proxy){ curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式 curl_setopt($ch, CURLOPT_PROXY, $proxy['ip']); //代理服务器地址 curl_setopt($ch, CURLOPT_PROXYPORT,$proxy['port']); //代理服务器端口 curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); //使用http代理模式 } $content=curl_exec($ch); if($content === false){ $content= 'Curl error: ' . curl_error($ch); } $info=curl_getinfo($ch); $result[1]=$content; $result[0]=$info; curl_close($ch); return $result;}//首次访问mail.163.com没有产生任何cookie/$url="https://mail.163.com/entry/cgi/ntesdoor?df=mail163_letter&from=web&funcid=loginone&iframe=1&language=-1&passtype=1&product=mail163&net=t&style=-1&race=468_528_417_gz&uid=qianxi770231@163.com";//貌似race参数是记录来源 $httpheader = array( 'Cookie: starttime=1454514129882; logType=; nts_mail_user=qianxi770231:-1:1; df=mail163_letter', );//貌似starttime就是js里面的gettiem();//$postdata['savelogin']=1;$postdata['url2']="http://mail.163.com/errorpage/error163.htm";$postdata['username']="qianxi770231";//这邮箱可以使用$postdata['password']="ziyanpuye";$cookie ="cookie.txt";$content= curl($url,0,$cookie,$httpheader,"http://mail.163.com/",0,1,$postdata);print_r($content);// echo $content[1]; $fp = @fopen("Log.html", "w"); //记录捕获到的页面源码fwrite($fp,$content[1]); fclose($fp);
参考:
<?php header("Content-Type: text/html; charset=UTF-8"); error_reporting(0); /** * 登陆 * $user 163用户名 * $pass 密码 **/ function login($user,$pass){ //登陆 $url = 'http://reg.163.com/logins.jsp?type=1&url=http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D-1'; $cookie = tempnam('./cache/','~');//创建一个用于存放cookie信息的临时文件 $fields_post = array( 'username' => $user, 'password' => $pass, 'verifycookie' => 1, 'style' => -1, 'product' => 'mail163', 'selType' => -1, 'secure' => 'on' ); $fields_string = ''; foreach($fields_post as $key => $value){ $fields_string .= $key . '=' . $value . '&'; } $fields_string = rtrim($fields_string , '&'); $headers = array( 'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0', 'Referer' => 'http://www.163.com' ); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//返回结果存放在变量中,而不是默认的直接输出 curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);//关闭连接时,将服务器端返回的cookie保存在以下文件中 curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); $result= curl_exec($ch); curl_close($ch); preg_match_all('/<div class="info" id="eHint">(.*?) <\/div>/i', $result,$infos,PREG_SET_ORDER); if(!empty($infos['0']['1'])){ unlink($cookie); exit('<script type="text/javascript">alert("'.$infos['0']['1'].'");history.go(-1);</script>'); }else{ $G_ROOT = dirname(__FILE__); file_put_contents($G_ROOT.'/cache/cookie', $cookie); return $cookie; } } /** * * $data['url'] 请求地址 * $data['data_post'] post数据 * $data['cookie'] * **/ function curl($data){ $url = $data['url']; $data_post= $data['data_post']? $data['data_post']: false; $cookie = $data['cookie']; $headers = array( 'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0', 'Referer' => 'http://www.163.com' ); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //cookie文件 登陆之后 //POST 提交 if($data_post){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_post); } $result = curl_exec($ch); curl_close($ch); return $result; }