首页 > 开发 > PHP > 正文

PHP版QQ互联OAuth示例代码分享

2024-05-04 23:37:19
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了PHP版QQ互联OAuth示例代码分享,十分的详细使用,有需要的小伙伴可以参考下。

由于国内QQ用户的普遍性,所以现在各大网站都尽可能的提供QQ登陆口,下面我们来看看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. } 


以上所述就是本文的全部内容了,希望大家能够喜欢。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表