亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 3504 | 回復(fù): 1
打印 上一主題 下一主題

QQ互聯(lián)OAuth [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2015-07-03 10:55 |只看該作者 |倒序?yàn)g覽
[PHP]代碼
  1. /**
  2. * QQ互聯(lián) 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.     //用戶授權(quán)之后的回調(diào)地址
  18.     public $redirectUri = null;
  19.      
  20.     // App Id
  21.     public $appid = null;
  22.      
  23.     //App Key
  24.     public $appKey = null;
  25.      
  26.     //授權(quán)列表
  27.     //字符串,多個(gè)用逗號(hào)隔開
  28.     public $scope = null;
  29.      
  30.     //授權(quán)code
  31.     public $code = null;
  32.      
  33.     //續(xù)期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內(nèi)容
  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內(nèi)容
  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.      * 驗(yàn)證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接口的返回?cái)?shù)據(jù)中解析出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. }
復(fù)制代碼

論壇徽章:
59
2015七夕節(jié)徽章
日期:2015-08-24 11:17:25ChinaUnix專家徽章
日期:2015-07-20 09:19:30每周論壇發(fā)貼之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38榮譽(yù)版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年紀(jì)念徽章
日期:2015-07-20 11:05:27IT運(yùn)維版塊每日發(fā)帖之星
日期:2015-07-20 11:05:34操作系統(tǒng)版塊每日發(fā)帖之星
日期:2015-07-20 11:05:36程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2015-07-20 11:05:40數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2015-07-20 11:05:432015年辭舊歲徽章
日期:2015-07-20 11:05:44
2 [報(bào)告]
發(fā)表于 2015-07-10 10:08 |只看該作者
現(xiàn)在使用第三方驗(yàn)證很重要。
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP