Files
swiftadmin/extend/system/Http.php

109 lines
3.6 KiB
PHP
Raw Normal View History

2022-08-19 19:48:37 +08:00
<?php
namespace system;
use GuzzleHttp\Client;
/**
* Http 请求类
*/
class Http
{
/**
* PC/Mobile 标识
2023-07-04 18:40:50 +08:00
* @var array 对象实例
2022-08-19 19:48:37 +08:00
*/
2023-07-04 18:40:50 +08:00
protected static array $agent = [
2022-08-19 19:48:37 +08:00
'Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10',
2023-07-04 18:40:50 +08:00
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (HTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
2022-08-19 19:48:37 +08:00
];
/**
* 发送一个POST请求
* @param string $url
* @param array $params
* @param bool $agent
* @param array $options
2022-11-28 19:11:12 +08:00
* @param array $header
2023-07-04 18:40:50 +08:00
* @param bool $headerOnly
2022-08-19 19:48:37 +08:00
* @return mixed|string
*/
2023-07-04 18:40:50 +08:00
public static function post(string $url, array $params = [], bool $agent = true, array $options = [], array $header = [], bool $headerOnly = false)
2022-08-19 19:48:37 +08:00
{
$req = self::request($url, $params, $agent, 'POST', $options, $header);
2023-07-04 18:40:50 +08:00
return $headerOnly ? ($req['ret'] ? ['data' => $req['msg'], 'header' => $req['header']] : ['header' => $req['header']]) : ($req['ret'] ? $req['msg'] : '');
2022-08-19 19:48:37 +08:00
}
/**
* 发送一个GET请求
* @param string $url
* @param array $params
* @param bool $agent
* @param array $options
2022-11-28 19:11:12 +08:00
* @param array $header
2023-07-04 18:40:50 +08:00
* @param bool $headerOnly
2022-08-19 19:48:37 +08:00
* @return mixed|string
*/
2023-07-04 18:40:50 +08:00
public static function get(string $url, array $params = [], bool $agent = true, array $options = [], array $header = [], bool $headerOnly = false)
2022-08-19 19:48:37 +08:00
{
$req = self::request($url, $params, $agent, 'GET', $options, $header);
2023-07-04 18:40:50 +08:00
return $headerOnly ? ($req['ret'] ? ['data' => $req['msg'], 'header' => $req['header']] : ['header' => $req['header']]) : ($req['ret'] ? $req['msg'] : '');
2022-08-19 19:48:37 +08:00
}
/**
* @param string $url
* @param array $params
* @param bool $agent
* @param string $method
* @param array $options
* @param array $header
* @return array
*/
public static function request(string $url, array $params, bool $agent, string $method = 'GET', array $options = [], array $header = []): array
{
try {
$client = self::getClient($agent, $options, $header);
2023-07-04 18:40:50 +08:00
$query = $method == 'GET' ? ['query' => $params] : ['form_params' => $params];
$response = $client->request($method, $url, $query);
$content = $response->getBody()->getContents();
$header = $response->getHeaders();
if (!empty($content)) {
return ['ret' => true, 'msg' => $content, 'header' => $header];
2022-08-19 19:48:37 +08:00
}
} catch (\Throwable $e) {
2023-07-04 18:40:50 +08:00
return ['ret' => false, 'msg' => $e->getMessage(), 'header' => $header];
2022-08-19 19:48:37 +08:00
}
2023-07-04 18:40:50 +08:00
return ['ret' => false, 'msg' => $content, 'header' => $header];
2022-08-19 19:48:37 +08:00
}
/**
* 获取访问客户端
* @param bool $agent
* @param array $options
* @param array $header
2023-07-04 18:40:50 +08:00
* @return Client
2022-08-19 19:48:37 +08:00
*/
2023-07-04 18:40:50 +08:00
private static function getClient(bool $agent, array $options = [], array $header = []): Client
2022-08-19 19:48:37 +08:00
{
if (empty($options)) {
$options = [
'timeout' => 60,
'connect_timeout' => 60,
'verify' => false,
'http_errors' => false,
'headers' => [
'X-REQUESTED-WITH' => 'XMLHttpRequest',
'Referer' => dirname(request()->url()),
'User-Agent' => self::$agent[$agent]
]
];
}
if (!empty($header)) {
$options['headers'] = array_merge($options['headers'], $header);
}
return new Client($options);
}
2023-07-04 18:40:50 +08:00
}