add ZMRequest support

This commit is contained in:
crazywhalecc 2022-12-31 15:35:19 +08:00
parent 3734f5d476
commit 2a3c953c36
No known key found for this signature in database
GPG Key ID: 4B0FFA175E762022
2 changed files with 102 additions and 0 deletions

View File

@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
namespace ZM\Utils;
use OneBot\Util\Singleton;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
use ZM\Framework;
class ZMRequest
{
use Singleton;
/**
* 快速发起一个 GET 请求
*
* @param string|\Stringable|UriInterface $url 请求地址
* @param array $headers 请求头
* @param array $config 传入参数
* @param bool $only_body 是否只返回 Response body 部分,默认为 True
* @return bool|ResponseInterface|string 返回 False 代表请求失败,返回 string 为仅 Body 的内容,返回 Response 接口对象表明是回包
*/
public static function get(string|UriInterface|\Stringable $url, array $headers = [], array $config = [], bool $only_body = true): bool|ResponseInterface|string
{
$socket = Framework::getInstance()->getDriver()->createHttpClientSocket(array_merge_recursive([
'url' => ($url instanceof UriInterface ? $url->__toString() : $url),
], $config));
$socket->withoutAsync();
$obj = $socket->get($headers, function (ResponseInterface $response) { return $response; }, function () { return false; });
if ($obj instanceof ResponseInterface) {
if ($obj->getStatusCode() !== 200 && $only_body) {
return false;
}
if (!$only_body) {
return $obj;
}
return $obj->getBody()->getContents();
}
return $obj;
}
/**
* 快速发起一个 POST 请求
*
* @param string|\Stringable|UriInterface $url 请求地址
* @param array $header 请求头
* @param mixed $data 请求数据,当传入了一个可以 Json 化的对象时,自动 json_encode其他情况须传入可字符串化的变量
* @param array $config 传入参数
* @param bool $only_body 是否只返回 Response body 部分,默认为 True
* @return bool|ResponseInterface|string 返回 False 代表请求失败,返回 string 为仅 Body 的内容,返回 Response 接口对象表明是回包
*/
public static function post(string|UriInterface|\Stringable $url, array $header, mixed $data, array $config = [], bool $only_body = true): bool|ResponseInterface|string
{
$socket = Framework::getInstance()->getDriver()->createHttpClientSocket(array_merge_recursive([
'url' => ($url instanceof UriInterface ? $url->__toString() : $url),
], $config));
$socket->withoutAsync();
$obj = $socket->post($data, $header, fn (ResponseInterface $response) => $response, fn () => false);
if ($obj instanceof ResponseInterface) {
if ($obj->getStatusCode() !== 200 && $only_body) {
return false;
}
if (!$only_body) {
return $obj;
}
return $obj->getBody()->getContents();
}
return $obj;
}
}

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Tests\ZM\Utils;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use ZM\Utils\ZMRequest;
/**
* @internal
*/
class ZMRequestTest extends TestCase
{
public function testPost()
{
$r = ZMRequest::post('http://httpbin.org/post', [], 'niubi=123');
$this->assertStringContainsString('123', $r);
$r2 = ZMRequest::post('http://httpbin.org/post', ['User-Agent' => 'test'], 'oijoij=ooo', [], false);
$this->assertInstanceOf(ResponseInterface::class, $r2);
$this->assertStringContainsString('ooo', $r2->getBody()->getContents());
}
public function testGet()
{
$r = ZMRequest::get('http://ip.zhamao.xin');
$this->assertStringContainsString('114', $r);
}
}