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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
發布: 2016-07-25 08:45:00
原創
1166 人瀏覽過

由於國內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

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板