Files

74 lines
2.1 KiB
PHP

<?php
namespace app\common\library;
use GuzzleHttp\Client as GuzzleHttpClient;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\TransferStats;
class Client
{
/**
* 获取请求对象
* @return Client
*/
public static function getClient()
{
if(\app\common\library\Platform::getSystemType() == 'single'){
$domain = request()->domain();
//thinkphp5获取系统的域名,如 baidu.com 去除 https或 http
}else{
$domain = request()->domain() . '/app/index.php';
}
$domain = preg_replace('/^(http|https):\/\//i', '', $domain);
$options = [
'base_uri' => 'https://www.tuzhi.ltd/',
'timeout' => 1000,
'connect_timeout' => 300,
'verify' => false,
'http_errors' => false,
'headers' => [
'X-REQUESTED-WITH' => 'XMLHttpRequest',
'Referer' => $domain,
]
];
static $client;
if (empty($client)) {
$client = new GuzzleHttpClient($options);
}
return $client;
}
public static function request($url,$params = []){
$license_token = input('license_token');
$data = false;
try {
$params['token'] = $license_token;
$client = self::getClient();
$response = $client->request("POST",$url,[
'form_params'=>$params
]);
$body = $response->getBody();
$data = $body->getContents();
} catch (\Exception $e) {
throw new \Exception('请求授权服务器异常,请刷新重试');
}
if(!$data){
throw new \Exception('请求失败,请刷新重试');
}
$data = json_decode($data,true);
if(!$data){
throw new \Exception('请求授权服务器数据错误,请刷新重试');
}
if($data['code'] !=1){
throw new \Exception($data['msg']);
}
return $data['data'];
}
}