PHP版QQ互联OAuth示例代码分享
- /**
- * QQ互联 oauth
- * @author dyllen
- * @edit http://www.lai18.com
- * @date 2015-07-06
- */
- 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 = )
- {
- $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版QQ互联OAuth示例代码分享的更多相关文章
- QQ互联OAuth
/** * QQ互联 oauth * @author dyllen * */ class Oauth { //取Authorization Code Url const PC_CODE_URL = ' ...
- android 集成QQ互联 (登录,分享)
参考:http://blog.csdn.net/syz8742874/article/details/39271117 http://blog.csdn.net/woblog/article/deta ...
- [转] VS2015中跑OpenGL红宝书第八版的第一章示例代码,运行
Ori Article Link OpenGL的东西快忘光了,把角落的第八版红宝书拿出来复习一下 从书中的地址下了个示例代码结果新系统(Win10+VS2015)各种跑不起来,懊恼之后在网上疯狂搜索资 ...
- Android版多线程下载器核心代码分享
首先给大家分享多线程下载核心类: package com.example.urltest; import java.io.IOException; import java.io.InputStream ...
- QQ互联OAuth2.0 .NET SDK 发布以及网站QQ登陆示例代码(转)
OAuth: OAuth(开放授权)是一个开放标准,允许用户授权第三方网站访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方网站或分享他们数据的所有内容. QQ登录OAuth2 ...
- QQ互联OAuth2.0 .NET SDK 发布以及网站QQ登陆示例代码
OAuth: OAuth(开放授权)是一个开放标准,允许用户授权第三方网站访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方网站或分享他们数据的所有内容. QQ登录OAuth2 ...
- 微软发布手机版 Sample Browser。7000多示例代码一手掌握
今天早上,Sample Browser发布其全新的Windows Phone版本.至此,自2012年相继推出Desktop版.Visual Studio版,以及2013年推出Windows Store ...
- 开发QQ互联ios版Ane扩张 辛酸史
来源:http://www.myexception.cn/operating-system/1451490.html 开发QQ互联ios版Ane扩展 辛酸史 开发QQ互联ios版Ane扩展辛酸史: 1 ...
- 基于DotNetOpenAuth的OAuth实现示例代码: 获取access token
1. 场景 根据OAuth 2.0规范,该场景发生于下面的流程图中的(D)(E)节点,根据已经得到的authorization code获取access token. 2. 实现环境 DotNetOp ...
随机推荐
- win2008r2激活码
我这有三个 以前用过可以 现在不知道能不能用 你试试BBFP3-49FVF-TJB8F-V26V6-DJPX9 CXTFT-74V4Y-9D48T-2DMFW-TX7CYGYF3T-H2V88-GRP ...
- TCP的那些事儿(下)
TCP的那些事儿(下) 这篇文章是下篇,所以如果你对TCP不熟悉的话,还请你先看看上篇<TCP的那些事儿(上)> 上篇中,我们介绍了TCP的协议头.状态机.数据重传中的东西.但是TCP要解 ...
- WINDOWS渗透与提权总结(1)
旁站路径问题: 1.读网站配置. 2.用以下VBS: 01 On Error Resume Next 02 03 If (LCase(Right(WScript.Fullname, 11)) = ...
- WPF 获取IP地址
List<string> ipList = new List<string>(); foreach (IPAddress _IPAddress in Dns.GetHostEn ...
- php 开启curl,重启php-fpm服务
1,找到php.ini配置 find / -name 'php.ini' /usr/local/php/etc/php.ini 找到extension=php_curl.dll 把前面的分号去掉即可. ...
- E. Tetrahedron(数学推导)
E. Tetrahedron 分类: AC路漫漫2013-08-08 16:07 465人阅读 评论(0) 收藏 举报 time limit per test 2 seconds memory lim ...
- web_url、web_custom_request、web_submit_data、web_submit_form的使用实例
业务:根据url获取图片用web_url()函数访问数据;请求方式:HTTP GET 请求;Action(){web_url("imageproxytest", "URL ...
- [Effective JavaScript 笔记]第62条:在异步序列中使用嵌套或命名的回调函数
异步程序的操作顺序 61条讲述了异步API如何执行潜在的代价高昂的I/O操作,而不阻塞应用程序继续处理其他输入.理解异步程序的操作顺序刚开始有点混乱.例如,下面的代码会在打印"finishe ...
- Unbuntu 下编译安装 PHP 必要库的安装
2010/08/22 LINUX, PHP 2 COMMENTS 编译环境 sudo apt-get install build-essential xml sudo apt-get install ...
- Ubuntu 下 LAMP 的配置文件路径 转:
配置文件路径: 1>apache 的配置文件路径 /etc/apache2/apache2.conf 2>apache 网站字符编码配置路径 /etc/apache2/conf.d/c ...