php模擬登陸的實現方法,這裡分別列舉兩種方法實現模擬登陸人人網。具體實例代碼如下:
(1)使用snoopy模擬登陸
<?php set_time_limit(0); require "Snoopy.class.php"; $snoopy=new Snoopy(); $snoopy->referer='http://www.renren.com/'; $snoopy->agent="Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0"; $submit_vars['email'] ='登陸賬號'; $submit_vars['password'] ='登陸密碼'; $url='http://www.renren.com/PLogin.do';//登陸數據提交的URL地址 $snoopy->submit($url,$submit_vars); $snoopy->fetch("http://www.renren.com/");//希望獲取的頁面數據 echo $snoopy->results;//www.phpernote.com
(2)使用curl模擬登陸
<?php set_time_limit(0); $cookie_file=tempnam('./tmp','cookie');//tmp目錄需要先建立好 $ch=curl_init(); $login_url='http://www.renren.com/PLogin.do'; $curlPost="email=登陸賬號&password=登陸密碼"; curl_setopt($ch,CURLOPT_URL,$login_url); //啟用時會將頭文件的信息作為數據流輸出 curl_setopt($ch,CURLOPT_HEADER,0); //設定是否輸出頁面內容 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_POST,1); //設置請求發送方式,post或get,CURLOPT_POST或CURLOPT_GET curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost); curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file); //保存cookie curl_exec($ch); curl_close($ch); $ch=curl_init(); $login_url2="http://www.renren.com/"; curl_setopt($ch,CURLOPT_URL,$login_url2); curl_setopt($ch,CURLOPT_HEADER,0); curl_setopt($ch,CURLOPT_RETURNTRANSFER,0); curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_file); //讀取cookie curl_exec($ch); curl_close($ch);