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

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

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

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

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

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

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

随着互联网技术的不断发展,越来越多的应用需要进行OAuth2认证,其中PHP中的GuzzleHttp是一种常用的HTTP请求库,如何使用GuzzleHttp进行OAuth2认证呢?本文将详细介绍GuzzleHttp的OAuth2认证相关使用方法。1.安装GuzzleHttp使用Composer安装GuzzleHttp:composerrequireguz


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

ホットトピック



