代码拉取完成,页面将自动刷新
<?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);
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。