Weibo.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace addons\third\library;
  3. use fast\Http;
  4. use think\Config;
  5. use think\Session;
  6. /**
  7. * 微博
  8. */
  9. class Weibo
  10. {
  11. const GET_AUTH_CODE_URL = "https://api.weibo.com/oauth2/authorize";
  12. const GET_ACCESS_TOKEN_URL = "https://api.weibo.com/oauth2/access_token";
  13. const GET_USERINFO_URL = "https://api.weibo.com/2/users/show.json";
  14. /**
  15. * 配置信息
  16. * @var array
  17. */
  18. private $config = [];
  19. public function __construct($options = [])
  20. {
  21. if ($config = Config::get('third.weibo')) {
  22. $this->config = array_merge($this->config, $config);
  23. }
  24. $this->config = array_merge($this->config, is_array($options) ? $options : []);
  25. }
  26. /**
  27. * 登陆
  28. */
  29. public function login()
  30. {
  31. header("Location:" . $this->getAuthorizeUrl());
  32. }
  33. /**
  34. * 获取authorize_url
  35. */
  36. public function getAuthorizeUrl()
  37. {
  38. $state = md5(uniqid(rand(), true));
  39. Session::set('state', $state);
  40. $queryarr = array(
  41. "response_type" => "code",
  42. "client_id" => $this->config['app_id'],
  43. "redirect_uri" => $this->config['callback'],
  44. "state" => $state,
  45. );
  46. request()->isMobile() && $queryarr['display'] = 'mobile';
  47. $url = self::GET_AUTH_CODE_URL . '?' . http_build_query($queryarr);
  48. return $url;
  49. }
  50. /**
  51. * 获取用户信息
  52. * @param array $params
  53. * @return array
  54. */
  55. public function getUserInfo($params = [])
  56. {
  57. $params = $params ?: request()->get('', null, null);
  58. if (isset($params['access_token']) || (isset($params['state']) && $params['state'] == Session::get('state') && isset($params['code']))) {
  59. //获取access_token
  60. $data = isset($params['code']) ? $this->getAccessToken($params['code']) : $params;
  61. $access_token = $data['access_token'] ?? '';
  62. $refresh_token = $data['refresh_token'] ?? '';
  63. $expires_in = $data['expires_in'] ?? 0;
  64. if ($access_token) {
  65. $uid = $data['uid'] ?? '';
  66. //获取用户信息
  67. $queryarr = [
  68. "access_token" => $access_token,
  69. "uid" => $uid,
  70. ];
  71. $ret = Http::get(self::GET_USERINFO_URL, $queryarr);
  72. $userinfo = (array)json_decode($ret, true);
  73. if (!$userinfo || isset($userinfo['error_code'])) {
  74. return [];
  75. }
  76. $userinfo['nickname'] = $userinfo['screen_name'] ?? '';
  77. $userinfo['avatar'] = $userinfo['profile_image_url'] ?? '';
  78. $data = [
  79. 'access_token' => $access_token,
  80. 'refresh_token' => $refresh_token,
  81. 'expires_in' => $expires_in,
  82. 'openid' => $uid,
  83. 'userinfo' => $userinfo
  84. ];
  85. return $data;
  86. }
  87. }
  88. return [];
  89. }
  90. /**
  91. * 获取access_token
  92. * @param string $code code
  93. * @return array
  94. */
  95. public function getAccessToken($code = '')
  96. {
  97. if (!$code) {
  98. return [];
  99. }
  100. $queryarr = array(
  101. "grant_type" => "authorization_code",
  102. "client_id" => $this->config['app_id'],
  103. "client_secret" => $this->config['app_secret'],
  104. "redirect_uri" => $this->config['callback'],
  105. "code" => $code,
  106. );
  107. $response = Http::post(self::GET_ACCESS_TOKEN_URL . '?' . http_build_query($queryarr), []);
  108. $ret = (array)json_decode($response, true);
  109. return $ret ?: [];
  110. }
  111. }