curl模拟登陆新浪微博代码

Posted by phper star | Posted in PHP | Posted on 2014-10-15 16:29

    最近想做一个利用curl模拟登陆新浪微博的功能,但是在实际测试过程中,遇到了一些问题。主要问题是新浪那边更改了微博登陆那边的加密方式,之前网上的哪些代码都失效了。所以我考虑曲线救国,先利用curl登陆新浪通行证,再从通信证这边跳转到微博那边。通过网页访问这种方式是可行的,但是在写代码利用curl操作的时候,遇到了问题。登陆新浪通行证成功,但是再去请求微博页面的时候,用户消息过不去。于是贴上代码,求解,欢迎留言告诉解决办法,先谢了。。。


<?php
/**
 * 用于模拟新浪微博登录! 
 */

//header("content-type:text/html;charset=UTF8");

/** 定义项目路径 */
define('PROJECT_ROOT_PATH' , dirname(__FILE__));
define('COOKIE_PATH' , PROJECT_ROOT_PATH );

// 通用时间戳
define('TIMESTAMP', time());


/** 用来做模拟登录的新浪帐号 */
$username = ""; 
$password = "";

/* Fire Up */
$weiboLogin = new weiboLogin( $username, $password );
echo $weiboLogin->showTestPage( 'http://login.sina.com.cn/member/person.php' );

/***通过这里再去登陆新浪微博就有问题,请解,还原留言***/
echo $weiboLogin->showTestPage( 'http://weibo.com/' );


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 = $username;
		$this->password = $password;
	}
	
	/**
	 * CURL请求
	 * @param String $url 请求地址
	 * @param Array $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_SSL_VERIFYPEER => "false",
        					CURLOPT_SSL_VERIFYHOST => "false",
							CURLOPT_COOKIEJAR => $this->cookiefile,
							CURLOPT_COOKIEFILE => $this->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 CurlLoginForSso(){
		$postArr = array(
				'callback' => 'parent.sinaSSOController.loginCallBack', 
				'encoding' => 'UTF-8', 
				'entry' => 'sso', 
				'from' => 'null', 
				'gateway' => 1, 
				'pagerefer' => 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.5)',
				'prelt' =>0,
				'returntype' => 'IFRAME', 
				'savestate' => 30, 
				'service' => 'sso', 
				'setdomain' => 1,
				'sp' => $this->password,
				'sr' => '1366*768',
				'su' => base64_encode(urlencode($this->username)), 
				'useticket' => 0, 
				'vsnf' => 1,	
			);
            
			$loginData = $this->curlRequest('https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)',$postArr); 

			if($loginData){ 
				$matchs = array(); 
				$loginResult = $this->curlRequest('http://login.sina.com.cn/crossdomain2.php?action=logincallback&retcode=0&reason=&callback=parent.sinaSSOController.loginCallBack&setdomain=1&entry=sso&sr=1366%2A768'); 
				//exit(var_dump($loginResult));
				$loginResultArr = array(); 
				preg_match('/loginCallBack\((.*?)\)/',$loginResult,$loginResultArr); 
				//exit($loginResultArr[1]);
				$userInfo = json_decode($loginResultArr[1],true); 
				return $userInfo; 
			}else{ 
				exit('Login sina fail.'); 
			} 
	}
	
	/**  测试登录情况, 调用参考 */
	function showTestPage( $url ) {
		$file_holder = $this->curlRequest( $url );
		
		// 如果未登录情况, 登录后再尝试
		$isLogin = strpos( $file_holder, 'class="user_name"');
		if ( !$isLogin ){
			unset($file_holder);
			//$this->doSinaLogin();
			$userinfo = $this->CurlLoginForSso();
			if(is_array($userinfo)){
				var_dump($userinfo);
				$file_holder = $this->curlRequest( $url );
			}
			
		}
		return $file_holder ;
	}	
	
	
}

标签: curl模拟登陆新浪微博 curl 模拟登陆新浪微博代码