weblabel/http-client

HTTP client wrapper

v1.1.1 2021-12-08 22:28 UTC

This package is auto-updated.

Last update: 2024-11-09 04:22:13 UTC


README

Github CI

Installation

Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.

Open a command console, enter your project directory and execute:

$ composer require weblabel/http-client

This package uses PSR-7, PSR-17 and PSR-18 interfaces, but does not provide their implementations, so you have to install libraries that implements those.

Recommended libraries:

$ composer require nyholm/psr7
$ composer require symfony/http-client

Basic Usage

<?php

declare(strict_types=1);

require 'vendor/autoload.php';

use Psr\Http\Client\ClientInterface;
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);
    }
}