QqSDK.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace login\sdk;
  3. use login\ThinkOauth;
  4. class QqSDK extends ThinkOauth
  5. {
  6. /**
  7. * 获取requestCode的api接口
  8. * @var string
  9. */
  10. protected $GetRequestCodeURL = 'https://graph.qq.com/oauth2.0/authorize';
  11. /**
  12. * 获取access_token的api接口
  13. * @var string
  14. */
  15. protected $GetAccessTokenURL = 'https://graph.qq.com/oauth2.0/token';
  16. /**
  17. * 获取request_code的额外参数,可在配置中修改 URL查询字符串格式
  18. * @var srting
  19. */
  20. protected $Authorize = 'scope=get_user_info,add_share';
  21. /**
  22. * API根路径
  23. * @var string
  24. */
  25. protected $ApiBase = 'https://graph.qq.com/';
  26. /**
  27. * 组装接口调用参数 并调用接口
  28. * @param string $api 微博API
  29. * @param string $param 调用API的额外参数
  30. * @param string $method HTTP请求方法 默认为GET
  31. * @return json
  32. */
  33. public function call($api, $param = '', $method = 'GET', $multi = false)
  34. {
  35. /* 腾讯QQ调用公共参数 */
  36. $params = array(
  37. 'oauth_consumer_key' => $this->AppKey,
  38. 'access_token' => $this->Token['access_token'],
  39. 'openid' => $this->openid(),
  40. 'format' => 'json'
  41. );
  42. $data = $this->http($this->url($api), $this->param($params, $param), $method);
  43. return json_decode($data, true);
  44. }
  45. /**
  46. * 解析access_token方法请求后的返回值
  47. * @param string $result 获取access_token的方法的返回值
  48. */
  49. protected function parseToken($result, $extend)
  50. {
  51. parse_str($result, $data);
  52. if ($data['access_token'] && $data['expires_in']) {
  53. $this->Token = $data;
  54. $data['openid'] = $this->openid();
  55. return $data;
  56. } else
  57. throw new \think\Exception("获取腾讯QQ ACCESS_TOKEN 出错:{$result}");
  58. }
  59. /**
  60. * 获取当前授权应用的openid
  61. * @return string
  62. */
  63. public function openid()
  64. {
  65. $data = $this->Token;
  66. if (isset($data['openid']))
  67. return $data['openid'];
  68. elseif ($data['access_token']) {
  69. $data = $this->http($this->url('oauth2.0/me'), array('access_token' => $data['access_token']));
  70. $data = json_decode(trim(substr($data, 9), " );\n"), true);
  71. if (isset($data['openid']))
  72. return $data['openid'];
  73. else
  74. throw new \think\Exception("获取用户openid出错:{$data['error_description']}");
  75. } else {
  76. throw new \think\Exception('没有获取到openid!');
  77. }
  78. }
  79. }