免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3414 | 回复: 1
打印 上一主题 下一主题

QQ互联OAuth [复制链接]

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

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
2 [报告]
发表于 2015-07-10 10:08 |只看该作者
现在使用第三方验证很重要。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP