diff --git a/src/ZM/Utils/ZMRequest.php b/src/ZM/Utils/ZMRequest.php new file mode 100644 index 00000000..4c6d1a56 --- /dev/null +++ b/src/ZM/Utils/ZMRequest.php @@ -0,0 +1,72 @@ +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; + } +} diff --git a/tests/ZM/Utils/ZMRequestTest.php b/tests/ZM/Utils/ZMRequestTest.php new file mode 100644 index 00000000..e27658b6 --- /dev/null +++ b/tests/ZM/Utils/ZMRequestTest.php @@ -0,0 +1,30 @@ +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); + } +}