当前位置:Gxlcms > PHP教程 > 爬人人网好友遇到的模拟登陆问题

爬人人网好友遇到的模拟登陆问题

时间:2021-07-01 10:21:17 帮助过:15人阅读

我用网上别人的模拟登陆代码
$username="xxx@126.com";//$_POST["username"];

$password="xxx";//$_POST["password"];

$message="1";//$_POST["message"];

if($message!=""){

updateRenren($message,$username,$password,0);

echo $message;

}

function updateRenren($status,$renren_username,$renren_password,$tried_times){
if($tried_times>=3){
return false;
}

$cookie_jar=tempnam('./cookie','renren');
$ch=&getCurl($cookie_jar,"POST","http://www.renren.com/ajaxLogin/login");
curl_setopt($ch,CURLOPT_POSTFIELDS,'email='.urlencode($renren_username).'&password='.urlencode($renren_password).'&origURL=http%3A%2F%2Fwww.renren.com%2FHome&domain=renren.com&key_id=1&captcha_type=web_login&_rtk=90ae5f12');
$str=&sendAndResult($ch);
$ch = &getCurl($cookie_jar,"GET","http://www.renren.com/123456");
$ret = &sendAndResult($ch);
}

function &getCurl($jar,$method,$url){
$ch=curl_init();
curl_setopt($ch,CURLOPT_COOKIEJAR,$jar);
curl_setopt($ch,CURLOPT_COOKIEFILE,$jar);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");
//curl_setopt($ch,CURLOPT_VERBOSE,true);
if($method=="POST"){
curl_setopt($ch,CURLOPT_POST,TRUE);
}else{
curl_setopt($ch,CURLOPT_POST,FALSE);
}
return $ch;
}

function &sendAndResult(&$ch){
$str=curl_exec($ch);
curl_close($ch);
unset($ch);
echo $str;
return $str;
}
我原以为模拟登陆后,就可以任意登陆我的人人主页或者查看好友,但当我
$URL="http://friend.renren.com/GetFriendList.do?curpage=0&id=382556764";
$fcontents=file_get_contents($URL);
echo"$fcontents";
又被弹回登陆界面了,请问有什么办法可以让我一直保持连接。
谢谢!


回复讨论(解决方案)

简单的 file_get_contents 不会传递 cookie
而既然是需要登陆的,那么 cookie 总是要传递的
继续使用的的 getCurl 呀

简单的 file_get_contents 不会传递 cookie
而既然是需要登陆的,那么 cookie 总是要传递的
继续使用的的 getCurl 呀
你意思是只要登陆一个页面就得还发一次cookie吗?
我以为和浏览器一样,我发了一次,后面的就不用发了。。。新手求拍。。

是的,和浏览器一样每次都要发送 cookie

是的,和浏览器一样每次都要发送 cookie
行,谢谢,本来想偷个懒 ,那每次发就每次发吧,反正写到函数里的。分给你了


是的,和浏览器一样每次都要发送 cookie
行,谢谢,本来想偷个懒 ,那每次发就每次发吧,反正写到函数里的。分给你了
谢谢版主,我人人网的写完了,现在要做微博的,我要怎么改啊
我觉得只需要该curl_setopt($ch,CURLOPT_POSTFIELDS,'email='.urlencode($renren_username).'&password='.urlencode($renren_password).'&origURL=http%3A%2F%2Fwww.renren.com%2FHome&domain=renren.com&key_id=1&captcha_type=web_login&_rtk=90ae5f12');
里面的第三个参数就行了,但新浪微博的要对应改成什么样呢。

人气教程排行