欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP CURL 模拟登录 SINA 微博

程序员文章站 2024-02-10 16:57:16
...

最近工作上有一个需求, 需要获取 http://weibo.com/at/weibo 的数据, 就是 @我自己 的数据, 没有接口, 只能通过抓页面. 下面贴下部分代码 来自: http://summerbluet.com/704


  1. /**
  2. * 用于模拟新浪微博登录! by CJ ( http://www.summerbluet.com )
  3. */
  4. /** 定义项目路径 */
  5. define('PROJECT_ROOT_PATH' , dirname(__FILE__));
  6. define('COOKIE_PATH' , PROJECT_ROOT_PATH );
  7. // 通用时间戳
  8. define('TIMESTAMP', time());
  9. // 出现问题的时候可以开启, 调试用的, 会在当前文件夹下面创建 LOG 文件
  10. define('DEBUG', false);
  11. /** 用来做模拟登录的新浪帐号 */
  12. $username = "";
  13. $password = "";
  14. /* Fire Up */
  15. $weiboLogin = new weiboLogin( $username, $password );
  16. exit($weiboLogin->showTestPage( 'http://weibo.com/at/comment' ));
  17. class weiboLogin {
  18. private $cookiefile;
  19. private $username;
  20. private $password;
  21. function __construct( $username, $password )
  22. {
  23. ( $username =='' || $password=='' ) && exit( "请填写用户名密码" );
  24. $this->cookiefile = COOKIE_PATH.'/cookie_sina_'.substr(base64_encode($username), 0, 10);
  25. $this->username = $username;
  26. $this->password = $password;
  27. }
  28. /**
  29. * CURL请求
  30. * @param String $url 请求地址
  31. * @param Array $data 请求数据
  32. */
  33. function curlRequest($url, $data = false)
  34. {
  35. $ch = curl_init();
  36. $option = array(
  37. CURLOPT_URL => $url,
  38. CURLOPT_HEADER => 0,
  39. CURLOPT_HTTPHEADER => array('Accept-Language: zh-cn','Connection: Keep-Alive','Cache-Control: no-cache'),
  40. CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1",
  41. CURLOPT_FOLLOWLOCATION => TRUE,
  42. CURLOPT_MAXREDIRS => 4,
  43. CURLOPT_RETURNTRANSFER => TRUE,
  44. CURLOPT_COOKIEJAR => $this->cookiefile,
  45. CURLOPT_COOKIEFILE => $this->cookiefile
  46. );
  47. if ( $data ) {
  48. $option[CURLOPT_POST] = 1;
  49. $option[CURLOPT_POSTFIELDS] = $data;
  50. }
  51. curl_setopt_array($ch, $option);
  52. $response = curl_exec($ch);
  53. if (curl_errno($ch) > 0) {
  54. exit("CURL ERROR:$url " . curl_error($ch));
  55. }
  56. curl_close($ch);
  57. return $response;
  58. }
  59. /** @desc CURL 模拟新浪登录 */
  60. function doSinaLogin()
  61. {
  62. // Step 1 : Get tickit
  63. $preLoginData = $this->curlRequest('http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=' .
  64. base64_encode($this->username) . '&client=ssologin.js(v1.3.16)');
  65. preg_match('/sinaSSOController.preloginCallBack\((.*)\)/', $preLoginData, $preArr);
  66. $jsonArr = json_decode($preArr[1], true);
  67. $this->debug('debug_1_Tickit', $preArr[1]);
  68. if (is_array($jsonArr)) {
  69. // Step 2 : Do Certification
  70. $postArr = array( 'entry' => 'weibo',
  71. 'gateway' => 1,
  72. 'from' => '',
  73. 'vsnval' => '',
  74. 'savestate' => 7,
  75. 'useticket' => 1,
  76. 'ssosimplelogin' => 1,
  77. 'su' => base64_encode(urlencode($this->username)),
  78. 'service' => 'miniblog',
  79. 'servertime' => $jsonArr['servertime'],
  80. 'nonce' => $jsonArr['nonce'],
  81. 'pwencode' => 'wsse',
  82. 'sp' => sha1(sha1(sha1($this->password)) . $jsonArr['servertime'] . $jsonArr['nonce']),
  83. 'encoding' => 'UTF-8',
  84. 'url' => 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack',
  85. 'returntype' => 'META');
  86. $loginData = $this->curlRequest('http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.19)', $postArr);
  87. $this->debug('debug_2_Certification_raw', $loginData);
  88. // Step 3 : SSOLoginState
  89. if ($loginData) {
  90. $matchs = $loginResultArr =array();
  91. preg_match('/replace\(\'(.*?)\'\)/', $loginData, $matchs);
  92. $this->debug('debug_3_Certification_result', $matchs[1]);
  93. $loginResult = $this->curlRequest( $matchs[1] );
  94. preg_match('/feedBackUrlCallBack\((.*?)\)/', $loginResult, $loginResultArr);
  95. $userInfo = json_decode($loginResultArr[1],true);
  96. $this->debug('debug_4_UserInfo', $loginResultArr[1]);
  97. } else {
  98. exit('Login sina fail.');
  99. }
  100. } else {
  101. exit('Server tickit fail');
  102. }
  103. }
  104. /** 测试登录情况, 调用参考 */
  105. function showTestPage( $url ) {
  106. $file_holder = $this->curlRequest( $url );
  107. // 如果未登录情况, 登录后再尝试
  108. $isLogin = strpos( $file_holder, 'class="user_name"');
  109. if ( !$isLogin ){
  110. unset($file_holder);
  111. $this->doSinaLogin();
  112. $file_holder = $this->curlRequest( $url );
  113. }
  114. return $file_holder ;
  115. }
  116. /** 调试 */
  117. function debug( $file_name, $data ) {
  118. if ( DEBUG ) {
  119. file_put_contents( $file_name.'.txt', $data );
  120. }
  121. }
  122. }
复制代码