juststeveking / http-slim
一个精简的PSR合规HTTP客户端,以提供更好的互操作性。
v2.1.0
2021-08-27 11:05 UTC
Requires
- php: ^8.0
- ext-json: *
- php-http/client-common: ^2.4
- php-http/discovery: ^1.14
- php-http/mock-client: ^1.5
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- thecodingmachine/safe: ^1.3
Requires (Dev)
- nyholm/psr7: ^1.3
- pestphp/pest: ^1.17
- pestphp/pest-plugin-parallel: ^0.3.0
- php-parallel-lint/php-parallel-lint: ^1.2
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.1
- squizlabs/php_codesniffer: ^3.5
- symfony/http-client: ^5.0
- symfony/var-dumper: ^5.0
- thecodingmachine/phpstan-safe-rule: ^1.0
Provides
This package is auto-updated.
Last update: 2024-09-21 23:56:29 UTC
README
此包的目的是创建一个互操作的HTTP客户端实现,允许采用“自行携带包”的方法连接到第三方服务。
此包的主要目标是遵循以下PSR规范
安装
使用composer
$ composer require juststeveking/http-slim
您可以在项目需要时自由使用它。
下一步
下一步是安装一个符合规范的包来执行请求本身。
一些推荐的包
使用方法
一旦您安装了PSR-18包和PSR-17包,我们就不需要做任何事情。此包支持您的PSR合规客户端和请求工厂的HTTP自动发现。然而,如果您选择注入,您也可以这样做!
<?php declare(strict_types=1); use JustSteveKing\HttpSlim\HttpClient; use Symfony\Component\HttpClient\Psr18Client; // Injecting Clients: $httpClient = HttpClient::build( clientInterface: new Psr18Client(), requestFactory: new Psr18Client(), streamFactory: new Psr18Client(), ); // Using HTTP Auto-discovery $httpClient = HttpClient::build(); // perform a get request $httpClient->get( uri: 'https://api.example.com/v1/resource', ); // perform a post request $httpClient->post( uri: 'https://api.example.com/v1/resource', body: ['foo' => 'bar'], ); // perform a put request $httpClient->put( uri: 'https://api.example.com/v1/resource/identifier', body: ['foo' => 'bar'], ); // perform a patch request $httpClient->patch( uri: 'https://api.example.com/v1/resource/identifier', body: ['foo' => 'bar'], ); // perform a delete request $httpClient->delete( uri: 'https://api.example.com/v1/resource/identifier', ); // perform an options request $httpClient->options( uri: 'https://api.example.com/v1/resource/identifier', headers: ['X-OPTIONAL' => 'headers'], ); // Adding Plugins $httpClient->addPlugin( plugin: new \Http\Client\Common\Plugin\HeaderDefaultsPlugin( headers: ['Content-Type' => 'application/json'], ), );
测试
有可用的composer脚本来运行测试
$ composer run preflight:test
但是,如果您无法运行此命令,请使用以下命令
$ ./vendor/bin/phpunit --testdox
安全性
如果您发现任何与安全相关的问题,请通过电子邮件[email protected]联系,而不是使用问题跟踪器。