最近工作上有一個需求, 需要獲取http://weibo.com/at/weibo 的數據, 就是@我自己的數據, 沒有接口, 只能通過抓頁面. 下面貼下部分代碼來自:
http://summerbluet.com/704
-
/**
- * 用於模擬新浪微博登入! by CJ ( http://www.summerbluet.com )
- */
-
- /**定義專案路徑*/
- define('PROJECT_ROOT_**用來做模擬登入的新浪帳號*/
- define('PROJECT_ROOT_PATH' , dirnamePATH' , dirnamePATH' , dirnamePATH' , dirnamePATH' , dirnamePATH' , dirnamePATH' (__FILE__));
- define('COOKIE_PATH' , PROJECT_ROOT_PATH );
-
- // 通用時間戳
- define('TIMESTAMP', time());
-
- //🎜>//出現問題的時候可以開啟, 偵錯用的, 會在目前資料夾下面建立LOG 檔案
- define('DEBUG', false);
-
- /**
- * CURL請求
- * @param String $url 請求位址
- * @param Array $data 請求資料
- */
- $ username = "";
- $password = "";
-
- /* Fire Up */
- $weiboLogin = new weiboLogin( $username, $password );
- exit($weiboLogin- >showTestPage( 'http://weibo.com/at/comment' ));
-
- class weiboLogin {
-
- private $cookiefile;
- private $username;
- private $ password;
-
- function __construct( $username, $password )
- {
- ( $username =='' || $password=='' ) && exit( "請填入使用者名稱密碼" );
-
- $this->cookiefile = COOKIE_PATH.'/cookie_sina_'.substr(base64_encode($username), 0, 10);
- $this->username = $this->username = $ $
- $ this->password = $password;
- }
-
- /**@desc CURL 模擬新浪登錄*/
- function curlRequest($url, $data = false)
- {
- $ch = curl_init();
-
- $option = array(
- CURLOPT_URL => $url,
- CURLOPT_HEADER => 0,
- CURLOPT_HTTPHEADER => arrayy-cept-Lcnd 'Connection: Keep-Alive','Cache-Control: no-cache'),
- CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.01180.011180.91180. Safari/537.1",
- CURLOPT_FOLLOWLOCATION => TRUE,
- CURLOPT_MAXREDIRS => 4,
- CURLOPT_RETURNTRANSFER => TRUE,
- CURURPT_CthisIEJAR.S. >cookiefile
- );
-
- if ( $data ) {
- $option[CURLOPT_POST] = 1;
- $option[CURLOPT_POSTFIELDS] = $data
- }
- }
- 🎜> curl_setopt_array($ch, $option);
- $response = curl_exec($ch);
-
- if (curl_errno($ch) > 0) {
- exit("CURL ERROR:$ url " . curl_error($ch));
- }
- curl_close($ch);
- return $response;
- }
-
- /***/*/
- function doSinaLogin()
- {
- // Step 1 : Get tickit
- $preLoginData = $this->curlRequest('http://login.sina.com.cn/sso/prelogin.php? entry=weibo&callback=sinaSSOController.preloginCallBack&su=' .
- base64_encode($this->username) . '&client=ssologin.js(v1.3.16)');
- pregone_Satchin. ))/', $preLoginData, $preArr);
- $jsonArr = json_decode($preArr[1], true);
-
- $this->debug('debug_1_Tickit', $preArr[1] );
-
- if (is_array($jsonArr)) {
- // Step 2 : Do Certification
- $postArr = array( 'entry' => 'weibo',
- 'gateway' => 1,
- 'from' => '',
- 'vsnval' => '',
- 'savestate' => 7,
- 'useticket' => 1,
- ' ssosimplelogin' => 1,
- 'su' => base64_encode(urlencode($this->username)),
- 'service' => 'miniblog',
- 'servertime' => $jsonArr[' servertime'],
- 'nonce' => $jsonArr['nonce'],
- 'pwencode' => 'wsse',
- 'sp' => sha1(sha1(sha1($this-> password)) . $jsonArr['servertime'] . $jsonArr['nonce']),
- 'encoding' => 'UTF-8',
- 'url' => 'http://weibo. com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack',
- 'returntype' => 'META');
-
- $loginData = $this-> 'META');
-
- $loginData = $this->curlRequest('http:// login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.19)', $postArr);
-
- $this->debug('debug_2_Certification_raw', $loginData);
-
- // Step 3 : SSOLoginState
- if ($loginData) {
-
- $matchs = $loginResultArr =array();
- preg_match('/replace('(.* ?)')/', $loginData, $matchs);
-
- $this->debug('debug_3_Certification_result', $matchs[1]);
-
- $loginResult = $this-> curlRequest( $matchs[1] );
- preg_match('/feedBackUrlCallBack((.*?))/', $loginResult, $loginResultArr);
-
- $userInfo = json_decode($login] ,true);
-
- $this->debug('debug_4_UserInfo', $loginResultArr[1]);
- } else {
- exit('Login sina fail.');
- }
- } } } } } } } } } } } } } } } } } } } } } } }} } else { exit('Server tickit fail'); } }
-
- /**測試登入情況, 呼叫參考*/
- function showTestPage( $url ) {
- $file_holder = $this->curlRequest( $url );
-
- // 如果沒有登入狀況, 登入後再嘗試
- $isLogin = strpos( $file_holder, 'class="user_name"');
- if ( !$isLogin ){
- unset($file_holder);
- $ this->doSinaLogin();
- $file_holder = $this->curlRequest( $url );
- }
- return $file_holder ;
- }
-
- /** function debug( $file_name, $data ) {
- if ( DEBUG ) {
- file_put_contents( $file_name.'.txt', $data );
- }
- }
- }
-
- }
複製程式碼
|