PHP code example of hugphp / http

1. Go to this page and download the library: Download hugphp/http 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/ */

    

hugphp / http example snippets


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'];

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