PHP code example of bentools / typed-http-client

1. Go to this page and download the library: Download bentools/typed-http-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bentools / typed-http-client example snippets


$client = new TypedHttpClient(new TodoFactory());

$todo = $client->request('GET', 'https://jsonplaceholder.typicode.com/todos/1')->getData();
// $todo is a Todo object — and PHPStan knows it, no annotation 

use BenTools\TypedHttpClient\RequestContext;
use BenTools\TypedHttpClient\TypedHttpClient;
use Symfony\Contracts\HttpClient\ResponseInterface;

$client = new TypedHttpClient(
    fn (ResponseInterface $response, bool $throw, RequestContext $context) => new Todo(...$response->toArray($throw)),
);

$todo = $client->request('GET', 'https://jsonplaceholder.typicode.com/todos/1')->getData();

use BenTools\TypedHttpClient\DataFactoryInterface;
use BenTools\TypedHttpClient\RequestContext;
use Symfony\Contracts\HttpClient\ResponseInterface;

/**
 * @implements DataFactoryInterface<Todo>
 */
final readonly class TodoFactory implements DataFactoryInterface
{
    public function __invoke(ResponseInterface $response, bool $throw, RequestContext $context): Todo
    {
        $data = $response->toArray($throw);

        return new Todo($data['userId'], $data['id'], $data['title'], $data['completed']);
    }
}

$client = new TypedHttpClient(new TodoFactory());
// PHPStan infers TypedHttpClient<Todo> from the factory — no @var annotation needed.

/**
 * @implements DataFactoryInterface<Todo|list<Todo>>
 */
final readonly class TodoEndpointFactory implements DataFactoryInterface
{
    public function __invoke(ResponseInterface $response, bool $throw, RequestContext $context): array|Todo
    {
        $data = $response->toArray($throw);

        return match (str_ends_with($context->url, '/todos')) {
            true => array_map(fn (array $item) => new Todo(...$item), $data),   // collection endpoint
            false => new Todo(...$data),                                        // single-item endpoint
        };
    }
}

$baseClient = new TypedHttpClient(innerClient: HttpClient::createForBaseUri('https://api.example.com'));

$todoClient = $baseClient->withFactory(new TodoFactory()); // TypedHttpClient<Todo>
$userClient = $baseClient->withFactory(new UserFactory()); // TypedHttpClient<User>

$authenticatedClient = $todoClient->withOptions(['auth_bearer' => $token]);

$responses = [
    $client->request('GET', 'https://jsonplaceholder.typicode.com/todos/1'),
    $client->request('GET', 'https://jsonplaceholder.typicode.com/todos/2'),
];

foreach ($client->stream($responses) as $response => $chunk) {
    if ($chunk->isLast()) {
        $todo = $response->getData(); // Todo object
    }
}

$client = new TypedHttpClient();
$data = $client->request('GET', 'https://jsonplaceholder.typicode.com/todos/1')->getData(); // plain array