A minimalist PSR-18 implementation for making HTTP requests in PHP.
The package can be installed via Composer.
Run the following command:
composer require borschphp/http-clientHere's a simple example of how to use the Borsch HTTP Client:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Borsch\Http\Client;
use Borsch\Http\Adapter\Curl;
use Laminas\Diactoros\{RequestFactory, ResponseFactory, StreamFactory};
$adapter = new Curl(new ResponseFactory(), new StreamFactory());
$client = new Client($adapter);
$request = (new RequestFactory())->createRequest(
'GET',
'https://jsonplaceholder.typicode.com/posts/1'
);
$response = $client->sendRequest($request);
echo $response->getBody();You can add middleware that will be executed before the request is sent:
$adapter = new Curl(new ResponseFactory(), new StreamFactory());
$client = new Client($adapter);
$client
->before(function (RequestInterface $request, AdapterInterface $adapter) {
// For instance, fetch a JWT Token and add it to the request headers
$token = get_jwt_token_from_cache_or_auth_service();
return $request->withHeader('Authorization', 'Bearer ' . $token);
})You can also add middleware that will be executed after the response is received:
$client
->after(function (ResponseInterface $response, RequestInterface $request) {
// For instance, log the response status code, add a header, modify the body, etc.
error_log('Response Status Code: ' . $response->getStatusCode());
return $response->withHeader('X-Processed-Time', time());
});