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下web目录权限设置
1.nginx和php-fpm运行用户为www 2.我们假设web目录所属着为web_owner 3.将web目录的用户和用户组设置为web_owner和www,如下命令:chown -R web_o ...
- 10.23lamp环境
前序: 查考文章:http://www.cnblogs.com/mchina/archive/2012/11/28/2778779.html http://www.centos.bz/2011/09/ ...
- WhatsApp值160亿美元,腾讯推大众点评微信支付!
腾讯前脚刚入股大众点评,FB后脚就将斥资160亿美元收购WhatsApp(40亿美元现金和120亿美元股票). 为什么WhatsApp值160亿美元?这是什么东东呢?WhatsApp这款服务可以帮助用 ...
- 关于showModalDialog()对话框点击按钮弹出新页面的问题
页面a.aspx上,单击按钮a,走脚本,弹出showModalDialog("b.aspx",....) 在b.aspx上有个服务器控件按钮b,单击按钮,更新数据后,会弹出一个新的 ...
- Android学习笔记之打钩显示输入的密码
利用EditText作为密码输入框是个不错的选择(只需设置输入类型为textPassword即可),保密且无需担心被盗取.但有时用户也不知道自己输入的是否正确,这时就应该提供一个“显示密码”的复选框, ...
- DrawText
该函数在指定的矩形里写入格式化的正文,根据指定的方法对正文格式化(扩展的制表符,字符对齐.折行等). int DrawText(HDC hDC, // 设备描述表句柄 LPCTSTR lpStri ...
- Step
php+MySQL html+css JQuery Mobile JavaScript weiPHP Sina Cloud 微信公众订阅号平台开发
- 一张图说明该选用神马程式来serve你的django应用
- How to install OpenResty
How to install OpenResty 15 January 2014, 6:18 am OpenResty, also called “ngx_openresty”, is a web ...
- 学习jquery mobile
学习jquery mobile的时间不是很长,在学习的过程当中也遇到了很多令人抓狂的问题,在网上搜索问题答案的时候发现,现在关于jquery mobile的文章还不是很多,所以,我也是一边学习,一边摸 ...