PHP 模拟登录出现问题

___________超 2012-04-28 09:26:16
在网上看了一编文章模拟人人网登录,恰好现在需要用到,但是出现了 “The URL has moved here” 是什么原因啊。谢谢,代码如下:

$login_url = 'http://passport.renren.com/PLogin.do';

$post_fields['email'] = 'xxx@gmail.com';
$post_fields['password'] = 'xxx';
$post_fields['origURL'] = 'http://www.renren.com/indexcon';
$post_fields['domain'] = 'renren.com';
//cookie文件存放在网站根目录的temp文件夹下
$cookie_file = tempnam('./temp','cookie');

$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_exec($ch);
curl_close($ch);

//带上cookie文件,访问人人网首页
$send_url='http://home.renren.com/Home.do';
$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$contents = curl_exec($ch);
curl_close($ch);

//清理cookie文件
unlink($cookie_file);

//输出人人网首页的内容
print_r($contents);
...全文
173 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
黄袍披身 2012-04-28
  • 打赏
  • 举报
回复
实际上已经登录了,只是还有一个跳转地址需要取得.
解决方法在后面已经描述,在cookie中取得地址然后再GET一次.

没有人人帐号,也没去过人人网.我这还一堆代码要敲,测试的事情 实在抱歉。
___________超 2012-04-28
  • 打赏
  • 举报
回复
是不是我的代码写的有问题,帮我试试啊。谢谢
黄袍披身 2012-04-28
  • 打赏
  • 举报
回复
以下内容转贴.

这两天模拟浏览器提交登录表单,提交完成后, The URL has movedhere 。。

每次都需要点击连接后才打开登录后的主页。这并不是我想要的。

又看了一遍说明,看到参数-L

如下:-L/--location Follow Location: hints (H)
--location-trusted Follow Location: and send auth to other hosts (H)

有点冲动,感觉这就是问题的关键 :

curl --output "./rr.html" --dump-header "d_cookie01" --cookie-jar "c_cookie01" --create-dirs --location --data "email=test&password=test&autoLogin=ture&origURL=&domain=renren.com&formName=&method=&isplogin=true&submit=登录" http://www.renren.com/PLogin.do

wowowo登录后的主页下载下来了!!



仔细查看d_cookie01(包含从c_cookie01的内容,但--cookie-jar 产生的更简洁)有这么一条

Location:http://www.renren.com/callback.do?t=9d5201d04d44156fb070037e9493f5fd3&origURL=http%3A%2F%2Fwww.renren.com%2FHome.do&needNotify=false

原来每次提交登录表单后还需要跳转的,而-L 则是跟随跳转链接的。



我们也可以分成两步去做:

1.先提交表单,保存返回来的cookie
curl --output "./rr.html" --dump-header "d_cookie01" --data "email=test&password=test&autoLogin=ture&origURL=& domain=renren.com&formName=&method=&isplogin=true& submit=登录" http://www.renren.com/PLogin.do
2.在d_cookie01中找到location,然后把cookie和location一起提交
curl --output "./rr.html" --dump-header "d_cookie01" --location http://www.renren.com/callback.do?t=9d5201d04d44156fb070037e9493f5fd3&origURL=http%3A%2F%2Fwww.renren.com%2FHome.do&needNotify=false

OK,就拿到了登录后的主页。
qq120848369 2012-04-28
  • 打赏
  • 举报
回复
这样还好,有的还用JS给密码加密什么的,很蛋疼。
黄袍披身 2012-04-28
  • 打赏
  • 举报
回复
干嘛要自己弄这些
人人网不是有API可以调用嘛,直接使用官方的接口不就好了。

http://wiki.dev.renren.com/wiki/PHP

另外有人提供了SDK
http://dev.renren.com/blog/239
___________超 2012-04-28
  • 打赏
  • 举报
回复
帮忙看看,已经弄了挺长时间的
___________超 2012-04-28
  • 打赏
  • 举报
回复
不太明白上面写的,太乱了,我重新写了一下,这次点击
The URL has moved here 可以进入首页,但是还是自动进不了
版主帮我看看,只要复制一下代码就可以了。


$cookie_file = tempnam('./temp','cookie');
$login_url = 'http://passport.renren.com/PLogin.do';
$post_fields = 'key_id=1&domain=renren.com&origURL=http://www.renren.com/home&email=xxx&password=xxx';

$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_exec($ch);
curl_close($ch);

$url = 'http://guide.renren.com/guide';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$contents = curl_exec($ch);
curl_close($ch);

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧