hugphp/http

A delightful HTTP client with a human-readable API.

v1.0.0 2025-02-27 01:15 UTC

This package is auto-updated.

Last update: 2025-04-27 01:54:35 UTC


README

hugphp/http

GitHub Workflow Status (main) Total Downloads Latest Version License

HugPHP HTTP Client

A delightful HTTP client for PHP with a human-readable API, designed to make HTTP requests simple, lovable, and powerful.

Requires PHP 8.3+ and ext-curl

Installation

Add HugPHP HTTP to your project using Composer:

composer require hugphp/http

Features

  • Fluent, chainable API for intuitive request building.
  • Built-in JSON support for easy data sending and parsing.
  • Optional SSL verification toggle for flexibility.
  • Lightweight and dependency-free (uses native cURL).

1. Example Usage

use HugPHP\Http\Client;

$client = new Client();

// Simple GET request (SSL verification enabled by default)
$response = $client->to('https://api.example.com/data')->get();
echo $response->body();

// POST with JSON, disabling SSL verification
$response = $client->to('https://api.example.com/post')
                   ->withHeader('Authorization', 'Bearer token')
                   ->sendJson(['name' => 'HugPHP'])
                   ->withOutSSLCertificate() // Disables SSL verification
                   ->post();
print_r($response->json());

// Fetch JSON directly
$data = $client->to('https://api.example.com/users/1')
               ->withOutSSLCertificate()
               ->get()
               ->json();
echo $data['name'];

2. Example Usage

use HugPHP\Http\Client;

$client = new Client();

// GET with rate limiting
$client->to('https://api.example.com/data')
       ->withRateLimit(5, 'minute')
       ->get();

// POST with JSON and debugging
$client->to('https://api.example.com/post')
       ->withHeader('Authorization', 'Bearer token')
       ->sendJson(['name' => 'HugPHP'])
       ->debug()
       ->post();

// Validate and transform response
class User {
    public int $id;
    public string $name;
}
$user = $client->to('https://api.example.com/user')
               ->withOutSSLCertificate()
               ->validateSchema('user-schema.json', User::class);
echo $user->name;

// Mock for testing
$client->mock('https://example.com', ['status' => 200, 'body' => '{"fake": true}']);
$data = $client->to('https://example.com')->get()->json();

Development

๐Ÿงน Keep a modern codebase with Pint:

composer lint

โœ… Run refactors using Rector

composer refacto

โš—๏ธ Run static analysis using PHPStan:

composer test:types

โœ… Run unit tests using PEST

composer test:unit

๐Ÿš€ Run the entire test suite:

composer test

HugPHP HTTP was created by Micheal Ataklt under the MIT license.