<?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;
}php
// 須要調用什麼方法能夠本身加
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;
}
}json
qc.php (callback.php回調頁面)api
<?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;
}
}
}session