Skip to content

borschphp/borsch-httpclient

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Borsch - PSR-18 HTTP Client

A minimalist PSR-18 implementation for making HTTP requests in PHP.

Installation

The package can be installed via Composer.
Run the following command:

composer require borschphp/http-client

Usage

Here'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();

Middlewares

Before

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);
    })

After

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());
    });

About

A minimal PSR-18 HTTP Client.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages