首頁 後端開發 php教程 PHP版QQ互聯OAuth範例程式碼分享

PHP版QQ互聯OAuth範例程式碼分享

Jul 25, 2016 am 08:45 AM

由於國內QQ用戶的普遍性,所以現在各大網站都盡可能的提供QQ登陸口,下面我們來看看php版,給大家參考下

  1. /**
  2. * QQ互聯 oauth
  3. * @author dyllen
  4. *
  5. */
  6. class Oauth
  7. {
  8. //取Authorization Code Url
  9. const PC_COhttps_URL = '取Authorization Code Url
  10. const PC_COhttps_URL = '取AuthorizationL = ' //graph.qq.com/oauth2.0/authorize';
  11. //取Access Token Url
  12. const PC_ACCESS_TOKEN_URL = 'https://graph.qq.com/oauth2.0/token' ;
  13. //取使用者Open Id Url
  14. const OPEN_ID_URL = 'https://graph.qq.com/oauth2.0/me';
  15. //使用者授權之後的回呼位址
  16. public $redirectUri = null;
  17. // App Id
  18. public $appid = null;
  19. //App Key
  20. public $appKey = null;
  21. //授權清單
  22. //字串,多個用逗號隔開
  23. public $scope = null;
  24. //授權code
  25. public $code = null ;
  26. //續期access token的憑證
  27. public $refreshToken = null;
  28. //access token
  29. public $accessToken = null;
  30. / /access token 有效期,單位秒
  31. public $expiresIn = null;
  32. //state
  33. public $state = null;
  34. public $openid = null;
  35. //construct
  36. public function __construct($config=[])
  37. {
  38. foreach($config as $key => $value) {
  39. $this->$key = $value ;
  40. }
  41. }
  42. /**
  43. * 得到取得Code的url
  44. * @throws InvalidArgumentException
  45. * @return string
  46. */
  47. public function codeUrl()
  48. {
  49. if (!$this->redirectUri) {
  50. {
  51. if (!$this->redirectUri) {
  52. throw new Exception('parameter $redirectUri must be set.');
  53. }
  54. $query = [
  55. 'response_type' => 'code',
  56. 'client_id' => $this- >appid,
  57. 'redirect_uri' => $this->redirectUri,
  58. 'state' => $this->getState(),
  59. 'scope' => $this->scope,
  60. ];
  61. return self::PC_CODE_URL . '?' . http_build_query($query);
  62. }
  63. /**
  64. * 取得存取權杖
  65. * @throws Exception
  66. * @return boolean
  67. */
  68. publicunc.
  69. {
  70. $params = [
  71. 'grant_type' => 'authorization_code',
  72. 'client_id' => $this->appid,
  73. 'client_secret' => $this->appKey ,
  74. 'code' => $this->code,
  75. 'redirect_uri' => $this->redirectUri,
  76. ];
  77. $url = self::PC_ACCESS_TOKEN_URL . '? ' . http_build_query($params);
  78. $content = $this->getUrl($url);
  79. parse_str($content, $res);
  80. if ( !isset($res['access_token' ]) ) {
  81. $this->thrwoError($content);
  82. }
  83. $this->accessToken = $res['access_token'];
  84. $this->expiresIn = $res['access_token'];
  85. $this->expiresIn = $res['access_token'];
  86. $this->expiresIn = $res['access_token'];
  87. $this->expiresIn = $res['access_token'];
  88. $this->expiresIn = $res['access_token'];
  89. $this->expiresIn = $res['access_token'];
  90. $this->expiresIn = $res['access_token'];
  91. $this->expiresIn = $res[’access_token'];
  92. $this->expiresIn = $res['expires_in'];
  93. $this->refreshToken = $res['refresh_token'];
  94. return true;
  95. }
  96. /**
  97. * 刷新存取權杖
  98. * @throws Exception
  99. * @return boolean
  100. */
  101. public function refreshToken()
  102. {
  103. $params = [
  104. 'grant_type' => 'refresh_token',
  105. 'client_id' => $this->appid,
  106. 'client_id' => $this->appid,
  107. 'client_secret' => $this->appKey,
  108. 'refresh_token' => $this->refreshToken,
  109. ];
  110. $url = self::PC_ACCESS_TOKEN_URL . '🎜> $url = self::PC_ACCESS_TOKEN_URL . '🎜> $url = self::PC_ACCESS_TOKEN_URL . '🎜 . ' $params);
  111. $content = $this->getUrl($url);
  112. parse_str($content, $res);
  113. if ( !isset($res['access_token']) ) {
  114. $this->thrwoError($content);
  115. }
  116. $this->accessToken = $res['access_token'];
  117. $this->expiresIn = $res[' expires_in'];
  118. $this->refreshToken = $res['refresh_token'];
  119. return true;
  120. }
  121. /** public function getOpenid()
  122. {
  123. $params = [
  124. 'access_token' => $this->accessToken,
  125. ];
  126. $url = self::🎜> ];
  127. $url = self::OPEN_ID_URL. '?' . http_build_query($params);
  128. $this->openid = $this->parseOpenid( $this->getUrl($url) );
  129. return $this-> openid;
  130. }
  131. /** * 取使用者open id * @return string*/ 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); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_URL, $url); $c. ($ch); return $response; }
  132. /**
  133. * 發佈 URL 內容
  134. * @param string $url
  135. * @param array $keysArr
  136. * @param number $flag
  137. * @return mix
  138. * @param number $flag
  139. * @return mix
  140. */
  141. public function postUrl($url, $keysArr, $flag = 0)
  142. {
  143. $ch = curl_init();
  144. if (!$flag)curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,FALSE);
  145. curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
  146. curl_setopt($ch,CURLOPT_POST,TRUE);
  147. curl_setopt($ch,CURLOPT_POSTFIELDS ) ) , $keysArr);
  148. curl_setopt($ch, CURLOPT_URL, $url);
  149. $ret = curl_exec($ch);
  150. curl_close($ch);
  151. return $
  152. curl_close($ch);
  153. ret ;
  154. }
  155. /**
  156. * 取得狀態
  157. * @return string
  158. */
  159. 受保護函數getState()
  160. {
  161. $this->state = md5(uniqid(rand() ) , true));
  162. //state暫存在儲存裡面
  163. //自己定義
  164. //。
  165. }
  166. /**
  167. * 驗證state
  168. * @return boolean
  169. */
  170. protected function verifyState()
  171. {
  172. //。 🎜> /**
  173. * 拋出例外
  174. * @param string $error
  175. * @throws Exception
  176. */
  177. 受保護函數thrwoError($error)
  178. {
  179. $subError = substr($error, strpos($error, "{"));
  180. $ subError = strstr($subError, "}", true) 。 "}";
  181. $error = json_decode($subError, true);
  182. throw new Exception($error['error_description'] , (int)$error['error']);
  183. }
  184. /**
  185. * 從取得openid介面的回傳資料解析出openid
  186. * @param string $str
  187. * @return string
  188. */
  189. 受保護函數parseOpenid($str)
  190. {
  191. $ subStr = substr($str, strpos($str, "{") );
  192. $subStr = strstr($subStr, "}", true) 。 "}";
  193. $strArr = json_decode($subStr, true);
  194. if(!isset($strArr['openid'])) {
  195. $this->thrwoError($str);
  196. }
  197. return $strArr['openid'];
  198. }
  199. }
複製代碼
以上說的就是這篇文章的全部內容了,希望大家能夠喜歡。

互聯,PHP

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

在PHP API中說明JSON Web令牌(JWT)及其用例。 在PHP API中說明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

解釋PHP中晚期靜態結合的概念。 解釋PHP中晚期靜態結合的概念。 Mar 21, 2025 pm 01:33 PM

文章討論了PHP 5.3中介紹的PHP中的晚期靜態結合(LSB),允許靜態方法的運行時間分辨率調用以更靈活的繼承。 LSB的實用應用和潛在的觸摸

描述紮實的原則及其如何應用於PHP的開發。 描述紮實的原則及其如何應用於PHP的開發。 Apr 03, 2025 am 12:04 AM

SOLID原則在PHP開發中的應用包括:1.單一職責原則(SRP):每個類只負責一個功能。 2.開閉原則(OCP):通過擴展而非修改實現變化。 3.里氏替換原則(LSP):子類可替換基類而不影響程序正確性。 4.接口隔離原則(ISP):使用細粒度接口避免依賴不使用的方法。 5.依賴倒置原則(DIP):高低層次模塊都依賴於抽象,通過依賴注入實現。

框架安全功能:防止漏洞。 框架安全功能:防止漏洞。 Mar 28, 2025 pm 05:11 PM

文章討論了框架中的基本安全功能,以防止漏洞,包括輸入驗證,身份驗證和常規更新。

自定義/擴展框架:如何添加自定義功能。 自定義/擴展框架:如何添加自定義功能。 Mar 28, 2025 pm 05:12 PM

本文討論了將自定義功能添加到框架上,專注於理解體系結構,識別擴展點以及集成和調試的最佳實踐。

如何在系統重啟後自動設置unixsocket的權限? 如何在系統重啟後自動設置unixsocket的權限? Mar 31, 2025 pm 11:54 PM

如何在系統重啟後自動設置unixsocket的權限每次系統重啟後,我們都需要執行以下命令來修改unixsocket的權限:sudo...

如何用PHP的cURL庫發送包含JSON數據的POST請求? 如何用PHP的cURL庫發送包含JSON數據的POST請求? Apr 01, 2025 pm 03:12 PM

使用PHP的cURL庫發送JSON數據在PHP開發中,經常需要與外部API進行交互,其中一種常見的方式是使用cURL庫發送POST�...

See all articles