社会化登录在现在大多数的网站中都会存在的,比较火的有 新浪、人人、QQ、 如果挨个对这些平台做开发的话,会花费开发者很长的时间来开发和测试。当然了,百度社会化服务解决了这个问题。
直接代码:

  <?php
/**
 * WebApp - 团购第三方登录model
 */
class WeboauthModel{
	/**
	 * 第三方登录
	 */
	public function loginAppUser(){
		$accesstoken=$this->requestAccessToken($_GET['code']);
		if($accesstoken==false){
			return false;
		}
		$userinfo=$this->requestUserInfo($accesstoken['access_token']); //返回第三方用户信息
		var_dump($userinfo);
	}
	public function loginUser($phone,$password){
		$passport=model('Passport');
		$result=$passport->loginLocal(phone,$password);
		if(!$result){
			$status = 0; 
			$info	= $passport->getError();
			$data 	= 0;
		}else{
			$status = 1;
			$info 	= $passport->getSuccess();
			$data 	= ($GLOBALS['ts']['site']['home_url'])?$GLOBALS['ts']['site']['home_url']:0;
		}
		return array('status'=>$status,'info'=>$info,'data'=>$data);
	}
    /**
     * 获取AccessToKen 
     */
    private function requestAccessToken($code){
		$url = 'https://openapi.baidu.com/social/oauth/2.0/token';
        $client_id = 'API Key';
        $client_secret = 'Secret Key';
        $redirect_uri='回调地址、与应用中回掉地址相同';
        $params = array(
            'grant_type'    =>  'authorization_code',
            'client_id'     =>  $client_id,
            'client_secret' =>  $client_secret,
            'redirect_uri'  =>  $redirect_uri,
            'code'          =>  $code,
        );
        $ch = curl_init();
    	$curl_opts = array(
			CURLOPT_CONNECTTIMEOUT	=> 3,
			CURLOPT_TIMEOUT			=> 5,
			CURLOPT_USERAGENT		=> 'baidu-apiclient-php-2.0',
	    	CURLOPT_HTTP_VERSION	=> CURL_HTTP_VERSION_1_1,
	    	CURLOPT_RETURNTRANSFER	=> true,
	    	CURLOPT_HEADER			=> false,
	    	CURLOPT_FOLLOWLOCATION	=> false,
		);
		if (stripos($url, 'https://') === 0) {
			$curl_opts[CURLOPT_SSL_VERIFYPEER] = false;
		}	
		$query = http_build_query($params, '', '&');
		$delimiter = strpos($url, '?') === false ? '?' : '&';
	   	$curl_opts[CURLOPT_URL] = $url . $delimiter . $query;
	   	$curl_opts[CURLOPT_POST] = false;
    	curl_setopt_array($ch, $curl_opts);
        $result = curl_exec($ch);
    	if ($result === false) {
            curl_close($ch);
            return false;
    	} elseif (empty($result)) {
    		$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    		if ($http_code != 200) {
    			curl_close($ch);
    			return false;
    		}
    	}  
	    curl_close($ch);
       return  json_decode($result, true);
    }
    /**
     * 返回第三方用户信息
     * @param $access_token
     */
    private function requestUserInfo($access_token){
    	$url="https://openapi.baidu.com/social/api/2.0/user/info?access_token={$access_token}";
		$result=file_get_contents($url);
    	$result =json_decode($result,true); 
    	return $result;
    }
}//end class

回调文件:

   <?php
	// 第三方登录回调方法
	include('./social/WeboauthModel.class.php');
	$token = $_GET['code'];
	$Weboauth = new WeboauthModel();
	$Weboauth->loginAppUser($token);
   ?>
//返回值为json、做了json_decode();
array(16) {
  ["username"]=>
  string(6) "真名、"
  ["sex"]=>
  string(1) "1"
  ["birthday"]=>
  string(4) "1991"
  ["tinyurl"]=>
  string(75) "http://hdn.xnimg.cn/photos/hdn321/20120101/1950/tiny_t6Jq_119344n019117.jpg"
  ["headurl"]=>
  string(80) "http://hdn.xnimg.cn/photos/hdn121/20120101/1950/h_head_KXXQ_7a90000220ec2f75.jpg"
  ["mainurl"]=>
  string(80) "http://hdn.xnimg.cn/photos/hdn121/20120101/1950/h_main_eBB8_7a90000220ec2f75.jpg"
  ["hometown_location"]=>
  array(2) {
    ["province"]=>
    string(6) "河北"
    ["city"]=>
    string(9) "邯郸市"
  }
  ["work_history"]=>
  array(1) {
    [0]=>
    array(4) {
      ["name"]=>
      string(18) "北京一起新游"
      ["time"]=>
      string(0) ""
      ["industry"]=>
      NULL
      ["job"]=>
      NULL
    }
  }
  ["university_history"]=>
  string(24) "注释"
  ["hs_history"]=>
  array(0) {
  }
  ["province"]=>
  string(0) ""
  ["city"]=>
  string(0) ""
  ["is_verified"]=>
  string(1) "1"
  ["media_uid"]=>
  string(9) "-------"
  ["media_type"]=>
  string(6) "renren"
  ["social_uid"]=>
  int(------)
}

百度社会化服务参考、api地址
http://developer.baidu.com/dev#/appinfo/1728015/soc_login_select // 创建样式
下面我们提供一段简单的PHP示例代码,来介绍应该如何实现"社会化服务回调地址"这个接口。

http://developer.baidu.com/wiki/index.php?title=docs/social/guide/web_login/web_login5

api列表注意回调地址应与应用地址相同

http://developer.baidu.com/wiki/index.php?title=docs/social/api/list

转载请注明转自:运达's blog 原文地址:http://www.yunda51.com/1154.html