由于国内QQ用户的普遍性,所以现在各大网站都尽可能的提供QQ登陆口,下面我们来看看php版,给大家参考下
/** * QQ互联 oauth * @author dyllen * */ class Oauth { //取Authorization Code Url const PC_CODE_URL = 'https://graph.qq.com/oauth2.0/authorize'; //取Access Token Url const PC_ACCESS_TOKEN_URL = 'https://graph.qq.com/oauth2.0/token'; //取用户 Open Id Url const OPEN_ID_URL = 'https://graph.qq.com/oauth2.0/me'; //用户授权之后的回调地址 public $redirectUri = null; // App Id public $appid = null; //App Key public $appKey = null; //授权列表 //字符串,多个用逗号隔开 public $scope = null; //授权code public $code = null; //续期access token的凭证 public $refreshToken = null; //access token public $accessToken = null; //access token 有效期,单位秒 public $expiresIn = null; //state public $state = null; public $openid = null; //construct public function __construct($config=[]) { foreach($config as $key => $value) { $this->$key = $value; } } /** * 得到获取Code的url * @throws \InvalidArgumentException * @return string */ public function codeUrl() { if (!$this->redirectUri) { throw new \Exception('parameter $redirectUri must be set.'); } $query = [ 'response_type' => 'code', 'client_id' => $this->appid, 'redirect_uri' => $this->redirectUri, 'state' => $this->getState(), 'scope' => $this->scope, ]; return self::PC_CODE_URL . '?' . http_build_query($query); } /** * 取access token * @throws Exception * @return boolean */ public function getAccessToken() { $params = [ 'grant_type' => 'authorization_code', 'client_id' => $this->appid, 'client_secret' => $this->appKey, 'code' => $this->code, 'redirect_uri' => $this->redirectUri, ]; $url = self::PC_ACCESS_TOKEN_URL . '?' . http_build_query($params); $content = $this->getUrl($url); parse_str($content, $res); if ( !isset($res['access_token']) ) { $this->thrwoError($content); } $this->accessToken = $res['access_token']; $this->expiresIn = $res['expires_in']; $this->refreshToken = $res['refresh_token']; return true; } /** * 刷新access token * @throws Exception * @return boolean */ public function refreshToken() { $params = [ 'grant_type' => 'refresh_token', 'client_id' => $this->appid, 'client_secret' => $this->appKey, 'refresh_token' => $this->refreshToken, ]; $url = self::PC_ACCESS_TOKEN_URL . '?' . http_build_query($params); $content = $this->getUrl($url); parse_str($content, $res); if ( !isset($res['access_token']) ) { $this->thrwoError($content); } $this->accessToken = $res['access_token']; $this->expiresIn = $res['expires_in']; $this->refreshToken = $res['refresh_token']; return true; } /** * 取用户open id * @return string */ public function getOpenid() { $params = [ 'access_token' => $this->accessToken, ]; $url = self::OPEN_ID_URL . '?' . http_build_query($params); $this->openid = $this->parseOpenid( $this->getUrl($url) ); return $this->openid; } /** * get方式取url内容 * @param string $url * @return mixed */ public function getUrl($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); return $response; } /** * post方式取url内容 * @param string $url * @param array $keysArr * @param number $flag * @return mixed */ public function postUrl($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; } /** * 取state * @return string */ protected function getState() { $this->state = md5(uniqid(rand(), true)); //state暂存在缓存里面 //自己定义 //。。。。。。。。。 return $this->state; } /** * 验证state * @return boolean */ protected function verifyState() { //。。。。。。。 } /** * 抛出异常 * @param string $error * @throws \Exception */ protected function thrwoError($error) { $subError = substr($error, strpos($error, "{")); $subError = strstr($subError, "}", true) . "}"; $error = json_decode($subError, true); throw new \Exception($error['error_description'], (int)$error['error']); } /** * 从获取openid接口的返回数据中解析出openid * @param string $str * @return string */ protected function parseOpenid($str) { $subStr = substr($str, strpos($str, "{")); $subStr = strstr($subStr, "}", true) . "}"; $strArr = json_decode($subStr, true); if(!isset($strArr['openid'])) { $this->thrwoError($str); } return $strArr['openid']; } }
以上所述就是本文的全部内容了,希望大家能够喜欢。

PHP中的OAuth:创建一个JWT授权服务器随着移动应用和前后端分离的趋势的兴起,OAuth成为了现代Web应用中不可或缺的一部分。OAuth是一种授权协议,通过提供标准化的流程和机制,用于保护用户的资源免受未经授权的访问。在本文中,我们将学习如何使用PHP创建一个基于JWT(JSONWebTokens)的OAuth授权服务器。JWT是一种用于在网络中

随着移动互联网的普及,越来越多的应用程序都需要用户进行身份验证和授权。OAuth2是一种流行的认证和授权框架,它为应用程序提供了一种标准化的机制来实现这些功能。LaravelPassport是一个易于使用,安全且开箱即用的OAuth2服务器实现,它为PHP开发人员提供了构建OAuth2身份验证和授权的强大工具。本文将介绍LaravelPassport的使

PHP和OAuth:实现微软登录集成随着互联网的发展,越来越多的网站和应用程序需要支持用户使用第三方账号登录,以提供方便的注册和登录体验。微软账号是全球范围内广泛使用的账号之一,许多用户希望使用微软账号登录网站和应用程序。为了实现微软登录集成,我们可以使用OAuth(开放授权)协议来实现。OAuth是一种开放标准的授权协议,允许用户授权第三方应用程序代表自己

随着API的使用逐渐普及,保护API的安全性和可扩展性变得越来越关键。而OAuth2已经成为了一种广泛采用的API安全协议,它允许应用程序通过授权来访问受保护的资源。为了实现OAuth2身份验证,LaravelPassport提供了一种简单、灵活的方式。在本篇文章中,我们将学习如何使用LaravelPassport实现APIOAuth2身份验证。Lar

随着互联网的不断发展,越来越多的应用程序都采用了分布式的架构方式进行开发。而在分布式架构中,鉴权是最为关键的安全问题之一。为了解决这个问题,开发人员通常采用的方式是实现OAuth2鉴权。SpringSecurityOAuth2是一个常用的用于OAuth2鉴权的安全框架,非常适合于JavaAPI开发。本文将介绍如何在JavaAPI开发

OAuth2.0是一种用来授权第三方应用程序访问用户资源的协议,现已被广泛应用于互联网领域。随着互联网业务的发展,越来越多的应用程序需要支持OAuth2.0协议。本文将介绍利用PHP实现OAuth2.0协议的最佳方式。一、OAuth2.0基础知识在介绍OAuth2.0的实现方式之前,我们需要先了解一些OAuth2.0的基础知识。授权类型OAuth2.0协议定

如何使用PHP和OAuth进行微信支付集成引言:随着移动支付的普及,微信支付已经成为了许多人首选的支付方式。在网站或者应用中集成微信支付,可以为用户提供便捷的支付体验。本文将介绍如何使用PHP和OAuth进行微信支付集成,并提供相关的代码示例。一、申请微信支付在使用微信支付之前,首先需要申请微信支付的商户号和相关密钥。具体的申请流程可参考微信支付的官方文档。

OAuth2是一个广泛使用的开放标准协议,用于在不将用户名和密码直接传输到第三方应用程序的情况下授权访问他们的用户资源,例如Google,Facebook和Twitter等社交网络。在PHP中,您可以使用现成的OAuth2库来轻松地实现OAuth2流程,或者您可以构建自己的库来实现它。在本文中,我们将重点关注使用现成的OAuth2库,如何通过它来使用OAut


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版
SublimeText3 Linux最新版

記事本++7.3.1
好用且免費的程式碼編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

禪工作室 13.0.1
強大的PHP整合開發環境