PHP CURL 模擬登入 SINA 微博

WBOY
發布: 2016-07-25 09:02:32
原創
1102 人瀏覽過

最近工作上有一個需求, 需要獲取http://weibo.com/at/weibo 的數據, 就是@我自己的數據, 沒有接口, 只能通過抓頁面. 下面貼下部分代碼來自: http://summerbluet.com/704


  1. /**
  2. * 用於模擬新浪微博登入! by CJ ( http://www.summerbluet.com )
  3. */
  4. /**定義專案路徑*/
  5. define('PROJECT_ROOT_**用來做模擬登入的新浪帳號*/
  6. define('PROJECT_ROOT_PATH' , dirnamePATH' , dirnamePATH' , dirnamePATH' , dirnamePATH' , dirnamePATH' , dirnamePATH' (__FILE__));
  7. define('COOKIE_PATH' , PROJECT_ROOT_PATH );
  8. // 通用時間戳
  9. define('TIMESTAMP', time());
  10. //🎜>//出現問題的時候可以開啟, 偵錯用的, 會在目前資料夾下面建立LOG 檔案
  11. define('DEBUG', false);
  12. /**
  13. * CURL請求
  14. * @param String $url 請求位址
  15. * @param Array $data 請求資料
  16. */
  17. $ username = "";
  18. $password = "";
  19. /* Fire Up */
  20. $weiboLogin = new weiboLogin( $username, $password );
  21. exit($weiboLogin- >showTestPage( 'http://weibo.com/at/comment' ));
  22. class weiboLogin {
  23. private $cookiefile;
  24. private $username;
  25. private $ password;
  26. function __construct( $username, $password )
  27. {
  28. ( $username =='' || $password=='' ) && exit( "請填入使用者名稱密碼" );
  29. $this->cookiefile = COOKIE_PATH.'/cookie_sina_'.substr(base64_encode($username), 0, 10);
  30. $this->username = $this->username = $ $
  31. $ this->password = $password;
  32. }
  33. /**@desc CURL 模擬新浪登錄*/
  34. function curlRequest($url, $data = false)
  35. {
  36. $ch = curl_init();
  37. $option = array(
  38. CURLOPT_URL => $url,
  39. CURLOPT_HEADER => 0,
  40. CURLOPT_HTTPHEADER => arrayy-cept-Lcnd 'Connection: Keep-Alive','Cache-Control: no-cache'),
  41. CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.01180.011180.91180. Safari/537.1",
  42. CURLOPT_FOLLOWLOCATION => TRUE,
  43. CURLOPT_MAXREDIRS => 4,
  44. CURLOPT_RETURNTRANSFER => TRUE,
  45. CURURPT_CthisIEJAR.S. >cookiefile
  46. );
  47. if ( $data ) {
  48. $option[CURLOPT_POST] = 1;
  49. $option[CURLOPT_POSTFIELDS] = $data
  50. }
  51. }
  52. 🎜> curl_setopt_array($ch, $option);
  53. $response = curl_exec($ch);
  54. if (curl_errno($ch) > 0) {
  55. exit("CURL ERROR:$ url " . curl_error($ch));
  56. }
  57. curl_close($ch);
  58. return $response;
  59. }
  60. /***/*/
  61. function doSinaLogin()
  62. {
  63. // Step 1 : Get tickit
  64. $preLoginData = $this->curlRequest('http://login.sina.com.cn/sso/prelogin.php? entry=weibo&callback=sinaSSOController.preloginCallBack&su=' .
  65. base64_encode($this->username) . '&client=ssologin.js(v1.3.16)');
  66. pregone_Satchin. ))/', $preLoginData, $preArr);
  67. $jsonArr = json_decode($preArr[1], true);
  68. $this->debug('debug_1_Tickit', $preArr[1] );
  69. if (is_array($jsonArr)) {
  70. // Step 2 : Do Certification
  71. $postArr = array( 'entry' => 'weibo',
  72. 'gateway' => 1,
  73. 'from' => '',
  74. 'vsnval' => '',
  75. 'savestate' => 7,
  76. 'useticket' => 1,
  77. ' ssosimplelogin' => 1,
  78. 'su' => base64_encode(urlencode($this->username)),
  79. 'service' => 'miniblog',
  80. 'servertime' => $jsonArr[' servertime'],
  81. 'nonce' => $jsonArr['nonce'],
  82. 'pwencode' => 'wsse',
  83. 'sp' => sha1(sha1(sha1($this-> password)) . $jsonArr['servertime'] . $jsonArr['nonce']),
  84. 'encoding' => 'UTF-8',
  85. 'url' => 'http://weibo. com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack',
  86. 'returntype' => 'META');
  87. $loginData = $this-> 'META');
  88. $loginData = $this->curlRequest('http:// login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.19)', $postArr);
  89. $this->debug('debug_2_Certification_raw', $loginData);
  90. // Step 3 : SSOLoginState
  91. if ($loginData) {
  92. $matchs = $loginResultArr =array();
  93. preg_match('/replace('(.* ?)')/', $loginData, $matchs);
  94. $this->debug('debug_3_Certification_result', $matchs[1]);
  95. $loginResult = $this-> curlRequest( $matchs[1] );
  96. preg_match('/feedBackUrlCallBack((.*?))/', $loginResult, $loginResultArr);
  97. $userInfo = json_decode($login] ,true);
  98. $this->debug('debug_4_UserInfo', $loginResultArr[1]);
  99. } else {
  100. exit('Login sina fail.');
  101. }
  102. } } } } } } } } } } } } } } } } } } } } } } }} } else { exit('Server tickit fail'); } }
  103. /**測試登入情況, 呼叫參考*/
  104. function showTestPage( $url ) {
  105. $file_holder = $this->curlRequest( $url );
  106. // 如果沒有登入狀況, 登入後再嘗試
  107. $isLogin = strpos( $file_holder, 'class="user_name"');
  108. if ( !$isLogin ){
  109. unset($file_holder);
  110. $ this->doSinaLogin();
  111. $file_holder = $this->curlRequest( $url );
  112. }
  113. return $file_holder ;
  114. }
  115. /** function debug( $file_name, $data ) {
  116. if ( DEBUG ) {
  117. file_put_contents( $file_name.'.txt', $data );
  118. }
  119. }
  120. }
  121. }
複製程式碼


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!