weblabel / http-client
HTTP client wrapper
Installs: 1 270
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.4
- ext-json: *
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.3
- nyholm/psr7: ^1.4.1
- phpstan/phpstan: ^0.12.99
- phpunit/phpunit: ^9.0
- symfony/http-client: ^5.4
README
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); } }