QQ互联登陆的最简洁代码
<?php
/**
* http://wiki.open.qq.com/wiki/
* Date: 14-6-18
* Time: 下午18:04
*/
class Model_Login_QqConnect extends Model_Abstract
{
public $qq_config = array(
\’qq_k\’ => \’10112****\’, //QQ应用APP ID
\’qq_s\’ => \’c3e5a337b1c8b82adafa80e5********\’, //QQ应用APP KEY
\’callback_url\’ => \’www.haitaohua.com/interface/login/qc\’, //授权回调网址
\’scope\’ => \’get_user_info,add_share\’ //权限列表,具体权限请查看官方的api文档
);
function __construct($appid =null, $appkey=null, $access_token=NULL){
$this->appid= $this->qq_config[\’qq_k\’];
$this->appkey=$this->qq_config[\’qq_s\’];
$this->access_token=$access_token;
}
function login_url($callback_url, $scope=\’\’){
$params=array(
\’client_id\’=>$this->appid,
\’redirect_uri\’=>$callback_url,
\’response_type\’=>\’code\’,
\’scope\’=>$scope
);
return \’https://graph.qq.com/oauth2.0/authorize?\’.http_build_query($params);
}
function access_token($callback_url, $code){
$params=array(
\’grant_type\’=>\’authorization_code\’,
\’client_id\’=>$this->appid,
\’client_secret\’=>$this->appkey,
\’code\’=>$code,
\’state\’=>\’\’,
\’redirect_uri\’=>$callback_url
);
$url=\’https://graph.qq.com/oauth2.0/token?\’.http_build_query($params);
$result_str=$this->http($url);
$json_r=array();
if($result_str!=\’\’)parse_str($result_str, $json_r);
return $json_r;
}
/**
function access_token_refresh($refresh_token){
}
**/
function get_openid(){
$params=array(
\’access_token\’=>$this->access_token
);
$url=\’https://graph.qq.com/oauth2.0/me?\’.http_build_query($params);
$result_str=$this->http($url);
$json_r=array();
if($result_str!=\’\’){
preg_match(\’/callback\(\s+(.*?)\s+\)/i\’, $result_str, $result_a);
$json_r=json_decode($result_a[1], true);
}
return $json_r;
}
// 需要调用什么方法可以自己加
function get_user_info($openid){
$params=array(
\’openid\’=>$openid
);
$url=\’https://graph.qq.com/user/get_user_info\’;
return $this->api($url, $params);
}
function add_share($openid, $title, $url, $site, $fromurl, $images=\’\’, $summary=\’\’){
$params=array(
\’openid\’=>$openid,
\’title\’=>$title,
\’url\’=>$url,
\’site\’=>$site,
\’fromurl\’=>$fromurl,
\’images\’=>$images,
\’summary\’=>$summary
);
$url=\’https://graph.qq.com/share/add_share\’;
return $this->api($url, $params, \’POST\’);
}
function api($url, $params, $method=\’GET\’){
$params[\’access_token\’]=$this->access_token;
$params[\’oauth_consumer_key\’]=$this->appid;
$params[\’format\’]=\’json\’;
if($method==\’GET\’){
$result_str=$this->http($url.\’?\’.http_build_query($params));
}else{
$result_str=$this->http($url, http_build_query($params), \’POST\’);
}
$result=array();
if($result_str!=\’\’)$result=json_decode($result_str, true);
return $result;
}
function http($url, $postfields=\’\’, $method=\’GET\’, $headers=array()){
$ci=curl_init();
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ci, CURLOPT_TIMEOUT, 30);
if($method==\’POST\’){
curl_setopt($ci, CURLOPT_POST, TRUE);
if($postfields!=\’\’)curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
}
$headers[]=”User-Agent: qqPHP(piscdong.com)”;
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLOPT_URL, $url);
$response=curl_exec($ci);
curl_close($ci);
return $response;
}
}
qc.php (callback.php回调页面)
<?php
/**
* QQ账号合作登陆
* User: xuxiang
* Date: 14-6-18
* Time: 上午17:43
*/
class Controller_Interface_Login_Qc extends Controller_Interface_AbstractE
{
//http://www.haitaohua.com/interface/login/qc
public function run()
{
try {
//授权回调页面,即配置文件中的$callback_url
session_start();
// Step1:获取Authorization Code
$code = $_REQUEST[“code”];
$qq = new Model_Login_QqConnect();
if(empty($code))
{
//state参数用于防止CSRF攻击,成功授权后回调时会原样带回
$_SESSION[\’state\’] = md5(uniqid(rand(), TRUE));
//拼接URL
$dialog_url = “https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=”
. $qq->qq_config[\’qq_k\’] . “&redirect_uri=” . urlencode($qq->qq_config[\’callback_url\’]) . “&state=”
. $_SESSION[\’state\’];
echo(“<script> top.location.href=\'” . $dialog_url . “\'</script>”);
}
$ret = $qq->access_token($qq->qq_config[\’callback_url\’], $code);
if( !empty($ret) ) {
$qq = new Model_Login_QqConnect($qq->qq_config[\’qq_k\’], $qq->qq_config[\’qq_s\’], $ret[\’access_token\’]);
$open_id = $qq->get_openid();
if( !empty($open_id) ) {
$user_info = $qq->get_user_info($open_id[\’openid\’]);
echo “<pre>”;
print_r($user_info);die;
} else {
Tool_Redirect::redirect_info($_SERVER[\’HTTP_REFERER\’], 2, \’授权失败\’);
}
} else {
Tool_Redirect::redirect_info($_SERVER[\’HTTP_REFERER\’], 2, \’授权失败\’);
}
} catch (Exception $e) {
throw $e;
}
}
}