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.
 
 
 
 

113 lines
2.8 KiB

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