2 Star 0 Fork 1

free-worker/独立系统php示例

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
oauth.php 3.59 KB
一键复制 编辑 原始数据 按行查看 历史
野生程序员 提交于 2022-07-29 10:01 . fix: add dockerfile
<?php
include __DIR__.'/vendor/autoload.php';
/**
*
*/
class oauth
{
private $appid;
private $appsecret;
private $client;
public function __construct(string $appid, string $appsecret)
{
$this->appid = $appid;
$this->appsecret = $appsecret;
$this->client = new \GuzzleHttp\Client(['base_uri'=>'https://openapi.w7.cc']);
}
/**
* "https://passport.w7.cc/oauth/authorize/index?appid=309250&redirect_url=http%3A%2F%2Fbaidu.com&install_type=0"
* @return string
*/
public function redirect(string $redirectUrl)
{
return $this->getLoginUrl($redirectUrl);
}
/**
* array(6) {
'username' =>
string(6) "memeto"
'avatar' =>
string(95) "https://cdn.w7.cc/images/avatar/0/410/884.jpg?v=1658830310&imageView2/5/w/100/h/100/format/webp"
'nickname' =>
string(6) "memeto"
'founder_openid' =>
string(22) "tFfxsAMp7YdTKINFQR46zw"
'openid' =>
string(22) "tFfxsAMp7YdTKINFQR46zw"
'open_id' =>
string(22) "tFfxsAMp7YdTKINFQR46zw"
}
* @param string $code
* @return false|array
* @throws OAuthException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function user(string $code)
{
$tokenArr = $this->getAccessToken($code);
if (!is_array($tokenArr)) {
return false;
}
$token = isset($tokenArr['access_token']) ? $tokenArr['access_token'] : false;
if ($token === false) {
return false;
}
$userInfo = $this->post('/we7/open/oauth/user/info', ['access_token' => $token]);
return $userInfo;
}
/**
* @param string $code
* @return array | false
*/
public function getAccessToken(string $code)
{
$result = $this->post('/we7/open/oauth/access-token/code', ['code'=>$code], true);
return $result;
}
/**
* @return array|bool
*/
protected function getLoginUrl(string $redirectUrl)
{
$result = $this->post('/we7/open/oauth/login-url/index',
[
'appid'=>$this->appid,
'redirect' => $redirectUrl,
], true);
if (is_array($result))
{
return isset($result['url']) ? $result['url'] : false;
}
return false;
}
/**
* 发送请求
* @param string $url
* @param array $data
* @param bool $sign
* @return false|string
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function post(string $url, array $data, bool $sign = true)
{
if ($sign)
{
$data = $this->sign($data);
}
$result = $this->client->post($url, ['form_params' => $data]);
if ($result->getStatusCode() === 200)
{
return json_decode($result->getBody()->getContents(), true);
}
return false;
}
private function sign(array $data) : array
{
$params = [
'appid' => $this->appid,
'timestamp' => time(),
'nonce' => random_int(1000, 10000).'',
// 'code' => $code
];
$params = array_merge($params, $data);
ksort($params);
reset($params);
$params = array_map(function ($value){
return is_null($value) ? '' : $value;
}, $params);
$sign = md5(http_build_query($params). $this->appsecret);
$params['sign'] = $sign;
return $params;
}
}
$appid = '309250';
$appsecret = '';
$code = 'uIx56I9ZTx0EV6u5yE0UI6X9NU19EQeq';
$oauth = new oauth($appid, $appsecret);
$info = $oauth->user($code);
var_dump($info);
$url = $oauth->redirect('http://baidu.com');
var_dump($url);
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/free-worker/w7-tcb-php-sample.git
git@gitee.com:free-worker/w7-tcb-php-sample.git
free-worker
w7-tcb-php-sample
独立系统php示例
master

搜索帮助