There is a need at work recently. I need to obtain the data of http://weibo.com/at/weibo, which is @myself's data. There is no interface and can only be obtained by grabbing the page. Here is some code From:
http://summerbluet.com/704
- /**
- * Used to simulate Sina Weibo login! by CJ ( http://www.summerbluet.com )
- */
- /**Define project path*/
- define('PROJECT_ROOT_PATH' , dirname(__FILE__));
- define('COOKIE_PATH' , PROJECT_ROOT_PATH );
- // Universal timestamp
- define('TIMESTAMP', time());
- // Can be turned on when a problem occurs. For debugging, a LOG file will be created under the current folder
- define('DEBUG', false);
- /**Sina account used for simulated login*/
- $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( "Please fill in the username and password" );
-
- $this->cookiefile = COOKIE_PATH.' /cookie_sina_'.substr(base64_encode($username), 0, 10);
- $this->username = $username;
- $this->password = $password;
- }
-
- /**
- * CURL request
- * @param String $url request address
- * @param Array $data request data
- * /
- function curlRequest($url, $data = false)
- {
- $ch = curl_init();
-
- $option = array(
- CURLOPT_URL => $url,
- CURLOPT_HEADER => 0,
- CURLOPT_HTTPHEADER => array('Accept-Language: zh-cn','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.0.1180.79 Safari/537.1",
- CURLOPT_FOLLOWLOCATION => TRUE,
- CURLOPT_MAXREDIRS => 4,
- CURLOPT_RETURNTRANSFER => TRUE,
- CURLOPT_COOKIEJAR => $ this->cookiefile,
- CURLOPT_COOKIEFILE => option);
- $response = curl_exec($ch);
-
- if (curl_errno($ch) > 0) {
- exit("CURL ERROR:$url " . curl_error($ch));
- }
- curl_close( $ch);
- return $response;
- }
-
- /**@desc CURL simulates Sina login*/
- 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)') ;
- preg_match('/sinaSSOController.preloginCallBack((.*))/', $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->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) {
-
- $ matches = $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($loginResultArr[1],true);
-
- $this->debug('debug_4_UserInfo', $loginResultArr[1]);
- } else {
- exit('Login sina fail.');
- }
- } else {
- exit('Server tickit fail');
- }
- }
-
- /**Test login situation, call reference*/
- function showTestPage( $url ) {
- $file_holder = $this->curlRequest( $url );
-
- // If not logged in, try again after logging in
- $isLogin = strpos( $file_holder, 'class="user_name"');
- if ( !$isLogin ){
- unset($file_holder);
- $this->doSinaLogin();
- $file_holder = $this->curlRequest( $url );
- }
- return $file_holder ;
- }
-
- /**debug*/
- function debug( $file_name, $data ) {
- if ( DEBUG ) {
- file_put_contents( $file_name.'.txt' , $data );
- }
- }
-
- }
-
Copy code
|