/**
* 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 = )
{
$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'];
}
}

QQ互联OAuth的更多相关文章

  1. PHP版QQ互联OAuth示例代码分享

    )   {     $ch = curl_init();     if(! $flag) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);     cu ...

  2. QQ互联登录回调路径错误redirect uri is illegal(100010)

    QQ互联登录设置的路径设置

  3. QQ互联OAuth2.0 .NET SDK 发布以及网站QQ登陆示例代码(转)

    OAuth: OAuth(开放授权)是一个开放标准,允许用户授权第三方网站访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方网站或分享他们数据的所有内容. QQ登录OAuth2 ...

  4. QQ互联OAuth2.0 .NET SDK 发布以及网站QQ登陆示例代码

    OAuth: OAuth(开放授权)是一个开放标准,允许用户授权第三方网站访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方网站或分享他们数据的所有内容. QQ登录OAuth2 ...

  5. 一元云购qq互联回调地址错误解决办法

    经过追踪,点击登录后调用 system/modules/api/下面的qqlogin.action.class.php 里面又调用了qq 互联php接口样例里的QC.php的QC类的方法qq_logi ...

  6. 登陆整合实现-QQ互联认证(ASP.NET版本)

    原文:登陆整合实现-QQ互联认证(ASP.NET版本) 首先 我们创建一个qq.ashx的页面,这个页面会跳转到QQ的请求界面 代码如下: QQSettingConfig qqSettingConfi ...

  7. QQ互联登录提示redirect uri is illegal(100010)完美解决方法

    大概2015年3月低,腾讯QQ互联开发平台调整了有关QQ登录应用回调地址填写规则,用来修复QQ登录过程因回调地址的漏洞可能导致存在的安全问题. 博主接触这块较多,但也是四月才了解此事,从4月起,所有新 ...

  8. QQ登录整合/oauth2.0认证-02-跳转到QQ互联页

    ---------------------------目录---------------------------------- QQ登录整合/oauth2.0认证-01-申请appkey和appid ...

  9. QQ互联

    [移动应用接入概述] QQ互联开放平台为第三方移动应用提供了丰富的API.第三方移动应用接入QQ互联开放平台后,即可通过调用平台提供的API实现用户使用QQ账号登录移动应用功能,且可以获取到腾讯QQ用 ...

随机推荐

  1. 3D音效

    摘自:http://baike.baidu.com/view/1330437.htm?fr=aladdin 3D音效就是用扬声器仿造出似乎存在但是虚构的声音.例如扬声器仿造头顶上有一架飞机从左至右飞过 ...

  2. ubuntu下配置apache2多域名(apache2.4.6)

    Ubuntu 在 Linux 各发行版中, 个人用户数量最多的. 很多人在本机和虚拟机中使用. 但 Ubuntu 和 Redhat 的 VirtualHost 设置方法不相同. 1. 打开目录 /et ...

  3. JavaScript Window对象

    1.Window对象的location属性引用的是Location对象,它表示该窗口中当前显示的文档的URL,并定义了方法来使窗口载入新的文档.Location对象的href属性是一个字符串,后者包含 ...

  4. Sublime Text编辑工具带有 PEP 8 格式检测插件

    Sublime Text编辑工具带有 PEP 8 格式检测插件

  5. poj 1028

    http://poj.org/problem?id=1028 题意(水):做一个游览器的历史记录. back:后退到上一个页面,当上一个页面没有时,输出ignored. forward:向前一个页面, ...

  6. sizeof进行结构体大小的判断

    typedef struct{    int a;    char b;}A_t;typedef struct{    int a;    char b;    char c;}B_t;typedef ...

  7. 解读Unity中的CG编写Shader系列十 (光滑的镜面反射(冯氏着色))

    前文完成了最基本的镜面反射着色器,单平行光源下的逐顶点着色(per-vertex lighting),又称为古罗着色(Gouraud shading).这篇文章作为后续讨论更光滑的镜面反射方式,逐像素 ...

  8. 【leetcode】LRU Cache(hard)★

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  9. 20145213《Java程序设计》实验五Java网络编程及安全

    20145213<Java程序设计>实验五Java网络编程及安全 实验内容 1.掌握Socket程序的编写. 2.掌握密码技术的使用. 3.设计安全传输系统. 实验预期 1.客户端与服务器 ...

  10. October 1st 2016 Week 40th Saturday

    Autumn, the year's last, loveliest smile. 秋,四季流转中的最后一抹,也是最美的那一抹微笑. I love autumn because it is the h ...