webservco / http-client
A PHP component/library. PSR-18 HTTP client built on cURL, plus a client that sends several requests in parallel.
Requires
- php: ^8.4
- ext-curl: *
- fig/http-message-util: ^1
- psr/http-client: ^1
- psr/http-factory: ^1
- webservco/log: ^2
Requires (Dev)
- pds/skeleton: ^1
- phan/phan: ^6
- php-parallel-lint/php-console-highlighter: ^1
- php-parallel-lint/php-parallel-lint: ^1
- phpmd/phpmd: ^3
- phpstan/phpstan: ^2
- phpstan/phpstan-deprecation-rules: ^2
- phpstan/phpstan-phpunit: ^2
- phpstan/phpstan-strict-rules: ^2
- phpunit/phpunit: ^13
- slevomat/coding-standard: ^8
- squizlabs/php_codesniffer: ^4
- vimeo/psalm: ^6
- webservco/coding-standards: ^2
- webservco/component-common: ^2
Suggests
None
Provides
Conflicts
None
Replaces
None
README
A PHP component/library. Please see webservco/sandbox for an example project using these types of components.
PSR-18 HTTP client built on cURL, plus a client that sends several requests in parallel.
- Implements: PSR-18: HTTP Client
- Provides:
psr/http-client-implementation - Requires:
ext-curl, a PSR-17 implementation such aswebservco/http - Related: webservco/http-client-test (tests, kept separate to avoid extra dependencies here)
Installation
composer require webservco/http-client
Usage
Create the cURL service
Every client needs it. This factory uses the webservco/http factories:
use WebServCo\Http\Client\DataTransfer\CurlServiceConfiguration; use WebServCo\Http\Client\Service\cURL\CurlService; use WebServCo\Http\Factory\Message\Response\ResponseFactory; use WebServCo\Http\Factory\Message\Stream\StreamFactory; use WebServCo\Http\Service\Message\Response\StatusCodeService; use WebServCo\Log\Factory\ContextFileLoggerFactory; $streamFactory = new StreamFactory(); // \WebServCo\Http\Client\Contract\Service\cURL\CurlServiceInterface $curlService = new CurlService( // Debug mode (logs requests and responses), timeout in seconds. new CurlServiceConfiguration(true, 30), // \WebServCo\Log\Contract\LoggerFactoryInterface new ContextFileLoggerFactory(sprintf('%svar/log', $projectPath)), // PSR-17: \Psr\Http\Message\ResponseFactoryInterface new ResponseFactory(new StatusCodeService(), $streamFactory), // PSR-17: \Psr\Http\Message\StreamFactoryInterface $streamFactory, );
Send a request
use WebServCo\Http\Client\Service\PSR18\HttpClient; // PSR-18: \Psr\Http\Client\ClientInterface $httpClient = new HttpClient($curlService); // PSR-7: \Psr\Http\Message\ResponseInterface $response = $httpClient->sendRequest($request);
Send requests in parallel
use WebServCo\Http\Client\Service\cURL\CurlMultiService; // \WebServCo\Http\Client\Contract\Service\cURL\CurlMultiServiceInterface $curlMultiService = new CurlMultiService($curlService); // Keep the handle identifier for each request, to find its response later. $handleIdentifiers = []; foreach ($requests as $requestId => $request) { $handleIdentifiers[$requestId] = $curlMultiService->createHandle($request); } $curlMultiService->executeSessions(); foreach ($handleIdentifiers as $requestId => $handleIdentifier) { $response = $curlMultiService->getResponse($handleIdentifier); } // Or, when you don't need to match them: foreach ($curlMultiService->iterateResponse() as $response) {} // Clean up before reusing the service. $curlMultiService->reset();
Contracts
| Interface | Implementation | Purpose |
|---|---|---|
\Psr\Http\Client\ClientInterface |
Service\PSR18\HttpClient |
Sends one request. |
Contract\Service\cURL\CurlMultiServiceInterface |
Service\cURL\CurlMultiService |
Sends many requests at once with curl_multi. |
Contract\Service\cURL\CurlServiceInterface |
Service\cURL\CurlService |
Creates cURL handles and turns results into PSR-7 responses. |
| none | DataTransfer\CurlServiceConfiguration |
Debug mode and timeout. |
Namespaces are relative to WebServCo\Http\Client\.
Notes
The timeout applies to both connecting and the whole transfer. With debug mode on, every request and response is logged through the logger factory. Turn it off in production unless you're tracking down a problem.
The parallel client takes PSR-7 requests and returns PSR-7 responses, but its own interface isn't part of PSR-18.
Development
# Start project. ddev start # Install dependencies. ddev c u # Run all validations. ddev c c # Run unit tests. ddev c test:dox