1. Go to this page and download the library: Download webservco/http-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// \WebServCo\Http\Client\Contract\Service\cURL\CurlMultiServiceInterface
$curlMultiService = new CurlMultiService(
// \WebServCo\Http\Client\Contract\Service\cURL\CurlServiceInterface
$curlService
);
/** @var array<int,\Psr\Http\Message\RequestInterface> $requests */
$requests = [
// PSR-7: \Psr\Http\Message\RequestInterface
1 => $request1,
// PSR-7: \Psr\Http\Message\RequestInterface
2 => $request2,
// etc
];
// Keep a list of handles to be able to link them to each id. key: id, value: handle identifier.
/** @var array<int,string> $curlHandleIdentifiers */
$curlHandleIdentifiers = [];
foreach ($requests as $request) {
// Create handle and add it's identifier to the list.
$handleIdentifier = $curlMultiService->createHandle($request);
$curlHandleIdentifiers[$releaseId] = $handleIdentifier;
}
// Add more requests if needed
// Create handle and add it's identifier to the list.
$handleIdentifier = $curlMultiService->createHandle($request);
$curlHandleIdentifiers[$releaseId] = $handleIdentifier;
// When ready, execute all requests in parallel
$curlMultiService->executeSessions();
// Retrieve responses
// Use case: consumer keeps track of requests added and needs to be able to identify corresponding responses.
foreach ($curlHandleIdentifiers as $releaseId => $handleIdentifier) {
$response = $curlMultiService->getResponse($handleIdentifier);
// Do something with the response
}
// Use case: consumer just needs all responses, no need to link them to the requests.
foreach ($curlMultiService->iterateResponse() as $response) {
// Do something with the response
}
// Cleanup. After this the service can be re-used, going through all the steps.
$curlMultiService->cleanup();
// \WebServCo\Http\Client\Contract\Service\cURL\CurlServiceInterface
$curlService = new CurlService(
// \WebServCo\Http\Client\DataTransfer\CurlServiceConfiguration
new CurlServiceConfiguration(
// enableDebugMode
true
),
// \WebServCo\Log\Contract\LoggerFactoryInterface
new ContextFileLoggerFactory(sprintf('%svar/log', $projectPath)),
// PSR-17: \Psr\Http\Message\ResponseFactoryInterface
$responseFactory,
// PSR-17: \Psr\Http\Message\StreamFactoryInterface
$streamFactory,
);
declare(strict_types=1);
namespace Project\Http\Client\Factory;
use WebServCo\Http\Client\Contract\Service\cURL\CurlServiceInterface;
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\Contract\LoggerFactoryInterface;
final class CurlServiceFactory
{
public function __construct(private LoggerFactoryInterface $loggerFactory)
{
}
public function createCurlService(int $timeout): CurlServiceInterface
{
$streamFactory = new StreamFactory();
return new CurlService(
new CurlServiceConfiguration(true, $timeout),
$this->loggerFactory,
new ResponseFactory(new StatusCodeService(), $streamFactory),
$streamFactory,
);
}
}
$loggerFactory = new ContextFileLoggerFactory(sprintf('%svar/log', $projectPath));
new CurlServiceFactory($loggerFactory);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.