php-auth-client/src/PdAuth/OAuth.php

82 lines
1.8 KiB
PHP
Raw Normal View History

2018-01-02 03:15:51 +00:00
<?php
namespace PdAuth;
2018-01-05 07:42:41 +00:00
class OAuth
2018-01-02 03:15:51 +00:00
{
protected $host;
protected $id;
protected $secret;
public function __construct($config)
{
$this->host = $config['host'];
$this->id = $config['appid'];
$this->secret = $config['secret'];
}
/**
* 生成授权的链接
* @param $redirect
* @return string
*/
public function connect($redirect)
{
$redirect = urlencode($redirect);
return $this->host . "/connect?appid={$this->id}&redirect=$redirect";
}
2018-01-05 07:42:41 +00:00
public function getAccessToken($code)
{
$resp = $this->get("$this->host/api/access_token?id={$this->id}&secret={$this->secret}&code={$code}");
if ($resp['code'] == 0) {
return $resp['data'];
}
return null;
}
2018-01-02 03:15:51 +00:00
/**
* 根据用户token获取用户信息
* @param $username
* @param $token
* @return array|null
*/
2018-01-05 07:42:41 +00:00
public function getUserInfo($token)
{
$token = urlencode($token);
$resp = $this->get("$this->host/api/user_info?access_token=$token");
if ($resp['code'] == 0) {
return $resp['data'];
}
return null;
}
public function getGroups($token)
2018-01-02 03:15:51 +00:00
{
$token = urlencode($token);
2018-01-05 07:42:41 +00:00
$resp = $this->get("$this->host/api/{$this->id}/groups?access_token=$token");
if ($resp['code'] == 0) {
2018-01-02 03:15:51 +00:00
return $resp['data'];
}
return null;
}
/**
* pd auth 系统发起请求
* @param $url
* @return mixed|null
*/
protected function get($url)
{
$client = new \GuzzleHttp\Client();
$res = $client->request('GET', $url);
if ($res->getStatusCode() == 200) {
return \GuzzleHttp\json_decode($res->getBody(), true);
} else {
return null;
}
}
}