You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

121 lines
3.1 KiB

  1. <?php
  2. namespace Kuxin\Oauth;
  3. use Kuxin\Helper\Http;
  4. use Kuxin\Helper\Json;
  5. class Qq extends Oauth{
  6. /**
  7. * 获取requestCode的api接口
  8. *
  9. * @var string
  10. */
  11. protected $getRequestCodeURL = 'https://graph.qq.com/oauth2.0/authorize';
  12. /**
  13. * 获取access_token的api接口
  14. *
  15. * @var string
  16. */
  17. protected $getAccessTokenURL = 'https://graph.qq.com/oauth2.0/token';
  18. /**
  19. * 获取request_code的额外参数,可在配置中修改 URL查询字符串格式
  20. *
  21. * @var array
  22. */
  23. protected $authorizeParam = array(
  24. "scope" => 'get_user_info,add_topic,add_share,add_t',
  25. );
  26. /**
  27. * 获取accesstoekn时候的附加参数
  28. *
  29. * @var array
  30. */
  31. protected $getTokenParam = array();
  32. /**
  33. * API根路径
  34. *
  35. * @var string
  36. */
  37. protected $apiBase = 'https://graph.qq.com/';
  38. /**
  39. * 组装接口调用参数 并调用接口
  40. *
  41. * @param string $api 微博API
  42. * @param array $param 调用API的额外参数
  43. * @param string $method HTTP请求方法 默认为GET
  44. * @param bool $multi
  45. * @return string json
  46. */
  47. public function call($api, $param = array(), $method = 'GET', $multi = false) {
  48. /* 腾讯QQ调用公共参数 */
  49. $params = array(
  50. 'oauth_consumer_key' => $this->appid,
  51. 'access_token' => $this->token,
  52. 'openid' => $this->getOpenId(),
  53. 'format' => 'json'
  54. );
  55. $params = array_merge($params, $param);
  56. $data = Http::get($this->apiBase . $api, $params);
  57. return Json::decode($data, true);
  58. }
  59. /**
  60. * 解析access_token方法请求后的返回值
  61. *
  62. * @param $result
  63. * @return mixed
  64. * @throws \Exception
  65. */
  66. protected function parseToken($result) {
  67. parse_str($result, $data);
  68. if (!empty($data['access_token'])) {
  69. $this->token = $data['access_token'];
  70. return array(
  71. 'openid' => $this->getOpenId(),
  72. 'token' => $data['access_token'],
  73. 'expires' => $data['expires_in'],
  74. 'refresh' => $data['refresh_token'],
  75. );
  76. } else
  77. return "获取 ACCESS_TOKEN 出错:{$result}";
  78. }
  79. /**
  80. * 获取openid
  81. *
  82. * @return mixed
  83. * @throws \Exception
  84. */
  85. public function getOpenId() {
  86. if ($this->openid) return $this->openid;
  87. $data = http::get($this->apiBase . 'oauth2.0/me', array('access_token' => $this->token));
  88. $data = Json::decode(trim(substr($data, 9), " );\n"), true);
  89. if (isset($data['openid'])) {
  90. $this->openid = $data['openid'];
  91. return $data['openid'];
  92. } else
  93. return false;
  94. }
  95. /**
  96. * 获取用户信息
  97. *
  98. * @return array
  99. */
  100. public function getInfo() {
  101. $data = $this->call('user/get_user_info');
  102. return array(
  103. 'id' => $this->openid,
  104. 'name' => $data['nickname'],
  105. 'gender' => $data['gender'],
  106. 'avatar' => $data['figureurl_2'],
  107. );
  108. }
  109. }