alanvdb / client
自动处理SSL证书检索的基本HTTP客户端
v0.1
2024-08-27 19:03 UTC
Requires
- guzzlehttp/guzzle: ^7.9
- psr/http-client: ^1.0
Requires (Dev)
- phpunit/phpunit: ^11.3
This package is auto-updated.
Last update: 2024-09-27 19:21:34 UTC
README
概述
alanvdb/http-client
是一个符合PSR-18的HTTP客户端库,它可以自动处理指定域的SSL证书检索。它基于Guzzle构建,旨在通过自动管理SSL证书简化HTTP请求,同时确保安全连接。
特性
- PSR-18兼容:可以无缝集成到任何PSR-18 HTTP客户端接口实现。
- 自动SSL证书检索:自动获取并存储指定域的SSL证书。
- 可定制SSL证书检索器:可以轻松定制SSL证书检索器或扩展它以满足特定需求。
- 工厂模式:`HttpClientFactory`允许轻松实例化和依赖注入。
- 基于Guzzle:利用Guzzle强大的HTTP客户端功能。
- 易于使用:简单API用于发送HTTP请求和处理SSL证书。
安装
您可以通过Composer安装此包
composer require alanvdb/http-client
为了开发目的,您还可以安装开发依赖项
composer install --dev
使用方法
基本使用
以下是如何使用HttpClient
向域发送GET请求的示例
require 'vendor/autoload.php'; use AlanVdb\HttpClient\Factory\HttpClientFactory; use GuzzleHttp\Psr7\Request; // Directory where SSL certificates will be stored $directoryPath = __DIR__ . '/path/to/ssl/certificates'; $factory = new HttpClientFactory(); $client = $factory->createHttpClient($directoryPath); $request = new Request('GET', 'https://example.com'); $response = $client->sendRequest($request); echo $response->getBody();
处理SSL证书
HttpClient
将自动检索请求中指定的域的SSL证书,并将其保存到您提供的目录中。这确保了安全连接,无需手动管理证书。
自定义SSL证书检索器
您还可以通过提供您的SslCertificateFetcherInterface
实现来定制SSL证书检索过程。
use AlanVdb\HttpClient\Factory\HttpClientFactory; use AlanVdb\HttpClient\Definition\SslCertificateFetcherInterface; class CustomSslCertificateFetcher implements SslCertificateFetcherInterface { // Implement the required methods here } $directoryPath = __DIR__ . '/path/to/ssl/certificates'; $customFetcher = new CustomSslCertificateFetcher(); $factory = new HttpClientFactory(); $client = $factory->createHttpClient($directoryPath, $customFetcher);
使用工厂模式
HttpClientFactory
提供了一种方便的方式来创建HttpClient
实例。
use AlanVdb\HttpClient\Factory\HttpClientFactory; $directoryPath = __DIR__ . '/path/to/ssl/certificates'; $factory = new HttpClientFactory(); $client = $factory->createHttpClient($directoryPath);
运行测试
要运行测试,您需要首先安装开发依赖项
composer install --dev
然后,您可以使用PHPUnit运行测试
vendor/bin/phpunit --testdox
许可证
本项目受MIT许可证的许可。有关详细信息,请参阅LICENSE文件。