PHP版QQ互联OAuth示例代码分享_PHP
由于国内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']; } }
以上所述就是本文的全部内容了,希望大家能够喜欢。

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

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

如何使用PHP和OAuth进行GoogleDrive集成GoogleDrive是一款流行的云存储服务,它允许用户在云端存储文件并与其他用户共享。通过GoogleDriveAPI,我们可以使用PHP编写代码来与GoogleDrive进行集成,实现文件的上传、下载、删除等操作。要使用GoogleDriveAPI,我们需要通过OAuth进行身份验证并

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

PHP中的OAuth2鉴权方法及实现方式随着互联网的发展,越来越多的应用程序需要与第三方平台进行交互。为了保护用户的隐私和安全,许多第三方平台使用OAuth2协议来实现用户鉴权。在本文中,我们将介绍PHP中的OAuth2鉴权方法及实现方式,并附上相应的代码示例。OAuth2是一种授权框架,它允许用户授权第三方应用程序访问其在另一个服务提供商上的资源,而无需提

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

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

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