Oauth2.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace cmf\lib;
  3. // +----------------------------------------------------------------------
  4. // | TOPThink [ WE CAN DO IT JUST THINK ]
  5. // +----------------------------------------------------------------------
  6. // | Copyright (c) 2010 http://topthink.com All rights reserved.
  7. // +----------------------------------------------------------------------
  8. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  9. // +----------------------------------------------------------------------
  10. // | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
  11. // +----------------------------------------------------------------------
  12. // | ThinkOauth.class.php 2013-02-25
  13. // +----------------------------------------------------------------------
  14. // | 老猫 <catman@thinkcmf.com> 规范代码和用法
  15. // +----------------------------------------------------------------------
  16. abstract class Oauth2
  17. {
  18. /**
  19. * oauth版本
  20. * @var string
  21. */
  22. protected $version = '2.0';
  23. /**
  24. * 申请应用时分配的app_key
  25. * @var string
  26. */
  27. protected $appKey = '';
  28. /**
  29. * 申请应用时分配的 app_secret
  30. * @var string
  31. */
  32. protected $appSecret = '';
  33. /**
  34. * 授权类型 response_type 目前只能为code
  35. * @var string
  36. */
  37. protected $responseType = 'code';
  38. /**
  39. * grant_type 目前只能为 authorization_code
  40. * @var string
  41. */
  42. protected $grantType = 'authorization_code';
  43. /**
  44. * 回调页面URL 可以通过配置文件配置
  45. * @var string
  46. */
  47. protected $callback = '';
  48. /**
  49. * 获取request_code的额外参数 URL查询字符串格式
  50. * @var string
  51. */
  52. protected $authorize = '';
  53. /**
  54. * 获取request_code请求的URL
  55. * @var string
  56. */
  57. protected $getRequestCodeURL = '';
  58. /**
  59. * 获取access_token请求的URL
  60. * @var string
  61. */
  62. protected $getAccessTokenURL = '';
  63. /**
  64. * API根路径
  65. * @var string
  66. */
  67. protected $apiBase = '';
  68. /**
  69. * 授权后获取到的TOKEN信息
  70. * @var array
  71. */
  72. protected $token = null;
  73. /**
  74. * 初始化配置
  75. */
  76. private function config()
  77. {
  78. // $config = C("THINK_SDK_{$this->Type}");
  79. // if (!empty($config['AUTHORIZE']))
  80. // $this->authorize = $config['AUTHORIZE'];
  81. // if (!empty($config['CALLBACK']))
  82. // $this->callback = $config['CALLBACK'];
  83. // else
  84. // throw new Exception('请配置回调页面地址');
  85. }
  86. /**
  87. * @param string $appKey
  88. */
  89. public function setAppKey(string $appKey)
  90. {
  91. $this->appKey = $appKey;
  92. }
  93. /**
  94. * @param string $callback
  95. */
  96. public function setCallback(string $callback)
  97. {
  98. $this->callback = $callback;
  99. }
  100. /**
  101. * @param string $appSecret
  102. */
  103. public function setAppSecret(string $appSecret)
  104. {
  105. $this->appSecret = $appSecret;
  106. }
  107. /**
  108. * 请求code
  109. */
  110. public function getRequestCodeURL()
  111. {
  112. $this->config();
  113. //Oauth 标准参数
  114. $params = [
  115. 'client_id' => $this->appKey,
  116. 'redirect_uri' => $this->callback,
  117. 'response_type' => $this->responseType,
  118. ];
  119. //获取额外参数
  120. if ($this->authorize) {
  121. parse_str($this->authorize, $_param);
  122. if (is_array($_param)) {
  123. $params = array_merge($params, $_param);
  124. } else {
  125. throw new \Exception('AUTHORIZE配置不正确!');
  126. }
  127. }
  128. return $this->getRequestCodeURL . '?' . http_build_query($params);
  129. }
  130. /**
  131. * 获取access_token
  132. * @param string $code 上一步请求到的code
  133. * @param null $extend
  134. * @return array
  135. */
  136. public function getAccessToken($code, $extend = null)
  137. {
  138. $this->config();
  139. $params = [
  140. 'client_id' => $this->appKey,
  141. 'client_secret' => $this->appSecret,
  142. 'grant_type' => $this->grantType,
  143. 'code' => $code,
  144. 'redirect_uri' => $this->callback,
  145. ];
  146. $data = $this->http($this->getAccessTokenURL, $params, 'POST');
  147. $this->token = $this->parseToken($data, $extend);
  148. return $this->token;
  149. }
  150. /**
  151. * 合并默认参数和额外参数
  152. * @param array $params 默认参数
  153. * @param array /string $param 额外参数
  154. * @return array:
  155. */
  156. protected function param($params, $param)
  157. {
  158. if (is_string($param))
  159. parse_str($param, $param);
  160. return array_merge($params, $param);
  161. }
  162. /**
  163. * 获取指定API请求的URL
  164. * @param string $api API名称
  165. * @param string $fix api后缀
  166. * @return string 请求的完整URL
  167. */
  168. protected function url($api, $fix = '')
  169. {
  170. return $this->apiBase . $api . $fix;
  171. }
  172. /**
  173. * /**
  174. * 发送HTTP请求方法,目前只支持CURL发送请求
  175. * @param string $url 请求URL
  176. * @param array $params 请求参数
  177. * @param string $method 请求方法GET/POST
  178. * @param array $header
  179. * @param bool $multi
  180. * @return array $data 响应数据
  181. * @throws \Exception
  182. */
  183. protected function http($url, $params, $method = 'GET', $header = [], $multi = false)
  184. {
  185. $opts = [
  186. CURLOPT_TIMEOUT => 30,
  187. CURLOPT_RETURNTRANSFER => 1,
  188. CURLOPT_SSL_VERIFYPEER => false,
  189. CURLOPT_SSL_VERIFYHOST => false,
  190. CURLOPT_HTTPHEADER => $header
  191. ];
  192. /* 根据请求类型设置特定参数 */
  193. switch (strtoupper($method)) {
  194. case 'GET':
  195. $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
  196. break;
  197. case 'POST':
  198. //判断是否传输文件
  199. $params = $multi ? $params : http_build_query($params);
  200. $opts[CURLOPT_URL] = $url;
  201. $opts[CURLOPT_POST] = 1;
  202. $opts[CURLOPT_POSTFIELDS] = $params;
  203. break;
  204. default:
  205. throw new \Exception('不支持的请求方式!');
  206. }
  207. /* 初始化并执行curl请求 */
  208. $ch = curl_init();
  209. curl_setopt_array($ch, $opts);
  210. $data = curl_exec($ch);
  211. $error = curl_error($ch);
  212. curl_close($ch);
  213. if ($error) throw new \Exception('请求发生错误:' . $error);
  214. return $data;
  215. }
  216. /**
  217. * 组装接口调用参数 并调用接口
  218. * @param $api
  219. * @param string $param
  220. * @param string $method
  221. * @param bool $multi
  222. * @return mixed
  223. */
  224. abstract protected function call($api, $param = '', $method = 'GET', $multi = false);
  225. /**
  226. * 解析access_token方法请求后的返回值
  227. * @param $result
  228. * @param $extend
  229. */
  230. abstract protected function parseToken($result, $extend);
  231. /**
  232. * 获取当前授权用户的SNS标识
  233. */
  234. abstract public function openid();
  235. }