QQ登录OAuth2.0 php接入种
QQ登录OAuth2.0 php接入类
?
/** ? * ? * qq登录 ? * @author http://www.heui.org ? * ? */ class Oauth_qq { ?? private static $_instance ; ?? private $config = array (); ?
?? private function __construct( $config ) ?? { ???? $this ->Oauth_qq( $config ); ?? } ?
?? public static function getInstance( $config ) ?? { ???? if (!isset(self:: $_instance )) ???? { ?????? $c = __CLASS__ ; ?????? self:: $_instance = new $c ( $config ); ???? } ???? return self:: $_instance ; ?? } ?
?? private function Oauth_qq( $config ) ?? { ???? $this ->config = $config ; ???? $_SESSION [ "appid" ]??? = $this ->config[ 'appid' ]; ???? $_SESSION [ "appkey" ]?? = $this ->config[ 'appkey' ]; ???? $_SESSION [ "callback" ] = $this ->config[ 'callback' ]; ???? $_SESSION [ "scope" ] = "get_user_info,add_share,list_album,add_album,upload_pic,add_topic,add_one_blog,add_weibo" ; ?? } ?
?? function login() ?? { ???? $_SESSION [ 'state' ] = md5(uniqid(rand(), TRUE)); //CSRF protection ???? $login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" ???? . $_SESSION [ "appid" ] . "&redirect_uri=" . urlencode( $_SESSION [ "callback" ]) ???? . "&state=" . $_SESSION [ 'state' ] ???? . "&scope=" . $_SESSION [ "scope" ]; ???? header( "Location:$login_url" ); ?? } ?
?? function callback() ?? { ???? if ( $_REQUEST [ 'state' ] == $_SESSION [ 'state' ]) //csrf ???? { ?????? $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&" ?????? . "client_id=" . $_SESSION [ "appid" ]. "&redirect_uri=" . urlencode( $_SESSION [ "callback" ]) ?????? . "&client_secret=" . $_SESSION [ "appkey" ]. "&code=" . $_REQUEST [ "code" ]; ?
?????? $response = get_url_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)) ???????? { ?????????? echo "<h3 id="error">error:</h3>" . $msg ->error; ?????????? echo "<h3 id="msg">msg? :</h3>" . $msg ->error_description; ?????????? exit ; ???????? } ?????? } ?
?????? $params = array (); ?????? parse_str ( $response , $params ); ?
?????? $_SESSION [ "access_token" ] = $params [ "access_token" ]; ???? } ???? else ???? { ?????? echo ( "The state does not match. You may be a victim of CSRF." ); ???? } ?? } ?
?? function get_openid() ?? { ???? $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=" ???? . $_SESSION [ 'access_token' ]; ?
???? $str ? = get_url_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 ); ???? if (isset( $user ->error)) ???? { ?????? echo "<h3 id="error">error:</h3>" . $user ->error; ?????? echo "<h3 id="msg">msg? :</h3>" . $user ->error_description; ?????? exit ; ???? } ?
???? //set openid to session ???? return $_SESSION [ "openid" ] = $user ->openid; ?? } ?
?? function get_user_info() ?? { ???? $get_user_info = "https://graph.qq.com/user/get_user_info?" ???? . "access_token=" . $_SESSION [ 'access_token' ] ???? . "&oauth_consumer_key=" . $_SESSION [ "appid" ] ???? . "&openid=" . $_SESSION [ "openid" ] ???? . "&format=json" ; ?
???? $info = get_url_contents( $get_user_info ); ???? $arr = json_decode( $info , true); ?
???? return $arr ; ?? } ?
?? public function __clone() ?? { ???? trigger_error( 'Clone is not allow' ,E_USER_ERROR); ?? } ?
} ?
/* 公用函数 */ if (!function_exists( "do_post" )) { ?? function do_post( $url , $data ) ?? { ???? $ch = curl_init(); ???? curl_setopt( $ch , CURLOPT_RETURNTRANSFER, TRUE); ???? curl_setopt( $ch , CURLOPT_POST, TRUE); ???? curl_setopt( $ch , CURLOPT_POSTFIELDS, $data ); ???? curl_setopt( $ch , CURLOPT_URL, $url ); ???? $ret = curl_exec( $ch ); ?
???? curl_close( $ch ); ???? return $ret ; ?? } } if (!function_exists( "get_url_contents" )) { ?? function get_url_contents( $url ) ?? { ???? if ( ini_get ( "allow_url_fopen" ) == "1" ) ???? return file_get_contents ( $url ); ?
???? $ch = curl_init(); ???? curl_setopt( $ch , CURLOPT_RETURNTRANSFER, TRUE); ???? curl_setopt( $ch , CURLOPT_URL, $url ); ???? $result =? curl_exec( $ch ); ???? curl_close( $ch ); ?
???? return $result ; ?? } } |
使用实例:
$config [ 'appid' ]??? = '' ; $config [ 'appkey' ]?? = '' ; $config [ 'callback' ] = '' ; $o_qq = Oauth_qq::getInstance( $config ); ?
//then $o_qq ->login(); //or $o_qq ->callback(); $o_qq ->get_openid(); $o_qq ->get_user_info();
|
原文:http://www.heui.org/archives/454

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

PHP函数介绍—get_headers():获取URL的响应头信息概述:在PHP开发中,我们经常需要获取网页或远程资源的响应头信息。PHP函数get_headers()能够方便地获取目标URL的响应头信息,并以数组形式返回。本文将介绍get_headers()函数的用法,以及提供一些相关的代码示例。get_headers()函数的用法:get_header

报错的原因NameResolutionError(self.host,self,e)frome是由urllib3库中的异常类型,这个错误的原因是DNS解析失败,也就是说,试图解析的主机名或IP地址无法找到。这可能是由于输入的URL地址不正确,或者DNS服务器暂时不可用导致的。如何解决解决此错误的方法可能有以下几种:检查输入的URL地址是否正确,确保它是可访问的确保DNS服务器可用,您可以尝试在命令行中使用"ping"命令来测试DNS服务器是否可用尝试使用IP地址而不是主机名来访问网站如果是在代理

session失效通常是由于 session 的生存时间过期或者服务器关闭导致的。其解决办法:1、延长session的生存时间;2、使用持久化存储;3、使用cookie;4、异步更新session;5、使用会话管理中间件。

function是函数的意思,是一段具有特定功能的可重复使用的代码块,是程序的基本组成单元之一,可以接受输入参数,执行特定的操作,并返回结果,其目的是封装一段可重复使用的代码,提高代码的可重用性和可维护性。

区别:1、定义不同,url是是统一资源定位符,而html是超文本标记语言;2、一个html中可以有很多个url,而一个url中只能存在一个html页面;3、html指的是网页,而url指的是网站地址。

PHPSession跨域问题的解决方法在前后端分离的开发中,跨域请求已成为常态。在处理跨域问题时,我们通常会涉及到session的使用和管理。然而,由于浏览器的同源策略限制,跨域情况下默认情况下无法共享session。为了解决这个问题,我们需要采用一些技巧和方法来实现session的跨域共享。一、使用cookie跨域共享session最常

1.基于session实现短信登录1.1短信登录流程图1.2实现发送短信验证码前端请求说明:说明请求方式POST请求路径/user/code请求参数phone(电话号码)返回值无后端接口实现:@Slf4j@ServicepublicclassUserServiceImplextendsServiceImplimplementsIUserService{@OverridepublicResultsendCode(Stringphone,HttpSessionsession){//1.校验手机号if

JavaScriptCookie使用JavaScriptcookie是记住和跟踪偏好、购买、佣金和其他信息的最有效方法。更好的访问者体验或网站统计所需的信息。PHPCookieCookie是存储在客户端计算机上的文本文件并保留它们用于跟踪目的。PHP透明地支持HTTPcookie。JavaScriptcookie如何工作?您的服务器将一些数据发送到访问者的浏览器cookie的形式。浏览器可以接受cookie。如果存在,它将作为纯文本记录存储在访问者的硬盘上。现在,当访问者到达站点上的另一个页面时
