php实现qq快速登录的方法有哪些,php如何实现登录和注册

php实现qq快速登录的方法有哪些,php如何实现登录和注册,PHP实现QQ快速登录的方法

本文主要详细介绍了PHP快速登录QQ的三种方法,有一定的参考价值,感兴趣的朋友可以参考一下。

前言:

PHP实现QQ快速登录有三种方式。

方法一:面向进程,回调地址和第一次触发登录都写在一个方法页上【因为if是用来判断的】,

方法二,三:面向对象

1.首先调用登录方法并向腾讯发送请求,

2.腾讯用该网站唯一对应的参数OpenID和访问令牌返回对应的回调页面。

3.回调页面收到腾讯的参数后,通过这两个参数发出相应的请求,比如查询用户的数据。

4.腾讯做相应的操作,比如把这个用户的数据还给你。

就算不懂也没关系。按照我下面的流程来做,保证你能实现。

前期准备:

要用腾讯的功能,你得跟别人打招呼!

QQ主页:http://connect.qq.com/

输入URL后,执行以下操作:

一.进入官网

二.申请创建【网站】应用

三.按要求填写资料

注意网址:填写要设置快速登录的网址,如:http://www . test . com;

回拨地址:填写您发送QQ快速登录后腾讯会给您的信息。此信息将在此页面上被接受。例如:http://www . test . com/accept _ info . PHP

【详细的申请填写,请见官方提示,这里不做赘述】

四.申请成功后,完善信息

最终要求,获得APP_ID ,APP_KEY

五.代码部分:

如下所示编写相应的PHP文件

方法一,流程导向方法

用法:配置$app_id、$app_secret和$my_url后,原封不动复制其他的,$user_data为返回的登录信息。

代码:

//应用程序的APPID

$app_id='您的APPID ';

//应用了APPKEY

$app_secret='您的APPKEY ';

//在【授权成功】后的回调地址,也就是这个地址存储在腾讯的信息里。

$my_url='您的回拨URL ';

//第一步:获取授权码

session_start()。

$ code=$ _ REQUEST[' code '];//存储授权码

if(empty($code))

{

//state参数用于防止CSRF攻击,授权成功后回调时会原样带回。

$ _ SESSION[' state ']=MD5(uniqid(rand(),TRUE));

//拼接URL

$ dialog _ URL=' https://graph . QQ . com/oauth 2.0/authorize?response_type=codeclient_id='。$app_id。redirect_uri='。urlencode($my_url)。状态='。$ _ SESSION[' state '];

echo('script top.location.href=' '。$dialog_url。/script’);

}

//第二步:通过授权码获取访问令牌

if($ _ REQUEST[' state ']=$ _ SESSION[' state ']| | 1)

{

//拼接URL

$ token _ URL=' https://graph . QQ . com/oauth 2.0/token?' grant_type=authorization_code '。client_id='。$app_id。redirect_uri='。urlencode($my_url)。client_secret='。$app_secret。代码='。$ code

$ response=file _ get _ contents($ token _ URL);

if (strpos($response,' callback ')!==false)//如果登录用户临时改变主意取消,返回true!==false,否则,执行步骤3。

{

$lpos=strpos($response,'(');

$rpos=strrpos($response,')');

$response=substr($response,$lpos 1,$ rpos-$ lpos-1);

$ msg=JSON _ decode($ response);

if (isset($msg-error))

{

回显“h3error:/h3”。$ msg-错误;

回显“h3msg :/h3”。$ msg-error _ description;

退出;

}

}

//步骤3:使用访问令牌获取用户的OpenID

$ params=array();

parse_str($response,$ params);//变量返回的数据参数

$ graph _ URL=' https://graph . QQ . com/oauth 2.0/me?access_token='。$ params[' access _ token '];

$ str=file _ get _ contents($ graph _ URL);

if (strpos($str,' callback ')!==假)

{

$lpos=strpos($str,'(');

$rpos=strrpos($str,')');

$str=substr($str,$lpos 1,$ rpos-$ lpos-1);

}

$ user=JSON _ decode($ str);//存储返回的数据client_id,openid

if(isset($用户错误))

{

回显“h3error:/h3”。$ user-error;

回显“h3msg :/h3”。$ user-error _ description;

退出;

}

//echo('Hello ')。$ user-OpenID);

//echo('Hello ')。$ params[' access _ token ']);

//第四步:使用字体系列:Arial,Helvetica,无衬线字体;'openid,Helvetica,sans-serif。'访问令牌来获取所接受的用户信息。跨度

$ 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);//此为获取到的用户信息

}

其他

{

回声(’状态不匹配。你可能是CSRF的受害者,");

}

方法二,面向对象使用类QQ_LoginAction.class

使用方法:

1.在QQ_LoginAction.class中正确配置APPID,APPKEY回调(回调网址)

2.在调用方法中,代码:

$ QQ _ log in=new \ Component \ QQ _ log in action();//引入此类文件即可

$ QQ _ log in-QQ _ log in();//调用登录方法,向腾讯发出快速登录请求

3.在回调页面中,代码:

$ 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()为获得该用户的信息,其他操作方法见应用程序接口文档

4.$用户数据即为返回的用户数据。

5.QQ_LoginAction.class.php文件代码:【用的ThinkPHP3.2】

?服务器端编程语言(专业超文本预处理器的缩写)

命名空间组件;

session_start()。

定义(' APPID ',' XXXX ');//appid

定义(' APPKEY ',' XXXX ');//appkey

定义('回调,‘XXXX’);//回调地址

定义(' SCOPE ',' get_user_info,list_album,add_album,upload_pic,add_topic,add _ Weibo));//授权接口列表

QQ _登录操作类{

const GET _ AUTH _ CODE _ URL=' https://graph。QQ。' com/oauth 2.0/authorize ';

const GET _ ACCESS _ TOKEN _ URL=' https://graph。QQ。“com/oauth 2.0/TOKEN”;

const GET _ OPENID _ URL=' https://graph。QQ。' com/oauth 2.0/me ';

private $APIMap=array(

get_user_info'=array( //获取用户资料

https://图表。QQ。' com/user/get _ user _ info,

数组(' format'='json '),

),

add_t'=array( //发布一条普通微博

https://graph.qq.com/t/add_t,

array('format'='json ',' content ',' #clientip ',' #longitude ',' #latitude ',' #compatibleflag '),

'发布'

),

add_pic_t'=array( //发布一条图片微博

https://graph.qq.com/t/add_pic_t,

数组(' content ',' pic ',' format'='json ',' #clientip ',' #longitude ',' #latitude ',' #syncflag ',' #compatiblefalg '),

'发布'

),

del_t'=array( //删除一条微博

https://graph.qq.com/t/del_t,

数组(' id ',' format'='json '),

'发布'

),

get_repost_list'=array( //获取单条微博的转发或点评列表

https://图表。QQ。com/t/get _ repost _ list ',

数组(' flag ',' rootid ',' pageflag ',' pagetime ',' reqnum ',' twitterid ',' format'='json ')

),

get_info'=array( //获取当前用户资料

https://图表。QQ。' com/user/get _ info,

数组(' format'='json ')

),

get_other_info'=array( //获取其他用户资料

https://图表。QQ。' com/user/get _ other _ info,

数组(' format'='json ',' #name-1 ',' #fopenid-1 ')

),

get_fanslist'=array(

https://图表。QQ。' com/relation/get _ fans list ',//我的微博粉丝列表

数组(' format'='json ',' reqnum ',' startindex ',' #mode ',' #install ',' #sex ')

),

get_idollist'=array(

https://图表。QQ。' com/relation/get _ idol list ',//我的微博收听列表

数组(' format'='json ',' reqnum ',' startindex ',' #mode ',' #install ')

),

add_idol'=array(

https://图表。QQ。' com/relation/add _ idol ',//微博收听某用户

数组(' format'='json ',' #name-1 ',' #fopenids-1 '),

'发布'

),

del_idol'=array( //微博取消收听某用户

https://图表。QQ。' com/relation/del _ idol,

数组(' format'='json ',' #name-1 ',' #fopenid-1 '),

'发布'

)

);

private $ keysArr

function __construct(){

if($_SESSION['openid']){

$this-keysArr=array(

oauth_consumer_key'=APPID,

访问令牌'=$ _会话['访问令牌'],

openid'=$_SESSION['openid']

);

}否则{

$this-keysArr=array(

oauth_consumer_key'=APPID

);

}

}

公共功能qq_login(){

//- 生成唯一随机串防CSRF攻击

$ _ SESSION[' state ']=MD5(uniqid(rand(),TRUE));

$keysArr=array(

response_type'='code ',

client_id'=APPID,

redirect_uri'=回调,

state'=$_SESSION['state'],

"范围"=范围

);

$ log in _ URL=self:GET _ AUTH _ CODE _ URL .'?'。http _ build _ query($ key sarr);

header(' Location:$ log in _ URL ');

}

公共函数qq_callback(){

//- 验证状态防止CSRF攻击

if($_GET['state']!=$_SESSION['state']){

返回错误的

}

//- 请求参数列表

$keysArr=array(

grant _ type '=' authorization _ code ',

client_id'=APPID,

redirect_uri'=回调,

client_secret'=APPKEY,

code'=$_GET['code']

);

//- 构造请求访问令牌的全球资源定位器(统一资源定位器)

$ TOKEN _ URL=self:GET _ ACCESS _ TOKEN _ URL .'?'。http _ build _ query($ key sarr);

$ response=$ this-get _ contents($ token _ URL);

if(strpos($response,' callback ')!==假){

$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 '];

}

公共函数get_contents($url){

if(ini _ get(' allow _ URL _ fopen ')==' 1 '){

$ response=file _ get _ contents($ URL);

}否则{

$ 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 $ response

}

公共函数get_openid(){

//- 请求参数列表

$keysArr=array(

access _ token '=$ _ SESSION[' access _ token ']

);

$ graph _ URL=self:GET _ OPENID _ URL .'?'。http _ build _ query($ key sarr);

$ response=$ this-get _ contents($ graph _ URL);

//- 检测错误是否发生

if(strpos($response,' callback ')!==假){

$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;

返回$ user-OpenID;

}

/**

*显示错误

* 显示错误信息

* @param int $code错误代码

* @ param string $描述描述信息(可选)

*/

公共函数showError($code,$description='$'){

echo ' meta charset=' UTF-8 ';

回显“H3错误:/H3 $代码”;

echo“h3msg:/H3 $ description”;

exit();

}

/**

* _呼叫

* 魔术方法,做美国石油学会(美国石油协会)调用转发

* @param string $name调用的方法名称

* @param array $arg参数列表数组

* @从5.0开始

* @返回数组返加调用结果数组

*/

公共函数__call($name,$arg){

//如果APIMap不存在相应的美国石油学会(美国石油协会)

if(empty($ this-API映射[$ name]){

$this-showError('api调用名称错误','不存在的API:span style=' color:red;$ name/span ');

}

//从APIMap获取美国石油学会(美国石油协会)相应参数

$ base URL=$ this-API映射[$ name][0];

$ args list=$ this-API map[$ name][1];

$ method=isset($ this-API map[$ name][2])?$ this-API映射[$ name][2]:' GET ';

if(empty($arg)){

$ arg[0]=null;

}

$ response arr=JSON _ decode($ this-_ apply API($ arg[0],$argsList,$baseUrl,$method),true);

//检查返回浸水使柔软判断美国石油学会(美国石油协会)是否成功调用

if($responseArr['ret']==0){

return $ responseArr

}否则{

$ this-show error($ responseArr[' ret '],$ responseArr[' msg ']);

}

}

//调用相应美国石油学会(美国石油协会)

private function _applyAPI($arr,$argsList,$baseUrl,$method){

$ pre=' #

$ key sarr=$ this-key sarr;

$ optionArgList=array();//一些多项选填参数必选一的情形

foreach($argsList as $key=$val){

$ tmpKey=$ key

$ tmpVal=$ val

如果(!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],'',$ tmp key);

$ optionArgList[]=$ tmp key;

}

}否则{

$ tmpVal=null

}

}

//- 如果没有设置相应的参数

如果(!isset($ arr[$ tmp key])| | $ arr[$ tmp key]===' '){

if($tmpVal==$pre){

继续;

}else if($tmpVal){//则使用默认的值

$ arr[$ tmp key]=$ tmp val;

}否则{

$this-showError('api调用参数错误','未传入参数$ tmpKey’);

}

}

$ key sarr[$ tmp key]=$ arr[$ tmp key];

}

//检查选填参数必填一的情形

if(count($optionArgList)!=0){

$ n=0;

foreach($optionArgList as $val){

if(in_array($val,array _ keys($ keysArr)){

$ n;

}

}

如果(!$n){

$str=内爆(',',$ optionArgList);

$this-showError('api调用参数错误,$ str . 0 '必填一个');

}

}

if($method=='POST'){

$response=$this-post($baseUrl,$keysArr,0);

}else if($method=='GET'){

$baseUrl=$baseUrl .'?'。http _ build _ query($ key sarr);

$ response=$ this-get _ contents($ base URL);

}

return $ response

}

公共函数post($url,$keysArr,$flag=0){

$ ch=curl _ init();

如果(!$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,$ key sarr);

curl_setopt($ch,CURLOPT_URL,$ URL);

$ ret=curl _ exec($ ch);

curl _ close($ ch);

返回$ ret

}

}

方法三,面向对象使用腾讯给的软件开发工具包(软件开发工具包)

使用方法:腾讯SDK、API写的很详细,不做赘述

地址:http://wiki.connect.qq.com/网站接入概述

这样就实现了即时通信软件快捷登录,其实很简单的,大家可以试一试。

还有什么不清楚的,可以看看官方介绍,更详细,

Tips:如何在本地测试即时通信软件快速登录

方法:修改宿主配置文件

1.打开c:\ Windows \ System32 \ drivers \ etc \ host

2.添加127 .0 .0 .1 www.test.com

然后操作就可以了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

相关文章阅读

  • webqq网页版登录入口,web版qq登录
  • webqq网页版登录入口,web版qq登录,WebQQ最新登陆协议的用法
  • qq邮箱收不到epic邮件,epicqq邮箱显示无效
  • QQ群课堂怎么设置回放,qq的群课堂怎么回放
  • 企业qq聊天记录找回,腾讯企点聊天记录怎么恢复
  • QQ空间评论特效,
  • qq空间回复特效怎么设置,,完美实现仿QQ空间评论回复特效
  • QQ强制聊天网页版,qq强制聊天器网页版
  • QQ强制聊天网页版,qq强制聊天器网页版,QQ强制聊天功能代码(加强版,兼容QQ2010)
  • QQ尾巴病毒,qq尾巴病毒是什么
  • QQ尾巴病毒,qq尾巴病毒是什么,QQ尾巴病毒核心技术的实现
  • QQ封号漏洞,腾讯的威胁
  • QQ封号漏洞,腾讯的威胁,惊!QQ新漏洞 腾讯3亿用户遭受威胁 附说明
  • qq密码被盗怎么找回,最简单的方法,qq密码被盗了怎么找回来吗密保手机也改了
  • qq密码被盗怎么找回,最简单的方法,qq密码被盗了怎么找回来吗密保手机也改了,QQ密码被盗怎么办 (教你如何找回QQ密码)
  • 留言与评论(共有 条评论)
       
    验证码: