wimski / laravel-psr-http
PSR HTTP setup for Laravel
Installs: 19 327
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ^8.0
- illuminate/support: ^9.0 || ^10.0 || ^11.0
- php-http/discovery: ^1.11
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.1 || ^2.0
Requires (Dev)
- nunomaduro/larastan: ^2.0
- nyholm/psr7: ^1.0
- orchestra/testbench: ^7.0 || ^8.0
- php-http/guzzle7-adapter: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.0
README
Laravel PSR HTTP
This package provides Laravel bindings to make PSR HTTP requests using discovery.
Install
composer require wimski/laravel-psr-http
Usage example
<?php declare(strict_types=1); namespace App\Console\Commands; use Illuminate\Console\Command; use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\StreamFactoryInterface; class TestCommand extends Command { protected $signature = 'http:test'; protected $description = 'Test the HTTP client'; public function __construct( protected ClientInterface $httpClient, protected RequestFactoryInterface $requestFactory, protected StreamFactoryInterface $streamFactory, ) { parent::__construct(); } public function handle(): void { $stream = $this->streamFactory->createStream(json_encode([ 'title' => 'foo', 'body' => 'bar', 'userId' => 1, ])); $request = $this->requestFactory->createRequest('POST', 'https://jsonplaceholder.typicode.com/posts') ->withHeader('Content-type', 'application/json; charset=UTF-8') ->withBody($stream); try { $response = $this->httpClient->sendRequest($request); dump(json_decode((string) $response->getBody(), true)); } catch (ClientExceptionInterface $exception) { dump($exception->getMessage()); } } }