PHP code example of innovatoranant / wirehttp

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

    

innovatoranant / wirehttp example snippets


use WireHttp\Wire;

$response = Wire::post('https://api.example.com/billing')
    ->withBearer('secret_token')
    ->withHeader('X-Client', 'MobileApp')
    ->withJson(['plan' => 'enterprise', 'users' => 500])
    ->timeout(5.0) // Request-specific timeout override
    ->send();

if ($response->isClientError()) {
    throw new Exception($response->json()['message']);
}

use WireHttp\Wire;
use WireHttp\Async\Future;

// Dispatch 3 non-blocking network requests
$users   = Wire::get('https://api.example.com/users')->sendAsync();
$stats   = Wire::get('https://api.example.com/stats')->sendAsync();
$reports = Wire::get('https://api.example.com/reports')->sendAsync();

// Block the current Fiber until ALL requests settle
[$resUsers, $resStats, $resReports] = Future::all(...[$users, $stats, $reports]);

echo $resUsers->status(); // 200

use WireHttp\Response\Hydrator\Attributes\JsonProperty;

class ProfileDto {
    public int $id;
    
    // Map mismatched JSON keys seamlessly
    #[JsonProperty('avatar_url')]
    public string $avatarUrl;
}

// Automatically decodes JSON, validates types, and returns the DTO!
$profile = Wire::get('https://api.example.com/me')->send()->into(ProfileDto::class);

echo $profile->avatarUrl;

use WireHttp\Wire;
use WireHttp\Configuration\ClientConfig;
use WireHttp\Middleware\Core\CookieJar;

$client = Wire::with(
    ClientConfig::create()
        ->withFollowRedirects(true, maxRedirects: 5)
        ->withCookies(true, new CookieJar())
);

// 1. Logs in and receives a Set-Cookie header
$client->post('/login')->withForm(['user' => 'admin'])->send();

// 2. Automatically injects the Cookie into subsequent requests!
$dashboard = $client->get('/dashboard')->send()->body();

use WireHttp\Wire;
use WireHttp\Transport\Mock\MockResponseQueue;
use WireHttp\Http\Response;
use WireHttp\Http\Stream;

$queue = new MockResponseQueue();
$queue->push(new Response(201, [], Stream::fromString('{"status": "created"}')));

// Hijack the transport layer globally
Wire::fake($queue);

// This executes instantly and returns the mock response!
$response = Wire::post('https://api.example.com/charge')->send();

assert($response->json()['status'] === 'created');