Download the PHP package strictphp/http-clients without Composer
On this page you can find all versions of the php package strictphp/http-clients. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download strictphp/http-clients
More information about strictphp/http-clients
Files in strictphp/http-clients
Package http-clients
Short Description Various http client implementations for better developer and devops experience.
License MIT
Homepage https://github.com/strictphp/http-clients
Informations about the package http-clients
HTTP Clients
The HTTP Clients package provides a collection of HTTP clients that can be used to manage HTTP requests and responses in your PHP application. The package includes a ClientsFactory to simplify the creation of clients by allowing you to define a list of ClientFactoryContract implementations.
Features
- Uses PSR container for dependency injection.
- CacheResponseClient: Utilizes PSR-6 (simple-cache) for caching responses, improving development speed by serving cached responses for subsequent requests.
- CustomizeRequestClient: You can modify the request before sending it.
- EventClient: Dependent on PSR-14 (event-dispatcher) and enables you to attach events before, during, or after a request, which is useful for logging or other actions.
- MainClient: First middleware.
- MockClient: Prepared for tests.
- RetryClient: If the call sendRequest throw exception, it tries to send request once more.
- SleepClient: Allows you to introduce a wait interval between requests, which may be necessary for interacting with external APIs that require rate limiting.
- StoreClient: Save your REQuests as PHPStorm
REQ.http
file and corresponding RESponse as a file with suffixRES.<code>.[headers|xml|txt|json|html|pdf]
.
Integration
- Laravel czech
- Nette czech
Installation
You can install the HTTP Clients package via Composer:
Usage
The ClientsFactory simplifies the creation of clients by allowing you to define a list of ClientFactoryContract implementations with dependency injection.
Example:
These examples demonstrate how to efficiently manage HTTP requests and responses in your PHP application using the provided HTTP client classes and the ClientsFactory.
ConfigManager
Config manager is designed for setting different configuration per host (IP, domain). Each HTTP client can contain Config
class in their namespace.
Setting up default configuration
Setting up override for given domain
You should set your DI container to provide ConfigManager as a singleton.
Clients
- Each client can be built using its own Factory class (in their namespace). Factory uses a DI container that should resolve: ClientInterface for HTTP/s communication and ConfigManaer
- Each client can be configured by ConfigManager.
CacheResponseClient (file)
The CacheResponseClient utilizes PSR-6 (simple-cache) for caching responses, improving development speed by serving cached responses for subsequent requests. Here are some benefits and considerations:
- Development Efficiency: Speeds up development by caching responses, reducing the need for repeated API calls during development.
- Local Testing: Enable the
saveOnly
option in production to cache responses and download them for testing on localhost, ensuring consistency and performance. - Customization: Customize cache key preparation by implementing your own contract in CacheKeyMakerAction.php.
CustomizeRequestClient (file)
Alter request before sending it to the HTTPClient. You can throw ClientExceptionInterface. This client could be useful for testing error handling mechanisms in your application and use cached *.shttp file.
CustomResponseClient (file)
Subject to change.
You can define custom response file foe each host.
The #1 parameter in constructor can be:
- a path on serialized SaveResponse.php, file with extension
shttp
it's meanserialized http
- a path on file with plain text, this is used like body only
EventClient (file)
dependent on PSR-14 (event-dispatcher)
You can attach events before, failed or request success. It is useful for logging.
- save http file for PHPStorm SaveForPhpstormRequest.php
- save response SaveResponse.php
MainHttpClient (file)
This is first endpoint for debugging. If you use ClientsFactory, this instance will be returned.
MockClient (file)
MockClient use for tests, you define response for current scope.
RetryClient (file)
Retry client is designed to retry failed request with ability to define number of tries and allowlist (based on exception).
SleepClient (file)
The SleepClient allows you to introduce a wait interval between requests, which may be necessary for interacting with external APIs that require rate limiting.
StoreClient (file)
The StoreClient saves request and response, without dependency on PSR-14
- save http file for PHPStorm SaveForPhpstormRequest.php
- save response SaveResponse.php
Testing
You can save file with extension *.shttp by MockClient.
Write your own client
You can write your own client simply by implementing these interfaces:
- Client must implement
Psr\Http\Client\ClientInterface
. - Config must implement
StrictPhp\HttpClients\Contracts\ConfigInteface
- Factory for client implement
StrictPhp\HttpClients\Contracts\ClientFactoryContract
Below is an example of an implementation:
Config
Client
ClientFactory
All versions of http-clients with dependencies
guzzlehttp/psr7 Version ^2.5
illuminate/filesystem Version ^9.0 || ^10.0 || ^11.0 || ^12.0
psr/container Version ^2.0
psr/event-dispatcher Version ^1.0
psr/http-client Version ^1.0
psr/http-message Version ^1.0 || ^2.0
psr/log Version ^3.0
psr/simple-cache Version ^3.0