当前位置:首页 > PHP教程 > php高级应用 > 列表

php curl模拟登陆人人网发表状态

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-22 11:29:28 浏览: 评论:0 

在php中要实现用户登录我们一般都会到curl模拟登陆功能,下面我就基于php的curl来实现登录人人网,完整的例子希望对各位有帮助,代码如下:

  1. <?php 
  2. $cookie_file = dirname(__FILE__)."/renren.cookie"
  3.  
  4. $login_url = 'http://passport.renren.com/PLogin.do'
  5.  
  6. $post_fields['email'] = '';//人人的帐号 
  7. $post_fields['password'] = '';//人人密码 
  8. $post_fields['origURL'] = 'http%3A%2F%2Fhome.renren.com%2FHome.do'
  9. $post_fields['domain'] = 'renren.com'
  10.  
  11.  
  12. $ch = curl_init($login_url); 
  13. 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'); 
  14. curl_setopt($ch, CURLOPT_HEADER, 0); 
  15. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  16. curl_setopt($ch, CURLOPT_MAXREDIRS, 1); 
  17. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
  18. curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 
  19. curl_setopt($ch, CURLOPT_POST, 1); 
  20. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); 
  21. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); 
  22. $content = curl_exec($ch); 
  23. curl_close($ch); 
  24. //匹配用户的ID 
  25. $send_url='http://www.renren.com/home'
  26. $ch = curl_init($send_url); 
  27. curl_setopt($ch, CURLOPT_HEADER, 0); 
  28. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  29. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); 
  30. curl_exec($ch); 
  31. $info = curl_getinfo($ch); 
  32. curl_close($ch); 
  33. //获取用户id 
  34. $tmp = explode('/',$info['redirect_url']); 
  35. $uid = array_pop($tmp); 
  36. unset($tmp); 
  37.  
  38. //$uid = "305115027"; 
  39. //获取token和rtk 
  40.  
  41. $send_url='http://www.renren.com/'.$uid
  42. $ch = curl_init($send_url); 
  43. curl_setopt($ch, CURLOPT_HEADER, 0); 
  44. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  45. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); 
  46. $tmp = curl_exec($ch); 
  47. curl_close($ch); 
  48. preg_match_all("/get_check:'(.*?)',get_check_x:'(.*?)',/is",$tmp,$arr); 
  49. $token = $arr[1][0];//1121558104 
  50. $rtk = $arr[2][0];//e9a9cb2 
  51.  
  52. //发布信息 
  53. $poststr['content'] = "这就是一个测试而已!!!"
  54. $poststr['withInfo'] = '{"wpath":[]}'
  55. $poststr['hostid:'] = $uid
  56. $poststr['privacyParams'] = '{"sourceControl": 99}'
  57. $poststr['requestToken'] = $token
  58. $poststr['_rtk'] = $rtk
  59. $poststr['channel'] = "renren"
  60.  
  61. $head = array
  62.    'Referer:http://shell.renren.com/ajaxproxy.htm'
  63.    'X-Requested-With:XMLHttpRequest'
  64.   ); 
  65. $ch = curl_init("http://shell.renren.com/{$uid}/status"); 
  66. 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'); 
  67. curl_setopt($ch,CURLOPT_HTTPHEADER,$head); 
  68. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  69. curl_setopt($ch, CURLOPT_MAXREDIRS, 1); 
  70. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
  71. curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 
  72. curl_setopt($ch, CURLOPT_POST, 1); 
  73. curl_setopt($ch, CURLOPT_POSTFIELDS, $poststr); 
  74. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); 
  75. $content = curl_exec($ch);//开源代码phpfensi.com 
  76. curl_close($ch); 
  77. $data = json_decode($content,true); 
  78. if($data["code"] == "0"){ 
  79.  echo "发布成功!"
  80. }else
  81.  echo "shit !!!"
  82. }

Tags: curl模拟登陆 php发表状态

分享到: