Merge pull request #216 from zhamao-robot/zm-request-update

新增 ZMRequest
This commit is contained in:
Jerry
2022-12-31 15:44:15 +08:00
committed by GitHub
3 changed files with 107 additions and 2 deletions

View File

@@ -51,8 +51,8 @@ class Framework
/** @var array 传入的参数 */
protected array $argv;
/** @var Driver|SwooleDriver|WorkermanDriver OneBot驱动 */
protected SwooleDriver|Driver|WorkermanDriver $driver;
/** @var null|Driver|SwooleDriver|WorkermanDriver OneBot驱动 */
protected SwooleDriver|Driver|WorkermanDriver|null $driver = null;
/** @var array<array<string, string>> 启动注解列表 */
protected array $setup_annotations = [];
@@ -178,6 +178,9 @@ class Framework
*/
public function getDriver(): Driver
{
if ($this->driver === null) {
$this->driver = new WorkermanDriver();
}
return $this->driver;
}

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);
}
}