michaelhall / http-client
简单的HTTP客户端
v2.1.0
2022-12-28 20:19 UTC
Requires
- php: >=8.0
- ext-curl: *
- ext-fileinfo: *
- datatypes/datatypes: ^3.0
Requires (Dev)
- ext-json: *
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-29 00:06:19 UTC
README
简单的HTTP客户端。
要求
- PHP >= 8.0
使用Composer安装
$ composer require michaelhall/http-client
基本用法
获取网页
<?php require_once __DIR__ . '/vendor/autoload.php'; use DataTypes\Net\Url; use MichaelHall\HttpClient\HttpClient; use MichaelHall\HttpClient\HttpClientRequest; $url = Url::parse('https://example.com/'); $client = new HttpClient(); $request = new HttpClientRequest($url); $response = $client->send($request); // Prints "success" if request was successful, "fail" otherwise. echo $response->isSuccessful() ? 'success' : 'fail'; // Prints the response content. echo $response->getContent(); // Prints the http code, e.g. 200. echo $response->getHttpCode(); // Prints the headers. foreach ($response->getHeaders() as $header) { echo $header; }
自定义请求
<?php require_once __DIR__ . '/vendor/autoload.php'; use DataTypes\Net\Url; use DataTypes\System\FilePath; use MichaelHall\HttpClient\HttpClientRequest; $url = Url::parse('https://example.com/'); // Set the method. $request = new HttpClientRequest($url, 'POST'); // Set a POST field. $request->setPostField('Foo', 'Bar'); // Set a file. $request->setFile('Baz', FilePath::parse('/path/to/file')); // Add a header. $request->addHeader('Content-type: application/json'); // Set raw content. $request->setRawContent('{"Foo": "Bar"}'); // Client certificates. $request->setCACertificate(FilePath::parse('/path/to/ca-certificate.pem')); $request->setClientCertificate(FilePath::parse('/path/to/client-certificate.pem')); $request->setClientKey(FilePath::parse('/path/to/client-key.pem'));
创建自定义请求处理器
可以创建一个自定义/伪造的请求处理器并在HttpClient
构造函数中注入。为此,请求处理器必须实现RequestHandlerInterface
接口和handleRequest
方法。
<?php use DataTypes\Net\Url; use MichaelHall\HttpClient\HttpClient; use MichaelHall\HttpClient\HttpClientRequest; use MichaelHall\HttpClient\HttpClientRequestInterface; use MichaelHall\HttpClient\HttpClientResponse; use MichaelHall\HttpClient\HttpClientResponseInterface; use MichaelHall\HttpClient\RequestHandlers\RequestHandlerInterface; require_once __DIR__ . '/vendor/autoload.php'; class FakeRequestHandler implements RequestHandlerInterface { public function handleRequest(HttpClientRequestInterface $request): HttpClientResponseInterface { if ($request->getUrl()->getPath()->__toString() === '/foo') { return new HttpClientResponse(200, 'Hello World'); } return new HttpClientResponse(404); } } // Inject the custom request handler in constructor. $client = new HttpClient(new FakeRequestHandler()); $request = new HttpClientRequest(Url::parse('https://example.com/foo')); $response = $client->send($request); // Prints "Hello World". echo $response->getContent(); $request = new HttpClientRequest(Url::parse('https://example.com/')); $response = $client->send($request); // Prints "404". echo $response->getHttpCode();
许可证
MIT