PHP实现QQ快速登录的方法
前言:PHP实现QQ快速登录,罗列了三种方法 方法一:面向过程,回调地址和首次触发登录写到了一个方法页面【因为有了if做判断】, 方法二,三:面向对象 1.先调用登录方法,向腾讯发送请求, 2.腾讯携带本网站唯一对应参数OPENID,ACCESSTOKEN,返回到对应回调页面, 3.回调页面接受到腾讯的参数后,通过这个两个参数,再发出对应的请求,如查询用户的数据。 4.腾讯做出对应的操作,如返回这个用户的数据给你 即使你没看懂,也没关系,按照我下面的流程来,保证你可以实现。 前期准备:使用人家腾讯的功能,总得和人家打招呼吧! QQ互联首页:http://connect.qq.com/ 进入网址后,按如下操作来: 一.进入官网二.申请创建【网站】应用三.按要求填写资料注意网站地址:填写你要设置快速登录的网址,eg:http://www.test.com; 回调地址:填写你发送QQ快速登陆后,腾讯得给你信息,这个信息往此页面接受。eg:http://www.test.com/accept_info.php 【详细的申请填写,请见官方提示,这里不做赘述】四.申请成功后,完善信息最终要求,获得APP_ID ,APP_KEY五.代码部分:在你对应的PHP文件内写入,如下 方法一,面向过程法 使用方法:配置$app_id,$app_secret,$my_url后,其他原封复制即可,$user_data为返回的登录信息 代码: //Step1:获取Authorization Code
session_start(); $code = $_REQUEST["code"];//存放Authorization Code 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=" . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state=" . $_SESSION['state']; echo(""); } //Step2:通过Authorization Code获取Access Token error:" . $msg->error;echo " msg :" . $msg->error_description;exit; } }
方法二,面向对象 使用类QQ_LoginAction.class 使用方法: 1.在QQ_LoginAction.class中正确配置 APPID,APPKEY CALLBACK(回调网址) 2.在调用方法中,代码: qq_login(); //调用登录方法,向腾讯发出快速登录请求
3.在回调页面中,代码: qq_callback();get_openid();get_user_info();
4.$user_data即为返回的用户数据。 5.QQ_LoginAction.class.php 文件代码:【用的ThinkPHP3.2】 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'); //授权接口列表 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( //发布一条普通微博 "https://graph.qq.com/t/add_t",array("format" => "json","content","#clientip","#longitude","#latitude","#compatibleflag"),"POST" ),"add_pic_t" => array( //发布一条图片微博 "https://graph.qq.com/t/add_pic_t",array("content","pic","format" => "json","#syncflag","#compatiblefalg"),"del_t" => array( //删除一条微博 "https://graph.qq.com/t/del_t",array("id","format" => "json"),"get_repost_list" => array( //获取单条微博的转发或点评列表 "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( //获取其他用户资料 "https://graph.qq.com/user/get_other_info","#name-1","#fopenid-1") ),"get_fanslist" => array( "https://graph.qq.com/relation/get_fanslist",//我的微博粉丝列表 array("format" => "json","startindex","#mode","#install","#sex") ),"get_idollist" => array( "https://graph.qq.com/relation/get_idollist",//我的微博收听列表 array("format" => "json","#install") ),"add_idol" => array( "https://graph.qq.com/relation/add_idol",//微博收听某用户 array("format" => "json","#fopenids-1"),"del_idol" => array( //微博取消收听某用户 "https://graph.qq.com/relation/del_idol","#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(){ //-------生成唯一随机串防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(){ //--------验证state防止CSRF攻击 if($_GET['state'] != $_SESSION['state']){ return false; } //-------请求参数列表 $keysArr = array( "grant_type" => "authorization_code","client_secret" => APPKEY,"code" => $_GET['code'] ); //------构造请求access_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,$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){ /**
/**
//调用相应api public function post($url,$flag = 0){ 方法三,面向对象 使用腾讯给的SDK 使用方法:腾讯SDK,API写的很详细,不做赘述 地址:http://wiki.connect.qq.com/%E7%BD%91%E7%AB%99%E6%8E%A5%E5%85%A5%E6%A6%82%E8%BF%B0 这样就实现了QQ快捷登录,其实很简单的,大家可以试一试。 还有什么不清楚的,可以看看官方介绍,更详细, Tips:如何在本地测试QQ快速登录 方法:修改HOST配置文件 1. 打开C:WindowsSystem32driversetchost 2. 添加127.0.0.1 www.test.com 然后操作就可以了。以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |