qq登陆类已封装,用于QQ登陆的callback页面<?php <br />
<br>
/**<br>
* qq登陆接口类<br>
* 实例化类时传入3个参数 app_id,app_key,callback<br>
* qq接入流程需要自己去QQ互联文档上了解,此类只做回调功能封装<br>
* arthur:米国村长<br>
* */<br>
<br>
class qqlogin{<br>
public $app_id;<br>
public $app_key;<br>
public $callback;<br>
public $code;<br>
public $state;<br>
<br>
<br>
public function __construct($app_id,$app_key,$callback){<br>
//接收从qq登陆页返回来的值<br>
$this->code = isset($_REQUEST['code'])? $_REQUEST['code'] : '';<br>
$this->state = isset($_REQUEST['state'])? $_REQUEST['state'] : '';<br>
//将参数赋值给成员属性<br>
$this->app_id = $app_id;<br>
$this->app_key = $app_key;<br>
$this->callback = $callback;<br>
}<br>
<br>
/**<br>
* 获取access_token值<br>
* @return array 返回包含access_token,过期时间的数组<br>
* */<br>
public function get_token(){<br>
$url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=".$this->app_id."&client_secret=".$this->app_key."&code=".$this->code."&redirect_uri=".urlencode($this->callback);<br>
$str = $this->visit_url($url);//访问url获得返回值<br>
parse_str($str,$arr);<br>
return $arr;<br>
}<br>
<br>
/**<br>
* 获取client_id 和 openid<br>
* @param $access_token access_token验证码<br>
* @return array 返回包含client_id 和 openid的数组<br>
* */<br>
public function get_client_id($access_token){<br>
$url = 'https://graph.qq.com/oauth2.0/me?access_token='.$access_token;<br>
$str = $this->visit_url($url);//访问url获得返回值<br>
return $this->change_callback($str);//返回经过json转码后的数组<br>
}<br>
<br>
/**<br>
* 获取用户信息<br>
* @param $client_id<br>
* @param $access_token<br>
* @param $openid<br>
* @return array 用户的信息数组<br>
* */<br>
public function user_info($client_id,$openid,$access_token){<br>
$url = 'https://graph.qq.com/user/get_user_info?oauth_consumer_key='.$client_id.'&access_token='.$access_token.'&openid='.$openid.'&format=json';<br>
$str = $this->visit_url($url);<br>
$arr = json_decode($str,true);<br>
return $arr;<br>
<br>
}<br>
<br>
/**<br>
* 请求URL地址,得到返回字符串<br>
* @param $url qq提供的api接口地址<br>
* */<br>
public function visit_url($url){<br>
static $cache = 0;<br>
//判断是否之前已经做过验证<br>
if($cache === 1){<br>
$str = $this->curl($url);<br>
}elseif($cache === 2){<br>
$str = $this->openssl($url);<br>
}else{<br>
//是否可以使用cURL<br>
if(function_exists('curl_init')){<br>
$str = $this->curl($url);<br>
$cache = 1;<br>
//是否可以使用openssl<br>
}elseif(function_exists('openssl_open') && ini_get("allow_fopen_url")=="1"){<br>
$str = $this->openssl($url);<br>
$cache = 2;<br>
}else{<br>
die('请开启php配置中的php_curl或php_openssl');<br>
}<br>
}<br>
return $str;<br>
}<br>
<br>
<br>
/**<br>
* 将字符串转换为可以进行json_decode的格式<br>
* 将转换后的参数值赋值给成员属性$this->client_id,$this->openid<br>
* @param $str 返回的callback字符串 <br>
* @return 数组<br>
* */<br>
protected function change_callback($str){<br>
if (strpos($str, "callback") !== false){<br>
//将字符串修改为可以json解码的格式<br>
$lpos = strpos($str, "(");<br>
$rpos = strrpos($str, ")");<br>
$json = substr($str, $lpos + 1, $rpos - $lpos -1);<br>
//转化json<br>
$result = json_decode($json,true);<br>
$this->client_id = $result['client_id'];<br>
$this->openid = $result['openid'];<br>
return $result;<br>
<br>
}else{<br>
return false;<br>
}<br>
}<br>
<br>
/**<br>
* 通过curl取得页面返回值<br>
* 需要打开配置中的php_curl<br>
* */<br>
private function curl($url){<br>
$ch = curl_init();<br>
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);//允许请求的内容以文件流的形式返回<br>
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);//禁用https<br>
curl_setopt($ch,CURLOPT_URL,$url);//设置请求的url地址<br>
$str = curl_exec($ch);//执行发送<br>
curl_close($ch);<br>
return $str;<br>
}<br>
<br>
/**<br>
* 通过file_get_contents取得页面返回值<br>
* 需要打开配置中的allow_fopen_url和php_openssl<br>
* */<br>
private function openssl($url){<br>
$str = file_get_contents($url);//取得页面内容<br>
return $str;<br>
}<br>
}<br>
<br>
//必须申请开通QQ登陆,并且自己有域名才可以使用呢~~~<br>
/*实例开始*/<br>
header('content-type:text/html;charset=utf-8');<br>
<br>
//申请QQ互联后得到的APP_ID 和 APP_KEY<br>
$app_id = *****9677;<br>
$app_key = '863b3eec66**************';<br>
<br>
//回调接口,接受QQ服务器返回的信息的脚本<br>
$callback = 'http://yoursite/xx/callback.php';<br>
<br>
//实例化qq登陆类,传入上面三个参数<br>
$qq = new qqlogin($app_id,$app_key,$callback);<br>
<br>
//得到access_token验证值<br>
$arr = $qq->get_token();<br>
if(isset($arr['access_token']))<br>
$access_token = $arr['access_token'];<br>
else<br>
die('登陆失败');<br>
<br>
//得到用户的openid(登陆用户的识别码)和Client_id<br>
$arr = $qq->get_client_id($access_token);<br>
if(isset($arr['client_id'])){<br>
$client_id = $arr['client_id'];<br>
$openid = $arr['openid'];<br>
}else{<br>
die('登陆失败');<br>
}<br>
//请求接口,得到用户所有数据<br>
$arr = $qq->user_info($client_id,$openid,$access_token);<br>
<br>
//var_dump($arr);<br>
<br>
/*实例结束*/<br>
?>
AD:真正免费,域名+虚机+企业邮箱=0元