1. Go to this page and download the library: Download strictphp/http-clients 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/ */
strictphp / http-clients example snippets
use Psr\Container\ContainerInterface;
use Psr\Http\Client\ClientInterface;
use StrictPhp\HttpClients\Clients\CacheResponse\CacheResponseClientFactory;
use StrictPhp\HttpClients\Clients\CustomizeRequest\CustomizeRequestClientFactory;
use StrictPhp\HttpClients\Clients\Event\EventClientFactory;
use StrictPhp\HttpClients\Clients\Retry\RetryClientFactory;
use StrictPhp\HttpClients\Clients\Sleep\SleepClientFactory;
use Strictphp\HttpClients\Factories\ClientsFactory;
use Strictphp\HttpClients\Iterators\FactoryToServiceIterator;
// Assuming $client is the main client like GuzzleHttp\Client
/** @var ClientInterface $client */
/** @var ContainerInterface $container */
// the order of classes is important, see image below
$clients = [
CacheResponseClientFactory::class, // used like first
RetryClientFactory::class,
SleepClientFactory::class,
EventClientFactory::class,
CustomizeRequestClientFactory::class,
// Other client factories...
];
/**
* This iterator change array<class-string<ClientFactoryContract>> to array<ClientFactoryContract>
*/
$toService = new FactoryToServiceIterator($container, $clients);
$clientFactory = new ClientsFactory($client);
$client = $clientFactory->create($toService);
// Alternatively, you can use second parameter of constructor:
$clientFactory = new ClientsFactory($client, $toService);
$client = $clientFactory->create();
use StrictPhp\HttpClients\Managers\ConfigManager;
use StrictPhp\HttpClients\Clients\Sleep;
// set up for SleepClient
$config = new Sleep\SleepConfig(1000, 2000);
/** @var ConfigManager $configManager */
$configManager->addDefault($config);
use StrictPhp\HttpClients\Managers\ConfigManager;
use StrictPhp\HttpClients\Clients\Sleep;
// set up for SleepClient
$config = new Sleep\SleepConfig(1000, 2000);
/** @var ConfigManager $configManager */
$configManager->add('strictphp.com', $config);
use Psr\Http\Message\RequestInterface;
use StrictPhp\HttpClients\Clients\CustomizeRequest\CustomizeRequestConfig;
use StrictPhp\HttpClients\Managers\ConfigManager;
/** @var ConfigManager $configManager */
$configManager->add('www.example.com', new CustomizeRequestConfig(function(RequestInterface $request): RequestInterface {
return $request->withHeader('uuid', generate_uuid());
}));
use StrictPhp\HttpClients\Clients\Mock\MockClient;
/** @var \Psr\Http\Message\RequestInterface $request */
$client = new MockClient(__DIR__ . '/dir/filename.shttp');
$response = $client->sendRequest($request);
$response instanceof \Psr\Http\Message\ResponseInterface;
namespace My;
use StrictPhp\HttpClients\Contracts\ConfigInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use StrictPhp\HttpClients\Entities\AbstractConfig;
/**
* parameters of constructor must have to filled default values
*/
class Config extends AbstractConfig
{
public function __construct(
private readonly int $optionA = 1,
private readonly int $optionB = 2,
) {
}
}
namespace My;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use StrictPhp\HttpClients\Managers\ConfigManager;
use My\Config;
class MyClient implements ClientInterface
{
public function __construct(
private ClientInterface $client,
private ConfigManager $configManager,
) {
}
public function sendRequest(RequestInterface $request): ResponseInterface
{
$host = $request->getUri()->getHost();
$config = $this->configManager->get(Config::class, $host);
$config->optionA;
$config->optionB;
// do anything
$response = $this->client->sendRequest($request)
// do anything
return $response;
}
}
namespace My;
use StrictPhp\HttpClients\Contracts\ClientFactoryContract;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use My\Config;
class ClientFactory implements ClientFactoryContract
{
public function __construct(
private ConfigManager $configManager,
) {
}
public function create(ClientInterface $client): ClientInterface
{
return new MyClient($client, $this->configManager);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.