1. /**
  2.  * QQ互联 oauth
  3.  * @author dyllen
  4.  * @edit http://www.lai18.com
  5.  * @date 2015-07-06
  6.  */
  7. class Oauth
  8. {
  9.   //取Authorization Code Url
  10.   const PC_CODE_URL = 'https://graph.qq.com/oauth2.0/authorize';
  11.     
  12.   //取Access Token Url
  13.   const PC_ACCESS_TOKEN_URL = 'https://graph.qq.com/oauth2.0/token';
  14.     
  15.   //取用户 Open Id Url
  16.   const OPEN_ID_URL = 'https://graph.qq.com/oauth2.0/me';
  17.     
  18.   //用户授权之后的回调地址
  19.   public $redirectUri = null;
  20.     
  21.   // App Id
  22.   public $appid = null;
  23.     
  24.   //App Key
  25.   public $appKey = null;
  26.     
  27.   //授权列表
  28.   //字符串,多个用逗号隔开
  29.   public $scope = null;
  30.     
  31.   //授权code
  32.   public $code = null;
  33.     
  34.   //续期access token的凭证
  35.   public $refreshToken = null;
  36.     
  37.   //access token
  38.   public $accessToken = null;
  39.     
  40.   //access token 有效期,单位秒
  41.   public $expiresIn = null;
  42.     
  43.   //state
  44.   public $state = null;
  45.     
  46.   public $openid = null;
  47.     
  48.   //construct
  49.   public function __construct($config=[])
  50.   {
  51.     foreach($config as $key => $value) {
  52.       $this->$key = $value;
  53.     }
  54.   }
  55.     
  56.   /**
  57.    * 得到获取Code的url
  58.    * @throws \InvalidArgumentException
  59.    * @return string
  60.    */
  61.   public function codeUrl()
  62.   {
  63.     if (!$this->redirectUri) {
  64.       throw new \Exception('parameter $redirectUri must be set.');
  65.     }
  66.     $query = [
  67.         'response_type' => 'code',
  68.         'client_id' => $this->appid,
  69.         'redirect_uri' => $this->redirectUri,
  70.         'state' => $this->getState(),
  71.         'scope' => $this->scope,
  72.     ];
  73.     
  74.     return self::PC_CODE_URL . '?' . http_build_query($query);
  75.   }
  76.     
  77.   /**
  78.    * 取access token
  79.    * @throws Exception
  80.    * @return boolean
  81.    */
  82.   public function getAccessToken()
  83.   {
  84.     $params = [
  85.         'grant_type' => 'authorization_code',
  86.         'client_id' => $this->appid,
  87.         'client_secret' => $this->appKey,
  88.         'code' => $this->code,
  89.         'redirect_uri' => $this->redirectUri,
  90.     ];
  91.     
  92.     $url = self::PC_ACCESS_TOKEN_URL . '?' . http_build_query($params);
  93.     $content = $this->getUrl($url);
  94.     parse_str($content, $res);
  95.     if ( !isset($res['access_token']) ) {
  96.       $this->thrwoError($content);
  97.     }
  98.     
  99.     $this->accessToken = $res['access_token'];
  100.     $this->expiresIn = $res['expires_in'];
  101.     $this->refreshToken = $res['refresh_token'];
  102.     
  103.     return true;
  104.   }
  105.     
  106.   /**
  107.    * 刷新access token
  108.    * @throws Exception
  109.    * @return boolean
  110.    */
  111.   public function refreshToken()
  112.   {
  113.     $params = [
  114.         'grant_type' => 'refresh_token',
  115.         'client_id' => $this->appid,
  116.         'client_secret' => $this->appKey,
  117.         'refresh_token' => $this->refreshToken,
  118.     ];
  119.     
  120.     $url = self::PC_ACCESS_TOKEN_URL . '?' . http_build_query($params);
  121.     $content = $this->getUrl($url);
  122.     parse_str($content, $res);
  123.     if ( !isset($res['access_token']) ) {
  124.       $this->thrwoError($content);
  125.     }
  126.     
  127.     $this->accessToken = $res['access_token'];
  128.     $this->expiresIn = $res['expires_in'];
  129.     $this->refreshToken = $res['refresh_token'];
  130.     
  131.     return true;
  132.   }
  133.     
  134.   /**
  135.    * 取用户open id
  136.    * @return string
  137.    */
  138.   public function getOpenid()
  139.   {
  140.     $params = [
  141.         'access_token' => $this->accessToken,
  142.     ];
  143.     
  144.     $url = self::OPEN_ID_URL . '?' . http_build_query($params);
  145.         
  146.     $this->openid = $this->parseOpenid( $this->getUrl($url) );
  147.       
  148.     return $this->openid;
  149.   }
  150.     
  151.   /**
  152.    * get方式取url内容
  153.    * @param string $url
  154.    * @return mixed
  155.    */
  156.   public function getUrl($url)
  157.   {
  158.     $ch = curl_init();
  159.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  160.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  161.     curl_setopt($ch, CURLOPT_URL, $url);
  162.     $response = curl_exec($ch);
  163.     curl_close($ch);
  164.     
  165.     return $response;
  166.   }
  167.     
  168.   /**
  169.    * post方式取url内容
  170.    * @param string $url
  171.    * @param array $keysArr
  172.    * @param number $flag
  173.    * @return mixed
  174.    */
  175.   public function postUrl($url, $keysArr, $flag = )
  176.   {
  177.     $ch = curl_init();
  178.     if(! $flag) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  179.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  180.     curl_setopt($ch, CURLOPT_POST, TRUE);
  181.     curl_setopt($ch, CURLOPT_POSTFIELDS, $keysArr);
  182.     curl_setopt($ch, CURLOPT_URL, $url);
  183.     $ret = curl_exec($ch);
  184.     
  185.     curl_close($ch);
  186.     return $ret;
  187.   }
  188.     
  189.     
  190.   /**
  191.    * 取state
  192.    * @return string
  193.    */
  194.   protected function getState()
  195.   {
  196.     $this->state = md5(uniqid(rand(), true));
  197.     //state暂存在缓存里面
  198.     //自己定义
  199.         //。。。。。。。。。
  200.     
  201.     return $this->state;
  202.   }
  203.     
  204.   /**
  205.    * 验证state
  206.    * @return boolean
  207.    */
  208.   protected function verifyState()
  209.   {
  210.     //。。。。。。。
  211.   }
  212.     
  213.   /**
  214.    * 抛出异常
  215.    * @param string $error
  216.    * @throws \Exception
  217.    */
  218.   protected function thrwoError($error)
  219.   {
  220.     $subError = substr($error, strpos($error, "{"));
  221.     $subError = strstr($subError, "}", true) . "}";
  222.     $error = json_decode($subError, true);
  223.       
  224.     throw new \Exception($error['error_description'], (int)$error['error']);
  225.   }
  226.     
  227.   /**
  228.    * 从获取openid接口的返回数据中解析出openid
  229.    * @param string $str
  230.    * @return string
  231.    */
  232.   protected function parseOpenid($str)
  233.   {
  234.     $subStr = substr($str, strpos($str, "{"));
  235.     $subStr = strstr($subStr, "}", true) . "}";
  236.     $strArr = json_decode($subStr, true);
  237.     if(!isset($strArr['openid'])) {
  238.       $this->thrwoError($str);
  239.     }
  240.       
  241.     return $strArr['openid'];
  242.   }
  243. }

PHP版QQ互联OAuth示例代码分享的更多相关文章

  1. QQ互联OAuth

    /** * QQ互联 oauth * @author dyllen * */ class Oauth { //取Authorization Code Url const PC_CODE_URL = ' ...

  2. android 集成QQ互联 (登录,分享)

    参考:http://blog.csdn.net/syz8742874/article/details/39271117 http://blog.csdn.net/woblog/article/deta ...

  3. [转] VS2015中跑OpenGL红宝书第八版的第一章示例代码,运行

    Ori Article Link OpenGL的东西快忘光了,把角落的第八版红宝书拿出来复习一下 从书中的地址下了个示例代码结果新系统(Win10+VS2015)各种跑不起来,懊恼之后在网上疯狂搜索资 ...

  4. Android版多线程下载器核心代码分享

    首先给大家分享多线程下载核心类: package com.example.urltest; import java.io.IOException; import java.io.InputStream ...

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

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

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

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

  7. 微软发布手机版 Sample Browser。7000多示例代码一手掌握

    今天早上,Sample Browser发布其全新的Windows Phone版本.至此,自2012年相继推出Desktop版.Visual Studio版,以及2013年推出Windows Store ...

  8. 开发QQ互联ios版Ane扩张 辛酸史

    来源:http://www.myexception.cn/operating-system/1451490.html 开发QQ互联ios版Ane扩展 辛酸史 开发QQ互联ios版Ane扩展辛酸史: 1 ...

  9. 基于DotNetOpenAuth的OAuth实现示例代码: 获取access token

    1. 场景 根据OAuth 2.0规范,该场景发生于下面的流程图中的(D)(E)节点,根据已经得到的authorization code获取access token. 2. 实现环境 DotNetOp ...

随机推荐

  1. win2008r2激活码

    我这有三个 以前用过可以 现在不知道能不能用 你试试BBFP3-49FVF-TJB8F-V26V6-DJPX9 CXTFT-74V4Y-9D48T-2DMFW-TX7CYGYF3T-H2V88-GRP ...

  2. TCP的那些事儿(下)

    TCP的那些事儿(下) 这篇文章是下篇,所以如果你对TCP不熟悉的话,还请你先看看上篇<TCP的那些事儿(上)> 上篇中,我们介绍了TCP的协议头.状态机.数据重传中的东西.但是TCP要解 ...

  3. WINDOWS渗透与提权总结(1)

    旁站路径问题: 1.读网站配置. 2.用以下VBS: 01 On Error Resume Next 02   03 If (LCase(Right(WScript.Fullname, 11)) = ...

  4. WPF 获取IP地址

    List<string> ipList = new List<string>(); foreach (IPAddress _IPAddress in Dns.GetHostEn ...

  5. php 开启curl,重启php-fpm服务

    1,找到php.ini配置 find / -name 'php.ini' /usr/local/php/etc/php.ini 找到extension=php_curl.dll 把前面的分号去掉即可. ...

  6. E. Tetrahedron(数学推导)

    E. Tetrahedron 分类: AC路漫漫2013-08-08 16:07 465人阅读 评论(0) 收藏 举报 time limit per test 2 seconds memory lim ...

  7. web_url、web_custom_request、web_submit_data、web_submit_form的使用实例

    业务:根据url获取图片用web_url()函数访问数据;请求方式:HTTP GET 请求;Action(){web_url("imageproxytest", "URL ...

  8. [Effective JavaScript 笔记]第62条:在异步序列中使用嵌套或命名的回调函数

    异步程序的操作顺序 61条讲述了异步API如何执行潜在的代价高昂的I/O操作,而不阻塞应用程序继续处理其他输入.理解异步程序的操作顺序刚开始有点混乱.例如,下面的代码会在打印"finishe ...

  9. Unbuntu 下编译安装 PHP 必要库的安装

    2010/08/22 LINUX, PHP 2 COMMENTS 编译环境 sudo apt-get install build-essential xml sudo apt-get install ...

  10. Ubuntu 下 LAMP 的配置文件路径 转:

      配置文件路径: 1>apache 的配置文件路径 /etc/apache2/apache2.conf 2>apache 网站字符编码配置路径 /etc/apache2/conf.d/c ...