osirisgate/http-client

Provides powerful and flexible methods to fetch HTTP resources synchronously or asynchronously.

1.0.0 2025-05-14 19:05 UTC

This package is not auto-updated.

Last update: 2025-05-14 19:07:09 UTC


README

License: MIT PHP Version

Lightweight, elegant HTTP client built with modern PHP. Provides an expressive and extensible wrapper to perform REST API calls using clean, typed interfaces. Aligned with Osirisgate Core standards.

๐Ÿ“ฆ Installation

composer require osirisgate/http-client

โœ… Requirements

โœจ Features

  • Simple and expressive methods for HTTP verbs: GET, POST, PUT, PATCH, DELETE
  • Structured exception system implementing ExceptionInterface
  • Response abstraction through ResponseInterface
  • Optional support for:
    • Query parameters (filters)
    • Custom headers
    • Low-level request options

๐Ÿง‘โ€๐Ÿ’ป How to use it ?

โœ… Send a GET request

use Osirisgate\Component\HttpClient\Request\Http;

$response = Http::get('https://jsonplaceholder.typicode.com/posts');
$data = $response->toArray();

๐Ÿ“Œ With query parameters

$response = Http::get(
    'https://jsonplaceholder.typicode.com/comments',
    filters: ['postId' => 1]
);

$comments = $response->toArray();

โœ๏ธ Create a resource (POST)

$response = Http::post(
    'https://jsonplaceholder.typicode.com/posts',
    payload: [
        'title' => 'foo',
        'body' => 'bar',
        'userId' => 1
    ]
);

๐Ÿ” Update a resource (PUT / PATCH)

// PUT (Full update)
$response = Http::put('https://jsonplaceholder.typicode.com/posts/1', payload: [
    'id' => 1,
    'title' => 'new title',
    'body' => 'updated content',
    'userId' => 1
]);

// PATCH (Partial update)
$response = Http::patch('https://jsonplaceholder.typicode.com/posts/1', payload: [
    'title' => 'patched title'
]);

โŒ Delete a resource

$response = Http::delete('https://jsonplaceholder.typicode.com/posts/1');

โš™๏ธ Advanced Configuration

๐Ÿงพ Custom headers

$response = Http::get(
    'https://api.example.com/data',
    headers: [
        'Authorization' => 'Bearer TOKEN',
        'X-Custom-Header' => 'value'
    ]
);

๐Ÿ›  Request options

$response = Http::get(
    'https://api.example.com/data',
    options: [
        'timeout' => 5,
        'verify_peer' => false
    ]
);

๐Ÿšจ Error Handling

Any HTTP client or server error will throw a typed exception implementing ExceptionInterface.

use Osirisgate\Core\Exception\ExceptionInterface;
use Osirisgate\Component\HttpClient\Request\Http;

try {
    $response = Http::get('https://api.example.com/invalid');
} catch (ExceptionInterface $e) {
    echo $e->getMessage(); // Not Found
    echo $e->getCode();    // 404
    print_r($e->format());
}

๐Ÿ“„ Response Interface

Each request returns a structured object implementing ResponseInterface.

Method Description
getStatusCode() Returns the HTTP status code
getHeaders() Returns an array of headers
getContent() Returns raw response body (string)
toArray() Returns JSON-decoded response
cancel() Closes the response stream and all related buffers.
getInfo($key) Returns meta information (e.g. url)

๐Ÿงช Run Tests

A full suite of unit tests is included and runs against a public API (jsonplaceholder.typicode.com).

vendor/bin/phpunit tests

๐Ÿงฐ Developer Tools

โœ… Code style

composer run phpcs-fix

๐Ÿง  Static analysis

composer run phpstan

Or run both checks at once:

make check-code-quality

๐Ÿ“œ License

This package is licensed under the MIT License.

๐Ÿ‘ค Author

Ulrich Geraud AHOGLA Software Engineer โ€” Osirisgate

If you have any questions or suggestions, please contact me via developer@osirisgate.com