サイト統合QQ登録
3794 ワード
最近は暇です.ウェブサイトの整合qqを見てどうやって実現しますか?残念ですが、公式のsdkはちょっと卵が痛いと思います.簡単なものを複雑化して、公式サイトのデモを見ました.
oauth 2.0の資料詳細は以下の通りです.
http://wiki.connect.qq.com/oauth2-0%E7%AE%80%E4%BB%8B
oauth 2.0の資料詳細は以下の通りです.
http://wiki.connect.qq.com/oauth2-0%E7%AE%80%E4%BB%8B
Oauth url
//Step1: Authorization Code
//Step2: Authorization Code Access Token
//Step3: Access Token OpenID
<?php
class QQ_Oauth{
const GET_AUTH_CODE_URL = "https://graph.qq.com/oauth2.0/authorize";
const GET_ACCESS_TOKEN_URL = "https://graph.qq.com/oauth2.0/token";
const GET_OPENID_URL = "https://graph.qq.com/oauth2.0/me";
const APP_ID = "xxx"; // APPKEY
const APP_SECRET = "xxxx";//
const MY_URL = 'cn-php.com/lab/qq/qqconn.php';//
public static function getAuthorizeUrl($app_param=array()){
$params = array(
'response_type'=>'code',
'client_id'=>isset($app_param['appid'])?$app_param['appid']:SELF::APP_ID,
'redirect_uri'=>isset($app_param['redirect_uri'])?$app_param['redirect_uri']:SELF::MY_URL,
'state'=>$app_param['state'],
);
$authorze_url = SELF::GET_AUTH_CODE_URL.'?'.http_build_query($params);
return $authorze_url;
}
public static function getToken($app_param=array()){
$params = array(
'grant_type'=>'authorization_code',
'client_id'=>isset($app_param['appid'])?$app_param['appid']:SELF::APP_ID,
'redirect_uri'=>isset($app_param['redirect_uri'])?$app_param['redirect_uri']:SELF::MY_URL,
'client_secret'=>isset($app_param['app_secret'])?$app_param['app_secret']:SELF::APP_SECRET,
'code'=>$app_param['code'],
);
$token_url = SELF::GET_ACCESS_TOKEN_URL.'?'.http_build_query($params);
$response = file_get_contents($token_url);
parse_str($response, $params);
$result = array(
'access_token'=>isset($params['access_token'])?$params['access_token']:'',
);
return $result;
}
public static function getOpenID($app_param=array()){
$params = array(
'access_token'=>$app_param['access_token'],
);
$openid_url = SELF::GET_OPENID_URL.'?'.http_build_query($params);
$str = file_get_contents($openid_url);
if(strpos($str, "callback") !== false)
{
$lpos = strpos($str, "(");
$rpos = strrpos($str, ")");
$str = substr($str, $lpos + 1, $rpos - $lpos -1);
}
$user = json_decode($str);
$result = array(
'openid'=>isset($user->openid)?$user->openid:'',
);
return $result;
}
}
//
<?php
require './QQ_Oauth.class.php';
//Step1: Authorization Code
session_start();
$code = isset($_REQUEST["code"])?$_REQUEST["code"]:'';
if(empty($code))
{
//state CSRF ,
$_SESSION['state'] = md5(uniqid(rand(), TRUE));
$data['state'] = $_SESSION['state'];
$authorze_url = QQ_Oauth::getAuthorizeUrl($data);
header('Location:'.$authorze_url);
}
if($_REQUEST['state'] == $_SESSION['state'])
{
//Step2: Authorization Code Access Token
$data = array(
'code'=>$code,
);
$token = QQ_Oauth::getToken($data);
//Step3: Access Token OpenID
if(!empty($token['access_token']))
{
$user_info = QQ_Oauth::getOpenID($token);
// $user_info['openid']
/*
uid openid
if( , ){
//
}else{
// openid
}
*/
}else{
exit('token error...');
}
}else{
exit("csrf...");
}