Ç°ÑÔ£º
PHPʵÏÖQQ¿iËٵǼ£¬ÂÞÁÐÁËÈýÖÖ½¨
½¨Ò»£ºÃaeÏo¹ý³Ì£¬»Øµ÷µØÖºÍÊ״δ¥¢µÇ¼дµ½ÁËÒ»¸o½¨Ò³Ãae¡¾ÒoΪÓÐÁËif×oÅжϡ¿£¬
½¨¶þ£¬Èý£ºÃaeÏo¶ÔÏo
1.Ïȵ÷ÓõǼ½¨£¬ÏoÌÚѶ¢ËÍÇeÇo£¬
2.ÌÚѶЯ´ø±¾ÍøվΨһ¶ÔÓ¦²ÎÊýOPENID,ACCESSTOKEN£¬µ»Øµ½¶ÔÓ¦»Øµ÷Ò³Ãae£¬
3.»Øµ÷Ò³Ãae½ÓÊܵ½ÌÚѶµÄ²ÎÊýºo£¬Í¨¹ýÕa¸oÁ½¸o²ÎÊý£¬ÔÙ¢³o¶ÔÓ¦µÄÇeÇo£¬Èç²eѯÓû§µÄÊý¾Ý¡£
4.ÌÚѶ×o³o¶ÔÓ¦µÄ²Ù×÷£¬Èçµ»ØÕa¸oÓû§µÄÊý¾Ý¸øÄa
¼´Ê¹Äaû¿´¶®£¬Ò²Ã»¹Øϵ£¬°´ÕÕÎÒÏÂÃaeµÄÁ÷³ÌÀ´£¬±£Ö¤Äa¿ÉÒÔʵÏÖ¡£
Ç°ÆÚ×¼±¸£º
ʹÓÃÈ˼ÒÌÚѶµÄ¹¦ÄÜ£¬×ܵúÍÈ˼ҴoÕкo°É£¡
QQ»¥ÁªÊ×Ò³£ºhttp://connect.qq.com/
½øÈeÍøÖ*ºo£¬°´ÈçϲÙ×÷À´£º
Ò».½øÈe¹ÙÍø
¶þ.ÉeÇe´´½¨¡¾ÍøÕ¾¡¿Ó¦ÓÃ
Èý.°´ÒªÇoÌiд×ÊÁÏ
×¢ÒaÍøÕ¾µØÖ£ºÌiдÄaÒªÉeÖÿiËٵǼµÄÍøÖ£¬eg:http://www.test.com;
»Øµ÷µØÖ:ÌiдÄa¢ËÍQQ¿iËٵǽºo£¬ÌÚѶµÃ¸øÄaÐÅÏ¢£¬Õa¸oÐÅÏ¢Íu´ËÒ³Ãae½ÓÊÜ¡£eg:http://www.test.com/accept_info.php
*¡¾ÏeϸµÄÉeÇeÌiд£¬Çe¼u¹Ù½Ìaʾ£¬ÕaÀi²»×o׸Êo¡¿**
ËÄ.ÉeÇe³É¹¦ºo£¬ÍeÉÆÐÅÏ¢
×iÖÕÒªÇo£¬»ñµÃAPP_ID £¬APP_KEY
*Îa.´uÂe²¿Ö£º**
ÔÚÄa¶ÔÓ¦µÄPHPÎļþÄÚдÈe£¬ÈçÏÂ
½¨Ò»£¬ÃaeÏo¹ý³Ì¨
ʹÓý¨£ºÅaÖÃ$app_id£¬$app_secret£¬$my_urlºo£¬ÆaËuÔa¸´ÖƼ´¿É£¬$user_dataΪ*µ»ØµÄµÇ¼ÐÅÏ¢
´uÂe£º
//Ó¦ÓõÄAPPID
$app_id = "ÄaµÄAPPID";
//Ó¦ÓõÄAPPKEY
$app_secret = "ÄaµÄAPPKEY";
//¡¾³É¹¦ÊÚȨ¡¿ºoµÄ»Øµ÷µØÖ*£¬¼´´ËµØÖ*ÔÚÌÚѶµÄÐÅÏ¢ÖÐÓд¢´ae
$my_url = "ÄaµÄ»Øµ÷ÍøÖ*";
//Step1£º»ñÈ¡Authorization Code
session_start();
$code = $_REQUEST["code"];//´ae*ÅAuthorization Code
if(empty($code))
{
//state²ÎÊýÓÃÓÚ*ÀÖ¹CSRF¹¥»÷£¬³É¹¦ÊÚȨºo»Øµ÷ʱ»aÔÑu´ø»Ø
$_SESSION['state'] = md5(uniqid(rand(), TRUE));
//Æ´½ÓURL
$dialog_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client;_id="
. $app_id . "&redirect;_uri=" . urlencode($my_url) . "&state;="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
//Step2£ºÍ¨¹ýAuthorization Code»ñÈ¡Access Token
if($_REQUEST['state'] == $_SESSION['state'] || 1)
{
//Æ´½ÓURL
$token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"
. "client_id=" . $app_id . "&redirect;_uri=" . urlencode($my_url)
. "&client;_secret=" . $app_secret . "&code;=" . $code;
$response = file_get_contents($token_url);
if (strpos($response, "callback") !== false)//Èç¹uµÇ¼Óû§ÁÙʱ¸Ä±aÖ÷ÒaÈ¡ÏuÁË£¬*µ»Øtrue!==false,*ñÔoÖ´ÐÐstep3
{
$lpos = strpos($response, "(");
$rpos = strrpos($response, ")");
$response = substr($response, $lpos + 1, $rpos - $lpos -1);
$msg = json_decode($response);
if (isset($msg->error))
{
echo "<h3>error:</h3>" . $msg->error;
echo "<h3>msg :</h3>" . $msg->error_description;
exit;
}
}
//Step3£ºÊ¹ÓÃAccess TokenÀ´»ñÈ¡Óû§µÄOpenID
$params = array();
parse_str($response, $params);//°Ñ´«»ØÀ´µÄÊý¾Ý²ÎÊý±aÁ¿»¯
$graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$params['access_token'];
$str = file_get_contents($graph_url);
if (strpos($str, "callback") !== false)
{
$lpos = strpos($str, "(");
$rpos = strrpos($str, ")");
$str = substr($str, $lpos + 1, $rpos - $lpos -1);
}
$user = json_decode($str);//´ae*Å*µ»ØµÄÊý¾Ý client_id £¬openid
if (isset($user->error))
{
echo "<h3>error:</h3>" . $user->error;
echo "<h3>msg :</h3>" . $user->error_description;
exit;
}
//echo("Hello " . $user->openid);
//echo("Hello " . $params['access_token']);
//Step4£ºÊ¹ÓÃ<span style="font-family: Arial, Helvetica, sans-serif;">openid,</span><span style="font-family: Arial, Helvetica, sans-serif;">access_tokenÀ´»ñÈ¡Ëu½ÓÊܵÄÓû§ÐÅÏ¢¡£</span>
$user_data_url = "https://graph.qq.com/user/get_user_info?access_token={$params['access_token']}&oauth;_consumer_key={$app_id}&openid;={$user->openid}&format;=json";
$user_data = file_get_contents($user_data_url);//´ËΪ»ñÈ¡µ½µÄuserÐÅÏ¢
}
else
{
echo("The state does not match. You may be a victim of CSRF.");
}
½¨¶þ£¬ÃaeÏo¶ÔÏo ʹÓÃÀaQQ_LoginAction.class
ʹÓý¨£º
1.ÔÚQQ_LoginAction.classÖÐÕýÈÅaÖà APPID£¬APPKEY CALLBACK(»Øµ÷ÍøÖ)
2.ÔÚµ÷Óý¨ÖУ¬´uÂe£º
$qq_login = new \Component\QQ_LoginAction(); //ÒýÈe´ËÀaÎļþ¼´¿É
$qq_login->qq_login(); //µ÷ÓõǼ*½*¨£¬ÏoÌÚѶ*¢³o¿iËٵǼÇeÇo
3.Ôڻص÷Ò³ÃaeÖУ¬´uÂe£º
$qc = new \Component\QQ_LoginAction();
$acs = $qc->qq_callback();<span style="white-space:pre"> //access_token
$oid=$qc->get_openid();<span style="white-space:pre"> //openid
$user_data = $qc->get_user_info();<span style="white-space:pre"> //get_user_info()Ϊ»ñµÃ¸ÃÓû§µÄÐÅÏ¢£¬ÆaËu²Ù×÷*½*¨¼uAPIÎĵµ
4.$user_data¼´Îª*µ»ØµÄÓû§Êý¾Ý¡£
5.QQ_LoginAction.class.php Îļþ´uÂe£º¡¾ÓõÄThinkPHP3.2¡¿
<?php
namespace Component;
session_start();
define('APPID','XXXX'); //appid
define('APPKEY','XXXX'); //appkey
define('CALLBACK','XXXX'); //»Øµ÷µØÖ*
define('SCOPE','get_user_info,list_album,add_album,upload_pic,add_topic,add_weibo'); //ÊÚȨ½Ó¿ÚÁбi
class QQ_LoginAction {
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";
private $APIMap = array(
"get_user_info" => array( //»ñÈ¡Óû§×ÊÁÏ
"https://graph.qq.com/user/get_user_info",
array("format" => "json"),
),
"add_t" => array( //*¢²¼Ò»ÌoÆÕͨ΢²(C)
"https://graph.qq.com/t/add_t",
array("format" => "json", "content","#clientip","#longitude","#latitude","#compatibleflag"),
"POST"
),
"add_pic_t" => array( //*¢²¼Ò»ÌoͼƬ΢²(C)
"https://graph.qq.com/t/add_pic_t",
array("content", "pic", "format" => "json", "#clientip", "#longitude", "#latitude", "#syncflag", "#compatiblefalg"),
"POST"
),
"del_t" => array( //ɾ³ýÒ»Ìo΢²(C)
"https://graph.qq.com/t/del_t",
array("id", "format" => "json"),
"POST"
),
"get_repost_list" => array( //»ñÈ¡µ¥Ìo΢²(C)µÄת*¢»oµaÆÀÁбi
"https://graph.qq.com/t/get_repost_list",
array("flag", "rootid", "pageflag", "pagetime", "reqnum", "twitterid", "format" => "json")
),
"get_info" => array( //»ñÈ¡µ±Ç°Óû§×ÊÁÏ
"https://graph.qq.com/user/get_info",
array("format" => "json")
),
"get_other_info" => array( //»ñÈ¡ÆaËuÓû§×ÊÁÏ
"https://graph.qq.com/user/get_other_info",
array("format" => "json", "#name-1", "#fopenid-1")
),
"get_fanslist" => array(
"https://graph.qq.com/relation/get_fanslist", //ÎÒµÄ΢²(C)*ÛË¿Áбi
array("format" => "json", "reqnum", "startindex", "#mode", "#install", "#sex")
),
"get_idollist" => array(
"https://graph.qq.com/relation/get_idollist", //ÎÒµÄ΢²(C)ÊÕÌýÁбi
array("format" => "json", "reqnum", "startindex", "#mode", "#install")
),
"add_idol" => array(
"https://graph.qq.com/relation/add_idol", //΢²(C)ÊÕÌýijÓû§
array("format" => "json", "#name-1", "#fopenids-1"),
"POST"
),
"del_idol" => array( //΢²(C)È¡ÏuÊÕÌýijÓû§
"https://graph.qq.com/relation/del_idol",
array("format" => "json", "#name-1", "#fopenid-1"),
"POST"
)
);
private $keysArr;
function __construct(){
if($_SESSION["openid"]){
$this->keysArr = array(
"oauth_consumer_key" => APPID,
"access_token" => $_SESSION['access_token'],
"openid" => $_SESSION["openid"]
);
}else{
$this->keysArr = array(
"oauth_consumer_key" => APPID
);
}
}
public function qq_login(){
//-------Éu³ÉΨһËae»u´®*ÀCSRF¹¥»÷
$_SESSION['state'] = md5(uniqid(rand(), TRUE));
$keysArr = array(
"response_type" => "code",
"client_id" => APPID,
"redirect_uri" => CALLBACK,
"state" => $_SESSION['state'],
"scope" => SCOPE
);
$login_url = self::GET_AUTH_CODE_URL.'?'.http_build_query($keysArr);
header("Location:$login_url");
}
public function qq_callback(){
//--------ÑeÖ¤state*ÀÖ¹CSRF¹¥»÷
if($_GET['state'] != $_SESSION['state']){
return false;
}
//-------ÇeÇo²ÎÊýÁбi
$keysArr = array(
"grant_type" => "authorization_code",
"client_id" => APPID,
"redirect_uri" => CALLBACK,
"client_secret" => APPKEY,
"code" => $_GET['code']
);
//------¹¹ÔiÇeÇoaccess_tokenµÄurl
$token_url = self::GET_ACCESS_TOKEN_URL.'?'.http_build_query($keysArr);
$response = $this->get_contents($token_url);
if(strpos($response, "callback") !== false){
$lpos = strpos($response, "(");
$rpos = strrpos($response, ")");
$response = substr($response, $lpos + 1, $rpos - $lpos -1);
$msg = json_decode($response);
if(isset($msg->error)){
$this->showError($msg->error, $msg->error_description);
}
}
$params = array();
parse_str($response, $params);
$_SESSION["access_token"]=$params["access_token"];
$this->keysArr['access_token']=$params['access_token'];
return $params["access_token"];
}
public function get_contents($url){
if (ini_get("allow_url_fopen") == "1") {
$response = file_get_contents($url);
}else{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
}
if(empty($response)){
return false;
}
return $response;
}
public function get_openid(){
//-------ÇeÇo²ÎÊýÁбi
$keysArr = array(
"access_token" => $_SESSION["access_token"]
);
$graph_url = self::GET_OPENID_URL.'?'.http_build_query($keysArr);
$response = $this->get_contents($graph_url);
//--------¼i²a´iÎoÊÇ*ñ*¢Éu
if(strpos($response, "callback") !== false){
$lpos = strpos($response, "(");
$rpos = strrpos($response, ")");
$response = substr($response, $lpos + 1, $rpos - $lpos -1);
}
$user = json_decode($response);
if(isset($user->error)){
$this->showError($user->error, $user->error_description);
}
//------¼Ç¼openid
$_SESSION['openid']=$user->openid;
$this->keysArr['openid']=$user->openid;
return $user->openid;
}
/**
* showError
* ÏÔʾ´iÎoÐÅÏ¢
* @param int $code ´iÎo´uÂe
* @param string $description ÃeÊoÐÅÏ¢£¨¿ÉÑ¡£(C)
*/
public function showError($code, $description = '$'){
echo "<meta charset=\"UTF-8\">";
echo "<h3>error:</h3>$code";
echo "<h3>msg :</h3>$description";
exit();
}
/**
* _call
* ħÊo*½*¨£¬×oapiµ÷ÓÃת*¢
* @param string $name µ÷ÓõÄ*½*¨Ãu³Æ
* @param array $arg ²ÎÊýÁбiÊý×e
* @since 5.0
* @return array *µ¼Óµ÷Óýa¹uÊý×e
*/
public function __call($name,$arg){
//Èç¹uAPIMap²»´aeÔÚÏaÓ¦µÄapi
if(empty($this->APIMap[$name])){
$this->showError("apiµ÷ÓÃÃu³Æ´iÎo","²»´aeÔÚµÄAPI: <span style='color:red;'>$name</span>");
}
//´ÓAPIMap»ñÈ¡apiÏaÓ¦²ÎÊý
$baseUrl = $this->APIMap[$name][0];
$argsList = $this->APIMap[$name][1];
$method = isset($this->APIMap[$name][2]) ? $this->APIMap[$name][2] : "GET";
if(empty($arg)){
$arg[0] = null;
}
$responseArr = json_decode($this->_applyAPI($arg[0], $argsList, $baseUrl, $method),true);
//¼i²e*µ»ØretÅжÏapiÊÇ*ñ³É¹¦µ÷ÓÃ
if($responseArr['ret'] == 0){
return $responseArr;
}else{
$this->showError($responseArr['ret'], $responseArr['msg']);
}
}
//µ÷ÓÃÏaÓ¦api
private function _applyAPI($arr, $argsList, $baseUrl, $method){
$pre = "#";
$keysArr = $this->keysArr;
$optionArgList = array();//Ò»Ð(C)¶aÏiÑ¡Ìi²ÎÊý±ØÑ¡Ò»µÄÇeÐÎ
foreach($argsList as $key => $val){
$tmpKey = $key;
$tmpVal = $val;
if(!is_string($key)){
$tmpKey = $val;
if(strpos($val,$pre) === 0){
$tmpVal = $pre;
$tmpKey = substr($tmpKey,1);
if(preg_match("/-(\d$)/", $tmpKey, $res)){
$tmpKey = str_replace($res[0], "", $tmpKey);
$optionArgList[]= $tmpKey;
}
}else{
$tmpVal = null;
}
}
//-----Èç¹uûÓÐÉeÖÃÏaÓ¦µÄ²ÎÊý
if(!isset($arr[$tmpKey]) || $arr[$tmpKey] === ""){
if($tmpVal == $pre){
continue;
}else if($tmpVal){//ÔoʹÓÃĬÈϵÄÖµ
$arr[$tmpKey] = $tmpVal;
}else{
$this->showError("apiµ÷ÓòÎÊý´iÎo","δ´«Èe²ÎÊý$tmpKey");
}
}
$keysArr[$tmpKey] = $arr[$tmpKey];
}
//¼i²eÑ¡Ìi²ÎÊý±ØÌiÒ»µÄÇeÐÎ
if(count($optionArgList)!=0){
$n = 0;
foreach($optionArgList as $val){
if(in_array($val, array_keys($keysArr))){
$n++;
}
}
if(!$n){
$str = implode(",",$optionArgList);
$this->showError("apiµ÷ÓòÎÊý´iÎo",$str."±ØÌiÒ»¸o");
}
}
if($method == "POST"){
$response = $this->post($baseUrl, $keysArr, 0);
}else if($method == "GET"){
$baseUrl=$baseUrl.'?'.http_build_query($keysArr);
$response = $this->get_contents($baseUrl);
}
return $response;
}
public function post($url, $keysArr, $flag = 0){
$ch = curl_init();
if(! $flag) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $keysArr);
curl_setopt($ch, CURLOPT_URL, $url);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
}
½¨Èý£¬ÃaeÏo¶ÔÏo ʹÓÃÌÚѶ¸øµÄSDK
ʹÓý¨:ÌÚѶSDK£¬APIдµÄºÜÏeϸ£¬²»×o׸Êo
µØÖ*£ºhttp://wiki.connect.qq.com/%E7%BD%91%E7%AB%99%E6%8E%A5%E5%85%A5%E6%A6%82%E8%BF%B0
ÕaÑu¾ÍʵÏÖÁËQQ¿i½ÝµÇ¼£¬ÆaʵºÜ¼oµ¥µÄ£¬´o¼Ò¿ÉÒÔÊÔÒ»ÊÔ¡£
»¹ÓÐʲô²»Ça³þµÄ£¬¿ÉÒÔ¿´¿´¹Ù*½½eÉÜ£¬¸uÏeϸ£¬
Tips:ÈçºÎÔÚ±¾µØ²aÊÔQQ¿iËٵǼ
½¨£ºÐÞ¸ÄHOSTÅaÖÃÎļþ
1. ´o¿ªC:\Windows\System32\drivers\etc\host
2. Ìi¼Ó127.0.0.1 www.test.com
È»ºo²Ù×÷¾Í¿ÉÒÔÁË¡£
ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íu¶Ô´o¼ÒµÄѧϰÓÐËu°iÖu£¬Ò²Ï£Íu´o¼Ò¶a¶aÖ§³Ö½Å±¾Ö®¼Ò¡£