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

PHP Curl模拟登录微信公众平台、新浪微博实例代码

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-07 14:06:00 浏览: 评论:0 

这篇文章主要介绍了PHP Curl模拟登录微信公众平台、新浪微博实例代码的相关资料,涉及到php curl模拟登录相关知识,需要的朋友可以参考下。

使用curl之前先打开curl配置,具体方式百度一下就知道,开启curl扩展。密码用md5加密,这是经过测试成功的,把用户跟密码改成你的就行了。

下面一段代码给大家介绍php使用curl模拟登录微信公众平台,具体代码如下所示:

  1. <?php  
  2. //模拟微信登入  
  3. $cookie_file = tempnam('./temp','cookie');  
  4. $login_url = 'https://mp.weixin.qq.com/cgi-bin/login';  
  5. $pwd = md5("********"); 
  6. $data = "f=json&imgcode=&pwd=$pwd&username=*****@***.com";  
  7. $ch = curl_init($login_url);  
  8. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);  
  9. curl_setopt($ch,CURLOPT_POST,1);  
  10. curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file);  
  11. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
  12. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);  
  13. curl_setopt($ch,CURLOPT_REFERER,'https://mp.weixin.qq.com');  
  14. curl_setopt($ch,CURLOPT_POSTFIELDS,$data);  
  15. $content = curl_exec($ch);  
  16. curl_close($ch);  
  17. $newurl = json_decode($content,1);  
  18. //var_dump($newurl); 
  19. //exit; 
  20. $newurl = $newurl['redirect_url'];  
  21. //获取登入后页面的源码  
  22. $go_url = 'https://mp.weixin.qq.com'.$newurl;  
  23. $ch = curl_init($go_url);  
  24. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);  
  25. curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_file);  
  26. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,0);  
  27. curl_setopt($ch, CURLOPT_HEADER, 0);  
  28. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
  29. $content = curl_exec($ch);  
  30. //var_dump(curl_error($ch));  
  31. print_r($content);  
  32. curl_close($ch);  
  33. ?> 

使用 PHP CURL 模拟登录新浪微博

有时候我们获取一些新浪微博的数据,但又不想使用API,只好使用模拟登录了.

发现以前可以使用的CURL模拟登录代码失效了,Google一下,发现有很多人碰到这个问题.但是没有找到解决方法,所以就自己研究了一下,发现了原因.

可能是因为新浪限制了不允许模拟登录,同样的登录参数,用网页登录一切正常,用CURL登录,返回的COOKIES竟然是临时的.

所以看起来是登录成功了,并且获取到了用户信息,但是再次访问还是未登录状态.我的解决方法比较简单,直接修改COOKIES的时效这样就行了.

附上我自己测试通过的PHP代码如下,希望有对有同样问题的朋友有用,如果你有更好的方案欢迎分享一下.

发现只要不设置CURLOPT_COOKIESESSION参数就行了,不需要修改COOKIE_FILE.

  1. <?php 
  2. class sina 
  3. /* 
  4. 一个简单的新浪微搏curl模拟登录类. 来源: http://chenall.net/post/sina_curl_login/ 
  5. 使用方法: 
  6. http函数是一个简单的curl封装函数,需要自己去实现, 
  7. http函数原型如下: 
  8. http($url,$post_data = null) 
  9. 返回网页内容. 
  10. 第一个参数$url,就是要访问的url地址,$post_data是post数据,如果为空,则代表GET访问. 
  11. 1.使用加密后密码登录 加密方法: sha1(sha1($pass)) 
  12. $sina = new sina($username,$sha1pass) 
  13. 2.直接使用原始密码登录 
  14. $sina = new sina($username,$sha1pass,0) 
  15. 执行之后如果$sina->status非空,则登录成功,否则登录失败. 
  16. 登录成功之后,你就可以直接继续使用http函数来访问其它内容. 
  17. 使用 unset($sina) 会自动注销登录. 
  18. */ 
  19. public $status
  20. function __construct($su,$sp,$flags = 1) { 
  21. $this->status = $this->login($su,$sp,$flags); 
  22. function __destruct() 
  23. //注销登录 
  24. $this->logout(); 
  25. function logout() 
  26. http("http://weibo.com/logout.php"); 
  27. unset($this->status); 
  28. /*不需要了,只要不设置HTTP函数中不设置CURLOPT_COOKIESESSION参数就行了,要设可以设为false. 
  29. function ResetCookie()//重置相关cookie 
  30. { 
  31. global $cookie_file; 
  32. $str = file_get_contents($cookie_file); 
  33. $t = time()+3600;//设置cookie有效时间一个小时 
  34. $str = preg_replace("/\t0\t/", "\t".$t."\t", $str); 
  35. $f = fopen($cookie_file,"w"); 
  36. fwrite($f,$str); 
  37. fclose($f); 
  38. } 
  39. */ 
  40. function login($su,$sp,$flags = 0) 
  41. $su = urlencode(base64_encode($su)); 
  42. $data = http("http://login.sina.com.cn/sso/prelogin.php?entry=miniblog&client=ssologin.js&user=".$su); 
  43. if (emptyempty($data)) 
  44. return null; 
  45. //$data = substr($data,35,-1); 
  46. $data = json_decode($data); 
  47. if ($data->retcode != 0) 
  48. return null; 
  49. if ($flags == 0) 
  50. $sp = sha1(sha1($sp)); 
  51. $sp .= strval($data->servertime).$data->nonce; 
  52. $sp = sha1($sp); 
  53. $data = "url=http%3A%2F%2Fweibo.com%2Fajaxlogin.php%3F&returntype=META&ssosimplelogin=1&su=".$su.'&service=miniblog&servertime='.$data->servertime."&nonce=".$data->nonce.'&pwencode=wsse&sp='.$sp
  54. $data = http("http://login.sina.com.cn/sso/login.php?client=ssologin.js",$data); 
  55. //$this->ResetCookie(); 
  56. if (preg_match("/location\.replace\('(.*)'\)/",$data,$url)) 
  57. $data = http($url[1]); 
  58. //$this->ResetCookie(); 
  59. $data = json_decode(substr($data,1,-2)); 
  60. if ($data->result == true) 
  61. return $data->userinfo; 
  62. return null; 
  63. ?> 

以上内容给大家介绍了PHP Curl模拟登录微信公众平台、新浪微博实例代码,希望本文所述对大家有所帮助。

Tags: Curl模拟登录 PHP登录新浪微博

分享到: