diff --git a/Think/Oauth.php b/Think/Oauth.php new file mode 100644 index 00000000..619c0521 --- /dev/null +++ b/Think/Oauth.php @@ -0,0 +1,71 @@ + +// +---------------------------------------------------------------------- +// $Id$ +namespace Think; +// oauth登录接口 +// +// Oauth::connect(['oauth_type'=>'qq','app_key'=>'','app_secret'=>'','callback'=>'','authorize'=>'']); // 链接QQ登录 +// Oauth::login(); // 跳转到授权登录页面 或者 Oauth::login($callbackUrl); +// Oauth::call('api','params'); // 调用API接口 +// +class Oauth { + + /** + * 操作句柄 + * @var object + * @access protected + */ + static protected $handler = null; + + /** + * 连接oauth + * @access public + * @param array $options 配置数组 + * @return object + */ + static public function connect($options=[]) { + $type = $options['oauth_type']; + $class = 'Think\\Oauth\\Driver\\'.ucwords($type); + if(class_exists($class)) { + unset($options['oauth_type']); + self::$handler = new $class($options); + return self::$handler; + }else{ + Error::halt('_CACHE_TYPE_INVALID_:'.$type); + } + } + + // 跳转到授权登录页面 + static public function login($callback=''){ + self::$handler->login($callback); + } + + // 获取access_token + static public function getAccessToken(){ + self::$handler->getAccessToken(); + } + + // 获取oauth用户信息 + static public function getOauthInfo(){ + return self::$handler->getOauthInfo(); + } + + // 获取oauth用户信息 + static public function getOpenId(){ + return self::$handler->getOpenId(); + } + + // 调用oauth接口API + static public function call($api,$param='',$method='GET'){ + return self::$handler->call($api,$param,$method); + } + +} \ No newline at end of file diff --git a/Think/Oauth/Driver.php b/Think/Oauth/Driver.php new file mode 100644 index 00000000..8f05458d --- /dev/null +++ b/Think/Oauth/Driver.php @@ -0,0 +1,233 @@ + +// +---------------------------------------------------------------------- +// $Id$ + +namespace Think\Oauth; +abstract class Driver { + + /** + * oauth版本 + * @var string + */ + protected $version = '2.0'; + + /** + * 申请应用时分配的app_key + * @var string + */ + protected $appKey = ''; + + /** + * 申请应用时分配的 app_secret + * @var string + */ + protected $appSecret = ''; + + /** + * 授权类型 response_type 目前只能为code + * @var string + */ + protected $responseType = 'code'; + + /** + * grant_type 目前只能为 authorization_code + * @var string + */ + protected $grantType = 'authorization_code'; + + /** + * 获取request_code请求的URL + * @var string + */ + protected $getRequestCodeURL = ''; + + /** + * 获取access_token请求的URL + * @var string + */ + protected $getAccessTokenURL = ''; + + /** + * API根路径 + * @var string + */ + protected $apiBase = ''; + + /** + * 授权后获取到的TOKEN信息 + * @var array + */ + protected $token = null; + + /** + * 回调页面URL 可以通过配置文件配置 + * @var string + */ + protected $callback = ''; + + /** + * 构造方法,配置应用信息 + * @param array $config + */ + public function __construct($config = []){ + $this->appKey = $config['app_key']; + $this->appSecret = $config['app_secret']; + $this->authorize = isset($config['authorize'])?$config['authorize']:''; + $this->callback = isset($config['callback'])?$config['callback']:''; + } + + // 跳转到授权登录页面 + public function login($callback=''){ + if($callback) { + $this->callback = $callback; + } + //跳转到授权页面 + header('Location: ' . $this->getRequestCodeURL()); + exit; + } + + /** + * 请求code + */ + public function getRequestCodeURL(){ + //Oauth 标准参数 + $params = array( + 'client_id' => $this->appKey, + 'redirect_uri' => $this->callback, + 'response_type' => $this->responseType, + ); + + //获取额外参数 + if($this->authorize){ + parse_str($this->authorize, $_param); + if(is_array($_param)){ + $params = array_merge($params, $_param); + } else { + throw new Exception('AUTHORIZE配置不正确!'); + } + } + return $this->getRequestCodeURL . '?' . http_build_query($params); + } + + /** + * 获取access_token + * @param string $code 授权登录成功后得到的code信息 + */ + public function getAccessToken($code){ + $params = array( + 'client_id' => $this->appKey, + 'client_secret' => $this->appSecret, + 'grant_type' => $this->grantType, + 'redirect_uri' => $this->callback, + 'code' => $code, + ); + // 获取token信息 + $data = $this->http($this->getAccessTokenURL, $params, 'POST'); + // 解析token + $this->token = $this->parseToken($data); + return $this->token; + } + + /** + * 设置access_token + * @param string $token + */ + public function setToken($token){ + $this->token = $token; + } + + /** + * 合并默认参数和额外参数 + * @param array $params 默认参数 + * @param array/string $param 额外参数 + * @return array: + */ + protected function param($params, $param){ + if(is_string($param)) + parse_str($param, $param); + return array_merge($params, $param); + } + + /** + * 获取指定API请求的URL + * @param string $api API名称 + * @param string $fix api后缀 + * @return string 请求的完整URL + */ + protected function url($api, $fix = ''){ + return $this->apiBase . $api . $fix; + } + + /** + * 发送HTTP请求方法,目前只支持CURL发送请求 + * @param string $url 请求URL + * @param array $params 请求参数 + * @param string $method 请求方法GET/POST + * @return array $data 响应数据 + */ + protected function http($url, $params, $method = 'GET', $header = array()){ + $vars = http_build_query($params); + $opts = array( + CURLOPT_TIMEOUT => 30, + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_SSL_VERIFYHOST => false, + CURLOPT_HTTPHEADER => $header + ); + + /* 根据请求类型设置特定参数 */ + switch(strtoupper($method)){ + case 'GET': + $opts[CURLOPT_URL] = $url . '?' . $vars; + break; + case 'POST': + $opts[CURLOPT_URL] = $url; + $opts[CURLOPT_POST] = 1; + $opts[CURLOPT_POSTFIELDS] = $vars; + break; + default: + throw new Exception('不支持的请求方式!'); + } + + /* 初始化并执行curl请求 */ + $ch = curl_init(); + curl_setopt_array($ch, $opts); + $data = curl_exec($ch); + $error = curl_error($ch); + curl_close($ch); + if($error) throw new Exception('请求发生错误:' . $error); + return $data; + } + + /** + * 抽象方法,在SNSSDK中实现 + * 组装接口调用参数 并调用接口 + */ + abstract protected function call($api, $param = '', $method = 'GET'); + + /** + * 抽象方法,在SNSSDK中实现 + * 解析access_token方法请求后的返回值 + */ + abstract protected function parseToken($result); + + /** + * 抽象方法,在SNSSDK中实现 + * 获取当前授权用户的SNS标识 + */ + abstract public function getOpenId(); + + /** + * 抽象方法 + * 获取当前授权用户的用户信息 + */ + abstract public function getOauthInfo(); +} \ No newline at end of file diff --git a/Think/Oauth/Driver/Baidu.php b/Think/Oauth/Driver/Baidu.php new file mode 100644 index 00000000..b35189ac --- /dev/null +++ b/Think/Oauth/Driver/Baidu.php @@ -0,0 +1,92 @@ + +// +---------------------------------------------------------------------- + +namespace Think\Oauth\Driver; +use Think\Oauth\Driver; +class Baidu extends Driver{ + /** + * 获取requestCode的api接口 + * @var string + */ + protected $getRequestCodeURL = 'https://openapi.baidu.com/oauth/2.0/authorize'; + + /** + * 获取access_token的api接口 + * @var string + */ + protected $getAccessTokenURL = 'https://openapi.baidu.com/oauth/2.0/token'; + + /** + * API根路径 + * @var string + */ + protected $apiBase = 'https://openapi.baidu.com/rest/2.0/'; + + /** + * 组装接口调用参数 并调用接口 + * @param string $api 百度API + * @param string $param 调用API的额外参数 + * @param string $method HTTP请求方法 默认为GET + * @return json + */ + public function call($api, $param = '', $method = 'GET'){ + /* 百度调用公共参数 */ + $params = array( + 'access_token' => $this->token['access_token'], + ); + + $data = $this->http($this->url($api), $this->param($params, $param), $method); + return json_decode($data, true); + } + + /** + * 解析access_token方法请求后的返回值 + * @param string $result 获取access_token的方法的返回值 + */ + protected function parseToken($result){ + $data = json_decode($result, true); + if($data['access_token'] && $data['expires_in'] && $data['refresh_token']){ + $data['openid'] = $this->openid(); + return $data; + } else + throw new Exception("获取百度ACCESS_TOKEN出错:{$data['error']}"); + } + + /** + * 获取当前授权应用的openid + * @return string + */ + public function getOpenId(){ + if(!empty($this->token['openid'])) + return $this->token['openid']; + $data = $this->call('passport/users/getLoggedInUser'); + return !empty($data['uid'])?$data['uid']:NULL; + } + + /** + * 获取当前登录的用户信息 + * @return array + */ + public function getOauthInfo(){ + $data = $this->call('passport/users/getLoggedInUser'); + + if(!empty($data['uid'])){ + $userInfo['type'] = 'BAIDU'; + $userInfo['name'] = $data['uid']; + $userInfo['nick'] = $data['uname']; + $userInfo['avatar'] = "http://tb.himg.baidu.com/sys/portrait/item/{$data['portrait']}"; + return $userInfo; + } else { + throw_exception("获取百度用户信息失败:{$data['error_msg']}"); + } + } + +} \ No newline at end of file diff --git a/Think/Oauth/Driver/Diandian.php b/Think/Oauth/Driver/Diandian.php new file mode 100644 index 00000000..1e9f7f04 --- /dev/null +++ b/Think/Oauth/Driver/Diandian.php @@ -0,0 +1,92 @@ + +// +---------------------------------------------------------------------- + +namespace Think\Oauth\Driver; +use Think\Oauth\Driver; +class Diandian extends Driver{ + /** + * 获取requestCode的api接口 + * @var string + */ + protected $getRequestCodeURL = 'https://api.diandian.com/oauth/authorize'; + + /** + * 获取access_token的api接口 + * @var string + */ + protected $getAccessTokenURL = 'https://api.diandian.com/oauth/token'; + + /** + * API根路径 + * @var string + */ + protected $apiBase = 'https://api.diandian.com/v1/'; + + /** + * 组装接口调用参数 并调用接口 + * @param string $api 点点网API + * @param string $param 调用API的额外参数 + * @param string $method HTTP请求方法 默认为GET + * @return json + */ + public function call($api, $param = '', $method = 'GET'){ + /* 点点网调用公共参数 */ + $params = array( + 'access_token' => $this->token['access_token'], + ); + + $data = $this->http($this->url($api, '.json'), $this->param($params, $param), $method); + return json_decode($data, true); + } + + /** + * 解析access_token方法请求后的返回值 + * @param string $result 获取access_token的方法的返回值 + */ + protected function parseToken($result){ + $data = json_decode($result, true); + if($data['access_token'] && $data['expires_in'] && $data['token_type'] && $data['uid']){ + $data['openid'] = $data['uid']; + unset($data['uid']); + return $data; + } else + throw new Exception("获取点点网ACCESS_TOKEN出错:{$data['error']}"); + } + + /** + * 获取当前授权应用的openid + * @return string + */ + public function getOpenId(){ + if(!empty($this->token['openid'])) + return $this->token['openid']; + return NULL; + } + + /** + * 获取当前登录的用户信息 + * @return array + */ + public function getOauthInfo(){ + $data = $this->call('user/info'); + + if(!empty($data['meta']['status']) && $data['meta']['status'] == 200){ + $userInfo['type'] = 'DIANDIAN'; + $userInfo['name'] = $data['response']['name']; + $userInfo['nick'] = $data['response']['name']; + $userInfo['avatar'] = "https://api.diandian.com/v1/blog/{$data['response']['blogs'][0]['blogUuid']}/avatar/144"; + return $userInfo; + } else { + throw_exception("获取点点用户信息失败:{$data}"); + } + } + +} \ No newline at end of file diff --git a/Think/Oauth/Driver/Douban.php b/Think/Oauth/Driver/Douban.php new file mode 100644 index 00000000..c52565b2 --- /dev/null +++ b/Think/Oauth/Driver/Douban.php @@ -0,0 +1,90 @@ + +// +---------------------------------------------------------------------- + +namespace Think\Oauth\Driver; +use Think\Oauth\Driver; +class Douban extends Driver{ + /** + * 获取requestCode的api接口 + * @var string + */ + protected $getRequestCodeURL = 'https://www.douban.com/service/auth2/auth'; + + /** + * 获取access_token的api接口 + * @var string + */ + protected $getAccessTokenURL = 'https://www.douban.com/service/auth2/token'; + + /** + * API根路径 + * @var string + */ + protected $apiBase = 'https://api.douban.com/v2/'; + + /** + * 组装接口调用参数 并调用接口 + * @param string $api 微博API + * @param string $param 调用API的额外参数 + * @param string $method HTTP请求方法 默认为GET + * @return json + */ + public function call($api, $param = '', $method = 'GET'){ + /* 豆瓣调用公共参数 */ + $params = array(); + $header = array("Authorization: Bearer {$this->token['access_token']}"); + $data = $this->http($this->url($api), $this->param($params, $param), $method, $header); + return json_decode($data, true); + } + + /** + * 解析access_token方法请求后的返回值 + * @param string $result 获取access_token的方法的返回值 + */ + protected function parseToken($result){ + $data = json_decode($result, true); + if($data['access_token'] && $data['expires_in'] && $data['refresh_token'] && $data['douban_user_id']){ + $data['openid'] = $data['douban_user_id']; + unset($data['douban_user_id']); + return $data; + } else + throw new Exception("获取豆瓣ACCESS_TOKEN出错:{$data['msg']}"); + } + + /** + * 获取当前授权应用的openid + * @return string|null + */ + public function getOpenId(){ + if(!empty($this->token['openid'])) + return $this->token['openid']; + return NULL; + } + + /** + * 获取当前登录的用户信息 + * @return array + */ + public function getOauthInfo(){ + $data = $this->call('user/~me'); + + if(empty($data['code'])){ + $userInfo['type'] = 'DOUBAN'; + $userInfo['name'] = $data['name']; + $userInfo['nick'] = $data['name']; + $userInfo['avatar'] = $data['avatar']; + return $userInfo; + } else { + throw_exception("获取豆瓣用户信息失败:{$data['msg']}"); + } + } + +} \ No newline at end of file diff --git a/Think/Oauth/Driver/Github.php b/Think/Oauth/Driver/Github.php new file mode 100644 index 00000000..c3298d0d --- /dev/null +++ b/Think/Oauth/Driver/Github.php @@ -0,0 +1,92 @@ + +// +---------------------------------------------------------------------- + +namespace Think\Oauth\Driver; +use Think\Oauth\Driver; +class Github extends Driver{ + /** + * 获取requestCode的api接口 + * @var string + */ + protected $getRequestCodeURL = 'https://github.com/login/oauth/authorize'; + + /** + * 获取access_token的api接口 + * @var string + */ + protected $getAccessTokenURL = 'https://github.com/login/oauth/access_token'; + + /** + * API根路径 + * @var string + */ + protected $apiBase = 'https://api.github.com/'; + + /** + * 组装接口调用参数 并调用接口 + * @param string $api 微博API + * @param string $param 调用API的额外参数 + * @param string $method HTTP请求方法 默认为GET + * @return json + */ + public function call($api, $param = '', $method = 'GET'){ + /* Github 调用公共参数 */ + $params = array(); + $header = array("Authorization: bearer {$this->token['access_token']}"); + + $data = $this->http($this->url($api), $this->param($params, $param), $method, $header); + return json_decode($data, true); + } + + /** + * 解析access_token方法请求后的返回值 + * @param string $result 获取access_token的方法的返回值 + */ + protected function parseToken($result){ + parse_str($result, $data); + if($data['access_token'] && $data['token_type']){ + $data['openid'] = $this->getOpenId(); + return $data; + } else + throw new Exception("获取 Github ACCESS_TOKEN出错:未知错误"); + } + + /** + * 获取当前授权应用的openid + * @return string + */ + public function getOpenId(){ + if(!empty($this->token['openid'])) + return $this->token['openid']; + + $data = $this->call('user'); + return !empty($data['id'])?$data['id']:NULL; + } + + /** + * 获取当前登录的用户信息 + * @return array + */ + public function getOauthInfo(){ + $data = $this->call('user'); + + if(empty($data['code'])){ + $userInfo['type'] = 'GITHUB'; + $userInfo['name'] = $data['login']; + $userInfo['nick'] = $data['name']; + $userInfo['avatar'] = $data['avatar_url']; + return $userInfo; + } else { + throw_exception("获取Github用户信息失败:{$data}"); + } + } + +} \ No newline at end of file diff --git a/Think/Oauth/Driver/Google.php b/Think/Oauth/Driver/Google.php new file mode 100644 index 00000000..64cee8f4 --- /dev/null +++ b/Think/Oauth/Driver/Google.php @@ -0,0 +1,98 @@ + +// +---------------------------------------------------------------------- + +namespace Think\Oauth\Driver; +use Think\Oauth\Driver; +class Google extends Driver{ + /** + * 获取requestCode的api接口 + * @var string + */ + protected $getRequestCodeURL = 'https://accounts.google.com/o/oauth2/auth'; + + /** + * 获取access_token的api接口 + * @var string + */ + protected $getAccessTokenURL = 'https://accounts.google.com/o/oauth2/token'; + + /** + * 获取request_code的额外参数 URL查询字符串格式 + * @var srting + */ + protected $authorize = 'scope=https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email'; + + /** + * API根路径 + * @var string + */ + protected $apiBase = 'https://www.googleapis.com/oauth2/v1/'; + + /** + * 组装接口调用参数 并调用接口 + * @param string $api 微博API + * @param string $param 调用API的额外参数 + * @param string $method HTTP请求方法 默认为GET + * @return json + */ + public function call($api, $param = '', $method = 'GET'){ + /* Google 调用公共参数 */ + $params = array(); + $header = array("Authorization: Bearer {$this->token['access_token']}"); + + $data = $this->http($this->url($api), $this->param($params, $param), $method, $header); + return json_decode($data, true); + } + + /** + * 解析access_token方法请求后的返回值 + * @param string $result 获取access_token的方法的返回值 + */ + protected function parseToken($result){ + $data = json_decode($result, true); + if($data['access_token'] && $data['token_type'] && $data['expires_in']){ + $data['openid'] = $this->getOpenId(); + return $data; + } else + throw new Exception("获取 Google ACCESS_TOKEN出错:未知错误"); + } + + /** + * 获取当前授权应用的openid + * @return string + */ + public function getOpenId(){ + if(!empty($this->token['openid'])) + return $this->token['openid']; + + $data = $this->call('userinfo'); + return !empty($data['id'])?$data['id']:NULL; + } + + /** + * 获取当前登录的用户信息 + * @return array + */ + public function getOauthInfo(){ + $data = $this->call('userinfo'); + + if(!empty($data['id'])){ + $userInfo['type'] = 'GOOGLE'; + $userInfo['name'] = $data['name']; + $userInfo['nick'] = $data['name']; + $userInfo['avatar'] = $data['picture']; + return $userInfo; + } else { + throw_exception("获取Google用户信息失败:{$data}"); + } + } + +} \ No newline at end of file diff --git a/Think/Oauth/Driver/Kaixin.php b/Think/Oauth/Driver/Kaixin.php new file mode 100644 index 00000000..2165a205 --- /dev/null +++ b/Think/Oauth/Driver/Kaixin.php @@ -0,0 +1,93 @@ + +// +---------------------------------------------------------------------- + +namespace Think\Oauth\Driver; +use Think\Oauth\Driver; +class Kaixin extends Driver{ + /** + * 获取requestCode的api接口 + * @var string + */ + protected $getRequestCodeURL = 'http://api.kaixin001.com/oauth2/authorize'; + + /** + * 获取access_token的api接口 + * @var string + */ + protected $getAccessTokenURL = 'https://api.kaixin001.com/oauth2/access_token'; + + /** + * API根路径 + * @var string + */ + protected $apiBase = 'https://api.kaixin001.com/'; + + /** + * 组装接口调用参数 并调用接口 + * @param string $api 开心网API + * @param string $param 调用API的额外参数 + * @param string $method HTTP请求方法 默认为GET + * @return json + */ + public function call($api, $param = '', $method = 'GET'){ + /* 开心网调用公共参数 */ + $params = array( + 'access_token' => $this->token['access_token'], + ); + + $data = $this->http($this->url($api, '.json'), $this->param($params, $param), $method); + return json_decode($data, true); + } + + /** + * 解析access_token方法请求后的返回值 + * @param string $result 获取access_token的方法的返回值 + */ + protected function parseToken($result){ + $data = json_decode($result, true); + if($data['access_token'] && $data['expires_in'] && $data['refresh_token']){ + $data['openid'] = $this->getOpenId(); + return $data; + } else + throw new Exception("获取开心网ACCESS_TOKEN出错:{$data['error']}"); + } + + /** + * 获取当前授权应用的openid + * @return string + */ + public function getOpenId(){ + if(!empty($this->token['openid'])) + return $this->token['openid']; + + $data = $this->call('users/me'); + return !empty($data['uid'])?$data['uid']:NULL; + } + + /** + * 获取当前登录的用户信息 + * @return array + */ + public function getOauthInfo(){ + $data = $this->call('users/me'); + + if(!empty($data['uid'])){ + $userInfo['type'] = 'KAIXIN'; + $userInfo['name'] = $data['uid']; + $userInfo['nick'] = $data['name']; + $userInfo['avatar'] = $data['logo50']; + return $userInfo; + } else { + throw_exception("获取开心网用户信息失败:{$data['error']}"); + } + } + +} \ No newline at end of file diff --git a/Think/Oauth/Driver/Msn.php b/Think/Oauth/Driver/Msn.php new file mode 100644 index 00000000..0a658d6a --- /dev/null +++ b/Think/Oauth/Driver/Msn.php @@ -0,0 +1,99 @@ + +// +---------------------------------------------------------------------- + +namespace Think\Oauth\Driver; +use Think\Oauth\Driver; +class Msn extends Driver{ + /** + * 获取requestCode的api接口 + * @var string + */ + protected $getRequestCodeURL = 'https://login.live.com/oauth20_authorize.srf'; + + /** + * 获取access_token的api接口 + * @var string + */ + protected $getAccessTokenURL = 'https://login.live.com/oauth20_token.srf'; + + /** + * 获取request_code的额外参数 URL查询字符串格式 + * @var srting + */ + protected $authorize = 'scope=wl.basic wl.offline_access wl.signin wl.emails wl.photos'; + + /** + * API根路径 + * @var string + */ + protected $apiBase = 'https://apis.live.net/v5.0/'; + + /** + * 组装接口调用参数 并调用接口 + * @param string $api 微博API + * @param string $param 调用API的额外参数 + * @param string $method HTTP请求方法 默认为GET + * @return json + */ + public function call($api, $param = '', $method = 'GET'){ + /* MSN 调用公共参数 */ + $params = array( + 'access_token' => $this->token['access_token'], + ); + + $data = $this->http($this->url($api), $this->param($params, $param), $method); + return json_decode($data, true); + } + + /** + * 解析access_token方法请求后的返回值 + * @param string $result 获取access_token的方法的返回值 + */ + protected function parseToken($result){ + $data = json_decode($result, true); + if($data['access_token'] && $data['token_type'] && $data['expires_in']){ + $data['openid'] = $this->getOpenId(); + return $data; + } else + throw new Exception("获取 MSN ACCESS_TOKEN出错:未知错误"); + } + + /** + * 获取当前授权应用的openid + * @return string + */ + public function getOpenId(){ + if(!empty($this->token['openid'])) + return $this->token['openid']; + + $data = $this->call('me'); + return !empty($data['id'])?$data['id']:NULL; + } + + /** + * 获取当前登录的用户信息 + * @return array + */ + public function getOauthInfo(){ + $data = $this->call('me'); + + if(!empty($data['id'])){ + $userInfo['type'] = 'MSN'; + $userInfo['name'] = $data['name']; + $userInfo['nick'] = $data['name']; + $userInfo['avatar'] = '微软暂未提供头像URL,请通过 me/picture 接口下载'; + return $userInfo; + } else { + throw_exception("获取msn用户信息失败:{$data}"); + } + } + +} \ No newline at end of file diff --git a/Think/Oauth/Driver/Qq.php b/Think/Oauth/Driver/Qq.php new file mode 100644 index 00000000..c93b2d3c --- /dev/null +++ b/Think/Oauth/Driver/Qq.php @@ -0,0 +1,101 @@ + +// +---------------------------------------------------------------------- + +namespace Think\Oauth\Driver; +use Think\Oauth\Driver; +class Qq extends Driver{ + /** + * 获取requestCode的api接口 + * @var string + */ + protected $getRequestCodeURL = 'https://graph.qq.com/oauth2.0/authorize'; + + /** + * 获取access_token的api接口 + * @var string + */ + protected $getAccessTokenURL = 'https://graph.qq.com/oauth2.0/token'; + + /** + * 获取request_code的额外参数,可在配置中修改 URL查询字符串格式 + * @var srting + */ + protected $authorize = 'scope=get_user_info,add_share'; + + /** + * API根路径 + * @var string + */ + protected $apiBase = 'https://graph.qq.com/'; + + /** + * 组装接口调用参数 并调用接口 + * @param string $api 微博API + * @param string $param 调用API的额外参数 + * @param string $method HTTP请求方法 默认为GET + * @return json + */ + public function call($api, $param = '', $method = 'GET'){ + /* 腾讯QQ调用公共参数 */ + $params = array( + 'oauth_consumer_key' => $this->AppKey, + 'access_token' => $this->token['access_token'], + 'openid' => $this->openid(), + 'format' => 'json' + ); + + $data = $this->http($this->url($api), $this->param($params, $param), $method); + return json_decode($data, true); + } + + /** + * 解析access_token方法请求后的返回值 + * @param string $result 获取access_token的方法的返回值 + */ + protected function parseToken($result){ + parse_str($result, $data); + if($data['access_token'] && $data['expires_in']){ + $data['openid'] = $this->getOpenId(); + return $data; + } else + throw new Exception("获取腾讯QQ ACCESS_TOKEN 出错:{$result}"); + } + + /** + * 获取当前授权应用的openid + * @return string + */ + public function getOpenId(){ + if(!empty($this->token['openid'])) + return $this->token['openid']; + if($data['access_token']){ + $data = $this->http($this->url('oauth2.0/me'), array('access_token' => $data['access_token'])); + $data = json_decode(trim(substr($data, 9), " );\n"), true); + if(isset($data['openid'])) + return $data['openid']; + } + return NULL; + } + + public function getOauthInfo(){ + $data = $this->call('user/get_user_info'); + + if($data['ret'] == 0){ + $userInfo['type'] = 'QQ'; + $userInfo['name'] = $data['nickname']; + $userInfo['nick'] = $data['nickname']; + $userInfo['avatar'] = $data['figureurl_2']; + return $userInfo; + } else { + throw_exception("获取腾讯QQ用户信息失败:{$data['msg']}"); + } + } +} \ No newline at end of file diff --git a/Think/Oauth/Driver/Renren.php b/Think/Oauth/Driver/Renren.php new file mode 100644 index 00000000..b48f7994 --- /dev/null +++ b/Think/Oauth/Driver/Renren.php @@ -0,0 +1,115 @@ + +// +---------------------------------------------------------------------- + +namespace Think\Oauth\Driver; +use Think\Oauth\Driver; +class Renren extends Driver{ + /** + * 获取requestCode的api接口 + * @var string + */ + protected $getRequestCodeURL = 'https://graph.renren.com/oauth/authorize'; + + /** + * 获取access_token的api接口 + * @var string + */ + protected $getAccessTokenURL = 'https://graph.renren.com/oauth/token'; + + /** + * API根路径 + * @var string + */ + protected $apiBase = 'http://api.renren.com/restserver.do'; + + /** + * 组装接口调用参数 并调用接口 + * @param string $api 微博API + * @param string $param 调用API的额外参数 + * @param string $method HTTP请求方法 默认为GET + * @return json + */ + public function call($api, $param = '', $method = 'POST'){ + /* 人人网调用公共参数 */ + $params = array( + 'method' => $api, + 'access_token' => $this->token['access_token'], + 'v' => '1.0', + 'format' => 'json', + ); + + $data = $this->http($this->url(''), $this->param($params, $param), $method); + return json_decode($data, true); + } + + /** + * 合并默认参数和额外参数 + * @param array $params 默认参数 + * @param array/string $param 额外参数 + * @return array: + */ + protected function param($params, $param){ + $params = parent::param($params, $param); + + /* 签名 */ + ksort($params); + $param = array(); + foreach ($params as $key => $value){ + $param[] = "{$key}={$value}"; + } + $sign = implode('', $param).$this->AppSecret; + $params['sig'] = md5($sign); + + return $params; + } + + /** + * 解析access_token方法请求后的返回值 + * @param string $result 获取access_token的方法的返回值 + */ + protected function parseToken($result){ + $data = json_decode($result, true); + if($data['access_token'] && $data['expires_in'] && $data['refresh_token'] && $data['user']['id']){ + $data['openid'] = $data['user']['id']; + unset($data['user']); + return $data; + } else + throw new Exception("获取人人网ACCESS_TOKEN出错:{$data['error_description']}"); + } + + /** + * 获取当前授权应用的openid + * @return string + */ + public function getOpenId(){ + if(!empty($this->token['openid'])) + return $this->token['openid']; + return NULL; + } + + /** + * 获取当前登录的用户信息 + * @return array + */ + public function getOauthInfo(){ + $data = $this->call('users.getInfo'); + + if(!isset($data['error_code'])){ + $userInfo['type'] = 'RENREN'; + $userInfo['name'] = $data[0]['name']; + $userInfo['nick'] = $data[0]['name']; + $userInfo['avatar'] = $data[0]['headurl']; + return $userInfo; + } else { + throw_exception("获取人人网用户信息失败:{$data['error_msg']}"); + } + } +} \ No newline at end of file diff --git a/Think/Oauth/Driver/Sina.php b/Think/Oauth/Driver/Sina.php new file mode 100644 index 00000000..b2ac75a8 --- /dev/null +++ b/Think/Oauth/Driver/Sina.php @@ -0,0 +1,92 @@ + +// +---------------------------------------------------------------------- + +namespace Think\Oauth\Driver; +use Think\Oauth\Driver; +class Sina extends Driver{ + /** + * 获取requestCode的api接口 + * @var string + */ + protected $getRequestCodeURL = 'https://api.weibo.com/oauth2/authorize'; + + /** + * 获取access_token的api接口 + * @var string + */ + protected $getAccessTokenURL = 'https://api.weibo.com/oauth2/access_token'; + + /** + * API根路径 + * @var string + */ + protected $apiBase = 'https://api.weibo.com/2/'; + + /** + * 组装接口调用参数 并调用接口 + * @param string $api 微博API + * @param string $param 调用API的额外参数 + * @param string $method HTTP请求方法 默认为GET + * @return json + */ + public function call($api, $param = '', $method = 'GET'){ + /* 新浪微博调用公共参数 */ + $params = array( + 'access_token' => $this->token['access_token'], + ); + + $data = $this->http($this->url($api, '.json'), $this->param($params, $param), $method); + return json_decode($data, true); + } + + /** + * 解析access_token方法请求后的返回值 + * @param string $result 获取access_token的方法的返回值 + */ + protected function parseToken($result){ + $data = json_decode($result, true); + if($data['access_token'] && $data['expires_in'] && $data['remind_in'] && $data['uid']){ + $data['openid'] = $data['uid']; + unset($data['uid']); + return $data; + } else + throw new Exception("获取新浪微博ACCESS_TOKEN出错:{$data['error']}"); + } + + /** + * 获取当前授权应用的openid + * @return string + */ + public function getOpenId(){ + if(!empty($this->token['openid'])) + return $this->token['openid']; + return NULL; + } + + /** + * 获取当前登录的用户信息 + * @return array + */ + public function getOauthInfo(){ + $data = $this->call('users.getInfo'); + + if(!isset($data['error_code'])){ + $userInfo['type'] = 'RENREN'; + $userInfo['name'] = $data[0]['name']; + $userInfo['nick'] = $data[0]['name']; + $userInfo['avatar'] = $data[0]['headurl']; + return $userInfo; + } else { + throw_exception("获取人人网用户信息失败:{$data['error_msg']}"); + } + } + +} \ No newline at end of file diff --git a/Think/Oauth/Driver/Sohu.php b/Think/Oauth/Driver/Sohu.php new file mode 100644 index 00000000..e0bceb13 --- /dev/null +++ b/Think/Oauth/Driver/Sohu.php @@ -0,0 +1,92 @@ + +// +---------------------------------------------------------------------- + +namespace Think\Oauth\Driver; +use Think\Oauth\Driver; +class Sohu extends Driver{ + /** + * 获取requestCode的api接口 + * @var string + */ + protected $getRequestCodeURL = 'https://api.sohu.com/oauth2/authorize'; + + /** + * 获取access_token的api接口 + * @var string + */ + protected $getAccessTokenURL = 'https://api.sohu.com/oauth2/token'; + + /** + * API根路径 + * @var string + */ + protected $apiBase = 'https://api.sohu.com/rest/'; + + /** + * 组装接口调用参数 并调用接口 + * @param string $api 搜狐API + * @param string $param 调用API的额外参数 + * @param string $method HTTP请求方法 默认为GET + * @return json + */ + public function call($api, $param = '', $method = 'GET'){ + /* 搜狐调用公共参数 */ + $params = array( + 'access_token' => $this->token['access_token'], + ); + + $data = $this->http($this->url($api), $this->param($params, $param), $method); + return json_decode($data, true); + } + + /** + * 解析access_token方法请求后的返回值 + * @param string $result 获取access_token的方法的返回值 + */ + protected function parseToken($result){ + $data = json_decode($result, true); + if($data['access_token'] && $data['expires_in'] && $data['refresh_token'] && $data['open_id']){ + $data['openid'] = $data['open_id']; + unset($data['open_id']); + return $data; + } else + throw new Exception("获取搜狐ACCESS_TOKEN出错:{$data['error']}"); + } + + /** + * 获取当前授权应用的openid + * @return string + */ + public function getOpenId(){ + if(!empty($this->token['openid'])) + return $this->token['openid']; + return NULL; + } + + /** + * 获取当前登录的用户信息 + * @return array + */ + public function getOauthInfo(){ + $data = $this->call('i/prv/1/user/get-basic-info'); + + if('success' == $data['message'] && !empty($data['data'])){ + $userInfo['type'] = 'SOHU'; + $userInfo['name'] = $data['data']['open_id']; + $userInfo['nick'] = $data['data']['nick']; + $userInfo['avatar'] = $data['data']['icon']; + return $userInfo; + } else { + throw_exception("获取搜狐用户信息失败:{$data['message']}"); + } + } + +} \ No newline at end of file diff --git a/Think/Oauth/Driver/T163.php b/Think/Oauth/Driver/T163.php new file mode 100644 index 00000000..345a1fd0 --- /dev/null +++ b/Think/Oauth/Driver/T163.php @@ -0,0 +1,94 @@ + +// +---------------------------------------------------------------------- + +namespace Think\Oauth\Driver; +use Think\Oauth\Driver; +class T163 extends Driver{ + /** + * 获取requestCode的api接口 + * @var string + */ + protected $getRequestCodeURL = 'https://api.t.163.com/oauth2/authorize'; + + /** + * 获取access_token的api接口 + * @var string + */ + protected $getAccessTokenURL = 'https://api.t.163.com/oauth2/access_token'; + + /** + * API根路径 + * @var string + */ + protected $apiBase = 'https://api.t.163.com/'; + + /** + * 组装接口调用参数 并调用接口 + * @param string $api 微博API + * @param string $param 调用API的额外参数 + * @param string $method HTTP请求方法 默认为GET + * @return json + */ + public function call($api, $param = '', $method = 'GET'){ + /* 新浪微博调用公共参数 */ + $params = array( + 'oauth_token' => $this->token['access_token'], + ); + + $data = $this->http($this->url($api, '.json'), $this->param($params, $param), $method); + return json_decode($data, true); + } + + /** + * 解析access_token方法请求后的返回值 + * @param string $result 获取access_token的方法的返回值 + */ + protected function parseToken($result){ + $data = json_decode($result, true); + if($data['uid'] && $data['access_token'] && $data['expires_in'] && $data['refresh_token']){ + $data['openid'] = $data['uid']; + unset($data['uid']); + return $data; + } else + throw new Exception("获取网易微博ACCESS_TOKEN出错:{$data['error']}"); + } + + /** + * 获取当前授权应用的openid + * @return string + */ + public function getOpenId(){ + if(!empty($this->token['openid'])) + return $this->token['openid']; + + $data = $this->call('users/show'); + return !empty($data['id'])?$data['id']:NULL; + } + + /** + * 获取当前登录的用户信息 + * @return array + */ + public function getOauthInfo(){ + $data = $this->call('users/show'); + + if($data['error_code'] == 0){ + $userInfo['type'] = 'T163'; + $userInfo['name'] = $data['name']; + $userInfo['nick'] = $data['screen_name']; + $userInfo['avatar'] = str_replace('w=48&h=48', 'w=180&h=180', $data['profile_image_url']); + return $userInfo; + } else { + throw_exception("获取网易微博用户信息失败:{$data['error']}"); + } + } + +} \ No newline at end of file diff --git a/Think/Oauth/Driver/Taobao.php b/Think/Oauth/Driver/Taobao.php new file mode 100644 index 00000000..4109157f --- /dev/null +++ b/Think/Oauth/Driver/Taobao.php @@ -0,0 +1,96 @@ + +// +---------------------------------------------------------------------- + +namespace Think\Oauth\Driver; +use Think\Oauth\Driver; +class Taobao extends Driver{ + /** + * 获取requestCode的api接口 + * @var string + */ + protected $getRequestCodeURL = 'https://oauth.taobao.com/authorize'; + + /** + * 获取access_token的api接口 + * @var string + */ + protected $getAccessTokenURL = 'https://oauth.taobao.com/token'; + + /** + * API根路径 + * @var string + */ + protected $apiBase = 'https://eco.taobao.com/router/rest'; + + /** + * 组装接口调用参数 并调用接口 + * @param string $api 微博API + * @param string $param 调用API的额外参数 + * @param string $method HTTP请求方法 默认为GET + * @return json + */ + public function call($api, $param = '', $method = 'GET'){ + /* 淘宝网调用公共参数 */ + $params = array( + 'method' => $api, + 'access_token' => $this->token['access_token'], + 'format' => 'json', + 'v' => '2.0', + ); + $data = $this->http($this->url(''), $this->param($params, $param), $method); + return json_decode($data, true); + } + + /** + * 解析access_token方法请求后的返回值 + * @param string $result 获取access_token的方法的返回值 + */ + protected function parseToken($result){ + $data = json_decode($result, true); + if($data['access_token'] && $data['expires_in'] && $data['taobao_user_id']){ + $data['openid'] = $data['taobao_user_id']; + unset($data['taobao_user_id']); + return $data; + } else + throw new Exception("获取淘宝网ACCESS_TOKEN出错:{$data['error']}"); + } + + /** + * 获取当前授权应用的openid + * @return string + */ + public function getOpenId(){ + if(!empty($this->token['openid'])) + return $this->token['openid']; + return NULL; + } + + /** + * 获取当前登录的用户信息 + * @return array + */ + public function getOauthInfo(){ + $fields = 'user_id,nick,sex,buyer_credit,avatar,has_shop,vip_info'; + $data = $this->call('taobao.user.buyer.get', "fields={$fields}"); + + if(!empty($data['user_buyer_get_response']['user'])){ + $user = $data['user_buyer_get_response']['user']; + $userInfo['type'] = 'TAOBAO'; + $userInfo['name'] = $user['user_id']; + $userInfo['nick'] = $user['nick']; + $userInfo['avatar'] = $user['avatar']; + return $userInfo; + } else { + throw_exception("获取淘宝网用户信息失败:{$data['error_response']['msg']}"); + } + } + +} \ No newline at end of file diff --git a/Think/Oauth/Driver/Tencent.php b/Think/Oauth/Driver/Tencent.php new file mode 100644 index 00000000..1be8b330 --- /dev/null +++ b/Think/Oauth/Driver/Tencent.php @@ -0,0 +1,97 @@ + +// +---------------------------------------------------------------------- + +namespace Think\Oauth\Driver; +use Think\Oauth\Driver; +class Tencent extends Driver{ + /** + * 获取requestCode的api接口 + * @var string + */ + protected $getRequestCodeURL = 'https://open.t.qq.com/cgi-bin/oauth2/authorize'; + + /** + * 获取access_token的api接口 + * @var string + */ + protected $getAccessTokenURL = 'https://open.t.qq.com/cgi-bin/oauth2/access_token'; + + /** + * API根路径 + * @var string + */ + protected $apiBase = 'https://open.t.qq.com/api/'; + + /** + * 组装接口调用参数 并调用接口 + * @param string $api 微博API + * @param string $param 调用API的额外参数 + * @param string $method HTTP请求方法 默认为GET + * @return json + */ + public function call($api, $param = '', $method = 'GET'){ + /* 腾讯微博调用公共参数 */ + $params = array( + 'oauth_consumer_key' => $this->AppKey, + 'access_token' => $this->token['access_token'], + 'openid' => $this->openid(), + 'clientip' => get_client_ip(), + 'oauth_version' => '2.a', + 'scope' => 'all', + 'format' => 'json' + ); + + $data = $this->http($this->url($api), $this->param($params, $param), $method); + return json_decode($data, true); + } + + /** + * 解析access_token方法请求后的返回值 + * @param string $result 获取access_token的方法的返回值 + */ + protected function parseToken($result){ + parse_str($result, $data); + $data = array_merge($data, ['openid' => $_GET['openid'], 'openkey' => $_GET['openkey']]); + if($data['access_token'] && $data['expires_in'] && $data['openid']) + return $data; + else + throw new Exception("获取腾讯微博 ACCESS_TOKEN 出错:{$result}"); + } + + /** + * 获取当前授权应用的openid + * @return string + */ + public function getOpenId(){ + if(!empty($this->token['openid'])) + return $this->token['openid']; + return NULL; + } + + /** + * 获取当前登录的用户信息 + * @return array + */ + public function getOauthInfo(){ + $data = $this->call('users.getInfo'); + + if(!isset($data['error_code'])){ + $userInfo['type'] = 'RENREN'; + $userInfo['name'] = $data[0]['name']; + $userInfo['nick'] = $data[0]['name']; + $userInfo['avatar'] = $data[0]['headurl']; + return $userInfo; + } else { + throw_exception("获取人人网用户信息失败:{$data['error_msg']}"); + } + } + +} \ No newline at end of file diff --git a/Think/Oauth/Driver/X360.php b/Think/Oauth/Driver/X360.php new file mode 100644 index 00000000..48c6d2c0 --- /dev/null +++ b/Think/Oauth/Driver/X360.php @@ -0,0 +1,92 @@ + +// +---------------------------------------------------------------------- + +namespace Think\Oauth\Driver; +use Think\Oauth\Driver; +class X360 extends Driver{ + /** + * 获取requestCode的api接口 + * @var string + */ + protected $getRequestCodeURL = 'https://openapi.360.cn/oauth2/authorize'; + + /** + * 获取access_token的api接口 + * @var string + */ + protected $getAccessTokenURL = 'https://openapi.360.cn/oauth2/access_token'; + + /** + * API根路径 + * @var string + */ + protected $apiBase = 'https://openapi.360.cn/'; + + /** + * 组装接口调用参数 并调用接口 + * @param string $api 360开放平台API + * @param string $param 调用API的额外参数 + * @param string $method HTTP请求方法 默认为GET + * @return json + */ + public function call($api, $param = '', $method = 'GET'){ + /* 360开放平台调用公共参数 */ + $params = array( + 'access_token' => $this->token['access_token'], + ); + + $data = $this->http($this->url($api, '.json'), $this->param($params, $param), $method); + return json_decode($data, true); + } + + /** + * 解析access_token方法请求后的返回值 + * @param string $result 获取access_token的方法的返回值 + */ + protected function parseToken($result){ + $data = json_decode($result, true); + if($data['access_token'] && $data['expires_in'] && $data['refresh_token']){ + $data['openid'] = $this->getOpenId(); + return $data; + } else + throw new Exception("获取360开放平台ACCESS_TOKEN出错:{$data['error']}"); + } + + /** + * 获取当前授权应用的openid + * @return string + */ + public function getOpenId(){ + if(!empty($this->token['openid'])) + return $this->token['openid']; + $data = $this->call('user/me'); + return !empty($data['id'])?$data['id']:NULL; + } + + /** + * 获取当前登录的用户信息 + * @return array + */ + public function getOauthInfo(){ + $data = $this->call('user/me'); + + if($data['error_code'] == 0){ + $userInfo['type'] = 'X360'; + $userInfo['name'] = $data['name']; + $userInfo['nick'] = $data['name']; + $userInfo['avatar'] = $data['avatar']; + return $userInfo; + } else { + throw_exception("获取360用户信息失败:{$data['error']}"); + } + } + +} \ No newline at end of file