PHP code example of weblabel / http-client
1. Go to this page and download the library: Download weblabel/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/ */
weblabel / http-client example snippets
declare(strict_types=1);
face;
use Psr\Http\Message\ResponseInterface;
use Nyholm\Psr7\Factory\Psr17Factory;
use Symfony\Component\HttpClient\Psr18Client;
use Weblabel\HttpClient\HttpClient;
use Weblabel\HttpClient\RequestFactory;
use Weblabel\HttpClient\Factory\JsonRequestFactory;
$symfonyClient = new Psr18Client();
$httpClient = new HttpClient($symfonyClient);
$psrFactory = new Psr17Factory();
$jsonRequestFactory = new JsonRequestFactory($psrFactory, $psrFactory);
$simpleClient = new SimpleClient('https://example.com', $httpClient, $jsonRequestFactory);
$statusResponse = $simpleClient->getStatus();
class SimpleClient
{
private ClientInterface $client;
private RequestFactory $requestFactory;
private string $baseUri;
public function __construct(string $baseUri, ClientInterface $client, RequestFactory $requestFactory)
{
$this->client = $client;
$this->requestFactory = $requestFactory;
$this->baseUri = $baseUri;
}
public function getStatus(): ResponseInterface
{
$request = $this->requestFactory->get($this->baseUri . '/status');
return $this->client->sendRequest($request);
}
}