Weibo.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 ? $params : $_GET;
  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 = isset($data['access_token']) ? $data['access_token'] : '';
  62. $refresh_token = isset($data['refresh_token']) ? $data['refresh_token'] : '';
  63. $expires_in = isset($data['expires_in']) ? $data['expires_in'] : 0;
  64. if ($access_token) {
  65. $uid = isset($data['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 = $userinfo ? $userinfo : [];
  77. $userinfo['nickname'] = isset($userinfo['screen_name']) ? $userinfo['screen_name'] : '';
  78. $userinfo['avatar'] = isset($userinfo['profile_image_url']) ? $userinfo['profile_image_url'] : '';
  79. $data = [
  80. 'access_token' => $access_token,
  81. 'refresh_token' => $refresh_token,
  82. 'expires_in' => $expires_in,
  83. 'openid' => $uid,
  84. 'userinfo' => $userinfo
  85. ];
  86. return $data;
  87. }
  88. }
  89. return [];
  90. }
  91. /**
  92. * 获取access_token
  93. * @param string code
  94. * @return array
  95. */
  96. public function getAccessToken($code = '')
  97. {
  98. if (!$code) {
  99. return '';
  100. }
  101. $queryarr = array(
  102. "grant_type" => "authorization_code",
  103. "client_id" => $this->config['app_id'],
  104. "client_secret" => $this->config['app_secret'],
  105. "redirect_uri" => $this->config['callback'],
  106. "code" => $code,
  107. );
  108. $response = Http::post(self::GET_ACCESS_TOKEN_URL, $queryarr);
  109. $ret = (array)json_decode($response, true);
  110. return $ret ? $ret : [];
  111. }
  112. }