* @version : HUOSDK 8.0 * http://open.weibo.com/wiki/Connect/login */ namespace huolib\oauth\driver; use huolib\constant\MemConst; use huolib\oauth\OAuth; use huolib\tool\Http; use think\Exception; class Weibo extends OAuth { /** * 获取requestCode的api接口 * * @var string */ protected $request_code_url = 'https://api.weibo.com/oauth2/authorize'; /** * mobile请求地址 * * @var string */ protected $request_code_url_m = 'https://open.weibo.cn/oauth2/authorize '; /** * 获取Access token的api接口 * * @var String */ protected $access_token_url = 'https://api.weibo.com/oauth2/access_token'; /** * API根路径 * * @var string */ protected $api_base = 'https://api.weibo.com/2/'; /** * 获取request_code的额外参数 URL查询字符串格式 * * @var string */ protected $authorize = 'snsapi_login'; /** * Weibo constructor. * * @param array $config * @param array|null $token * * @throws \think\Exception */ public function __construct(array $config = [], array $token = null) { $_config = $config; if (empty($config)) { if (file_exists(GLOBAL_CONF_PATH."extra/oauth/weibo.php")) { $_config = include GLOBAL_CONF_PATH."extra/oauth/weibo.php"; } else { $_config = array(); } } parent::__construct($_config, $token); } /** * 第一步:请求CODE 跳转URL * 请求Authorize访问地址 * * @param string $display * * @return string 跳转地址 * http://open.weibo.com/wiki/Oauth2/authorize * @return string */ public function getRequestCodeUrl($display = 'pc') { $_display = $display; if ('pc' == $_display) { $_display = 'default'; $_request_code_url = $this->request_code_url; } else { $_display = 'mobile'; $_request_code_url = $this->request_code_url_m; } $params = array( 'client_id' => $this->app_key, 'redirect_uri' => $this->callback, 'state' => $this->getState(), 'scope' => $this->authorize, 'display' => $_display, 'forcelogin' => 'false' ); return $_request_code_url.'?'.http_build_query($params); } /** * 第二步:通过code获取access_token * http://open.weibo.com/wiki/Oauth2/access_token * * @param $code * @param null $extend * * @return array|null * @throws Exception */ public function getAccessToken($code, $extend = null) { $_params = array( 'client_id' => $this->app_key, 'client_secret' => $this->app_secret, 'grant_type' => $this->grant_type, 'code' => $code, 'redirect_uri' => $this->callback, ); $_rdata = Http::post($this->access_token_url, $_params); $this->token = $this->parseToken($_rdata); return $this->token; } /** * 第三步:通过access_token调用接口 * * @param string $api API * @param string $param 调用API的额外参数 * @param string $method HTTP请求方法 默认为GET * @param null $multi * * @return mixed * @throws Exception */ public function call($api, $param = '', $method = 'GET', $multi = null) { $params = array( 'access_token' => $this->token['access_token'], 'uid' => $this->getOpenid(), ); $data = Http::request($this->url($api), $params, $method); return json_decode($data, true); } /** * 获取授权用户的用户信息 * * @return array * @throws \Exception * http://open.weibo.com/wiki/2/users/show * * id int64 用户UID * idstr string 字符串型的用户UID * screen_name string 用户昵称 * name string 友好显示名称 * province int 用户所在省级ID * city int 用户所在城市ID * location string 用户所在地 * description string 用户个人描述 * url string 用户博客地址 * profile_image_url string 用户头像地址(中图),50×50像素 * profile_url string 用户的微博统一URL地址 * domain string 用户的个性化域名 * weihao string 用户的微号 * gender string 性别,m:男、f:女、n:未知 * followers_count int 粉丝数 * friends_count int 关注数 * statuses_count int 微博数 * favourites_count int 收藏数 * created_at string 用户创建(注册)时间 * following boolean 暂未支持 * allow_all_act_msg boolean 是否允许所有人给我发私信,true:是,false:否 * geo_enabled boolean 是否允许标识用户的地理位置,true:是,false:否 * verified boolean 是否是微博认证用户,即加V用户,true:是,false:否 * verified_type int 暂未支持 * remark string 用户备注信息,只有在查询用户关系时才返回此字段 * status object 用户的最近一条微博信息字段 详细 * allow_all_comment boolean 是否允许所有人对我的微博进行评论,true:是,false:否 * avatar_large string 用户头像地址(大图),180×180像素 * avatar_hd string 用户头像地址(高清),高清头像原图 * verified_reason string 认证原因 * follow_me boolean 该用户是否关注当前登录用户,true:是,false:否 * online_status int 用户的在线状态,0:不在线、1:在线 * bi_followers_count int 用户的互粉数 * lang string 用户当前的语言版本,zh-cn:简体中文,zh-tw:繁体中文,en:英语 * */ public function getUserInfo() { $_response = $this->call('users/show.json'); if (empty($_response) || (isset($_response['error_code']) && $_response['error_code'] != 0)) { throw new Exception('接口访问失败!'.$_response['error']); } else { $_oauth_data = array( 'openid' => $_response['id'], 'unionid' => isset($_response['idstr']) ? $_response['idstr'] : '', 'channel' => 'weibo', 'nickname' => $_response['name'], 'gender' => $this->getGender($_response['gender']), 'avatar' => $_response['avatar_large'], 'token' => $this->token ); return $_oauth_data; } } /** * 获取当前授权用户的SNS标识 * * @throws Exception */ public function getOpenid() { $_data = $this->token; if (isset($_data['uid'])) { return $_data['uid']; } else { throw new Exception('没有获取到微博用户ID!'); } } /** * 解析access_token方法请求后的返回值 * * @param $result * @param null $extend * * @return mixed * @throws Exception */ protected function parseToken($result, $extend = null) { $_rdata = json_decode($result, true); if ($_rdata['access_token'] && $_rdata['expires_in']) { $this->token = $_rdata; $_rdata['openid'] = $this->getOpenid(); return $_rdata; } else { throw new Exception("获取微博 ACCESS_token 出错:{$result}"); } } /** * @param string $gender * * @return int */ private function getGender($gender = 'm') { switch ($gender) { case 'm': return MemConst::GENDER_M; case 'f': return MemConst::GENDER_F; default : return MemConst::GENDER_N; } } }