PHP version QQ interconnect OAuth Sample code sharing

Source: Internet
Author: User
Tags oauth openid

Because of the popularity of domestic QQ users, so now the major sites are as far as possible to provide QQ landing, the following we look at the PHP version, for everyone to refer to the next

  1. /**
  2. * QQ Internet OAuth
  3. * @author Dyllen
  4. *
  5. */
  6. Class Oauth
  7. {
  8. Fetch Authorization Code URL
  9. Const PC_CODE_URL = ' https://graph.qq.com/oauth2.0/authorize ';
  10. Take Access Token URL
  11. Const PC_ACCESS_TOKEN_URL = ' Https://graph.qq.com/oauth2.0/token ';
  12. Fetch user Open Id URL
  13. Const OPEN_ID_URL = ' https://graph.qq.com/oauth2.0/me ';
  14. Callback Address after user authorization
  15. public $redirectUri = null;
  16. APP Id
  17. public $appid = null;
  18. APP Key
  19. public $appKey = null;
  20. Authorization list
  21. String, multiple separated by commas
  22. public $scope = null;
  23. Authorization Code
  24. public $code = null;
  25. Renew Credentials for access token
  26. public $refreshToken = null;
  27. Access token
  28. public $accessToken = null;
  29. Access token validity period, per second
  30. public $expiresIn = null;
  31. State
  32. public $state = null;
  33. public $openid = null;
  34. Construct
  35. Public function __construct ($config =[])
  36. {
  37. foreach ($config as $key = = $value) {
  38. $this $key = $value;
  39. }
  40. }
  41. /**
  42. * Get URL to get code
  43. * @throws \invalidargumentexception
  44. * @return String
  45. */
  46. Public Function Codeurl ()
  47. {
  48. if (! $this->redirecturi) {
  49. throw new \exception (' parameter $redirectUri must be set. ');
  50. }
  51. $query = [
  52. ' Response_type ' = ' Code ',
  53. ' client_id ' = $this->appid,
  54. ' Redirect_uri ' = $this->redirecturi,
  55. ' state ' = $this->getstate (),
  56. ' Scope ' = $this->scope,
  57. ];
  58. Return self::P c_code_url. '?' . Http_build_query ($query);
  59. }
  60. /**
  61. * Take access token
  62. * @throws Exception
  63. * @return Boolean
  64. */
  65. Public Function Getaccesstoken ()
  66. {
  67. $params = [
  68. ' Grant_type ' = ' authorization_code ',
  69. ' client_id ' = $this->appid,
  70. ' Client_secret ' = $this->appkey,
  71. ' Code ' = $this->code,
  72. ' Redirect_uri ' = $this->redirecturi,
  73. ];
  74. $url = self::P c_access_token_url. '?' . Http_build_query ($params);
  75. $content = $this->geturl ($url);
  76. Parse_str ($content, $res);
  77. if (!isset ($res [' Access_token '])) {
  78. $this->thrwoerror ($content);
  79. }
  80. $this->accesstoken = $res [' Access_token '];
  81. $this->expiresin = $res [' expires_in '];
  82. $this->refreshtoken = $res [' Refresh_token '];
  83. return true;
  84. }
  85. /**
  86. * Refresh Access Tokens
  87. * @throws Exception
  88. * @return Boolean
  89. */
  90. Public Function Refreshtoken ()
  91. {
  92. $params = [
  93. ' Grant_type ' = ' refresh_token ',
  94. ' client_id ' = $this->appid,
  95. ' Client_secret ' = $this->appkey,
  96. ' Refresh_token ' = $this->refreshtoken,
  97. ];
  98. $url = self::P c_access_token_url. '?' . Http_build_query ($params);
  99. $content = $this->geturl ($url);
  100. Parse_str ($content, $res);
  101. if (!isset ($res [' Access_token '])) {
  102. $this->thrwoerror ($content);
  103. }
  104. $this->accesstoken = $res [' Access_token '];
  105. $this->expiresin = $res [' expires_in '];
  106. $this->refreshtoken = $res [' Refresh_token '];
  107. return true;
  108. }
  109. /**
  110. * Take user Open ID
  111. * @return String
  112. */
  113. Public Function Getopenid ()
  114. {
  115. $params = [
  116. ' Access_token ' = $this->accesstoken,
  117. ];
  118. $url = Self::open_id_url. '?' . Http_build_query ($params);
  119. $this->openid = $this->parseopenid ($this->geturl ($url));
  120. return $this->openid;
  121. }
  122. /**
  123. * Get URL Content
  124. * @param string $url
  125. * @return Mixed
  126. */
  127. Public Function GetUrl ($url)
  128. {
  129. $ch = Curl_init ();
  130. curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);
  131. curl_setopt ($ch, Curlopt_returntransfer, TRUE);
  132. curl_setopt ($ch, Curlopt_url, $url);
  133. $response = curl_exec ($ch);
  134. Curl_close ($ch);
  135. return $response;
  136. }
  137. /**
  138. * Post method to fetch URL content
  139. * @param string $url
  140. * @param array $KEYSARR
  141. * @param number $flag
  142. * @return Mixed
  143. */
  144. Public Function PostURL ($url, $keysArr, $flag = 0)
  145. {
  146. $ch = Curl_init ();
  147. if (! $flag) curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);
  148. curl_setopt ($ch, Curlopt_returntransfer, TRUE);
  149. curl_setopt ($ch, Curlopt_post, TRUE);
  150. curl_setopt ($ch, Curlopt_postfields, $KEYSARR);
  151. curl_setopt ($ch, Curlopt_url, $url);
  152. $ret = curl_exec ($ch);
  153. Curl_close ($ch);
  154. return $ret;
  155. }
  156. /**
  157. * Take State
  158. * @return String
  159. */
  160. protected function GetState ()
  161. {
  162. $this->state = MD5 (Uniqid (rand (), true));
  163. State is temporarily cached inside
  164. Define Yourself
  165. 。。。。。。。。。
  166. return $this->state;
  167. }
  168. /**
  169. * Verify State
  170. * @return Boolean
  171. */
  172. protected function Verifystate ()
  173. {
  174. 。。。。。。。
  175. }
  176. /**
  177. * Throws an exception
  178. * @param string $error
  179. * @throws \exception
  180. */
  181. protected function Thrwoerror ($error)
  182. {
  183. $subError = substr ($error, Strpos ($error, "{"));
  184. $subError = Strstr ($subError, "}", true). "}";
  185. $error = Json_decode ($subError, true);
  186. throw new \exception ($error [' Error_description '], (int) $error [' Error ']);
  187. }
  188. /**
  189. * The OpenID is parsed from the return data obtained from the OpenID interface.
  190. * @param string $str
  191. * @return String
  192. */
  193. protected function Parseopenid ($STR)
  194. {
  195. $SUBSTR = SubStr ($str, Strpos ($str, "{"));
  196. $SUBSTR = Strstr ($subStr, "}", true). "}";
  197. $STRARR = Json_decode ($subStr, true);
  198. if (!isset ($strArr [' OpenID ')]) {
  199. $this->thrwoerror ($STR);
  200. }
  201. return $STRARR [' OpenID '];
  202. }
  203. }
Copy Code

The above mentioned is the whole content of this article, I hope you can like.

Interconnect, PHP
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.