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 ...
随机推荐
- linux查看内核版本、系统版本、系统位数(32or64)
linux查看内核版本.系统版本.系统位数(32or64) 2011-05-01 22:05:12 标签:linux 内核版本 休闲 系统版本 系统位数 1. 查看内核版本命令: 1) [root@ ...
- gcc的-D和-U参数:宏的设置与取消 _CCFLAGS=" -w -enable-threads=posix -DLINUX -D_REENTRANT -DWORKONGN -Dlinux -D_GN_DETAIL_SDR_"
_CCFLAGS=" -w -enable-threads=posix -DLINUX -D_REENTRANT -DWORKONGN -Dlinux -D_GN_DETAIL_SDR_&q ...
- ubuntu快速清理磁盘垃圾
.快速清理磁盘垃圾 磁盘空间又不够用了?尝试在终端窗口中输入sudo apt-get autoremove然后输入sudo apt-get clean,前一个命令会卸载系统中所有未被使用的依赖关系,后 ...
- NGUI 学习笔记实战——制作商城UI界面
http://www.cnblogs.com/chongxin/p/3876575.html Unity3D的uGUI听说最近4.6即将推出,但是目前NGUI等UI插件大行其道并且已经非常成熟,所以我 ...
- ios开发者到真机测试
ios就是矫情, 没事搞那么多步奏, 搞得我都不会弄了, 不懈努力后还是弄好了, 总结一下, 避免新人走弯路. 苹果的脾气就是这样, 只能慢慢学了 1. 生成CSR (开发者证书认证请求) 打开钥匙 ...
- 基于贪心算法的几类区间覆盖问题 nyoj 12喷水装置(二) nyoj 14会场安排问题
1)区间完全覆盖问题 问题描述:给定一个长度为m的区间,再给出n条线段的起点和终点(注意这里是闭区间),求最少使用多少条线段可以将整个区间完全覆盖 样例: 区间长度8,可选的覆盖线段[2,6],[1, ...
- Controller之间传递数据:协议传值
http://itjoy.org/?p=416 前边介绍过从第一个页面传递数据到第二个页面,那么反过来呢我们该如何操作?还是同一个例子,将第二个页面的字符串传递到第一个页面显示出来,这中形式就可以使用 ...
- cookie注入讲解
我们首先还是来看看中网景论坛的最新版本"(CNKBBS2007)中网景论坛2007v5.0 "官方下载地址" http://www.cnetking.com/websys ...
- 《linux备份与恢复之二》3.19 dump(文件系统备份)
<Linux指令从初学到精通>第3章文件管理,本章介绍了许多常用命令,如cp.ln.chmod.chown.diff.tar.mv等,因为这些都与文件管理相关,在日常的使用中经常用到,因此 ...
- 【云计算】开源的Docker Registry WebUI
kwk/docker-registry-frontend Code Issues 9 Pull requests 6 Wiki ...