PHP code example of spodnet / laravel-http-client-replay

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

    

spodnet / laravel-http-client-replay example snippets


use Illuminate\Support\Facades\Http;

// In 'auto' mode:
// 1st run: Hits Strava live, sanitizes secrets, and saves a cassette.
// Subsequent runs: Replays the response instantly from the cassette!
$response = Http::withToken($token)->get('https://www.strava.com/api/v3/athlete/activities', [
    'page' => 1,
    'per_page' => 30,
]);

$activities = $response->json();

use Spodnet\HttpClientReplay\Facades\HttpClientReplay;

// Temporarily run a block in record mode
HttpClientReplay::record(function () use ($stravaClient) {
    $stravaClient->syncActivities();
});

// Temporarily run a block in replay mode
HttpClientReplay::replay(function () use ($stravaClient) {
    $stravaClient->syncActivities();
});

// Change mode dynamically using Mode enum or string
HttpClientReplay::mode(\Spodnet\HttpClientReplay\Enums\Mode::Replay);
// or: HttpClientReplay::mode('replay');

use Spodnet\HttpClientReplay\Facades\HttpClientReplay;

it('fetches athlete activities', function () {
    HttpClientReplay::useCassette('strava/recent-activities');

    $response = Http::get('https://www.strava.com/api/v3/athlete/activities');

    expect($response->status())->toBe(200);
});

$data = HttpClientReplay::useCassette('stripe/customer-create', function () {
    return $stripeService->createCustomer(['email' => '[email protected]']);
});

use Spodnet\HttpClientReplay\Testing\InteractsWithHttpClientReplay;

uses(TestCase::class, InteractsWithHttpClientReplay::class);

it('stores customer charge cassette', function () {
    $this->useCassette('stripe/charge');

    $this->recordHttpClientReplay(function () {
        Http::post('https://api.stripe.com/v1/charges', ['amount' => 2000]);
    });

    $this->assertCassetteExists('stripe/charge');
});

'redaction' => [
    'mask' => '[REDACTED]',

    // Headers sanitized (case-insensitive)
    'headers' => [
        'Authorization', // 'Bearer secret' -> 'Bearer [REDACTED]'
        'X-Api-Key',
        'api-key',
        'X-Auth-Token',
        'Cookie',
        'Set-Cookie',
    ],

    // Sensitive URL query parameters (e.g. ?api_key=secret)
    'query_parameters' => [
        'api_key',
        'key',
        'token',
        'secret',
        'access_token',
        'refresh_token',
        'client_secret',
    ],

    // Sensitive request & response body fields (JSON and form-urlencoded)
    'body_fields' => [
        'client_secret',
        'refresh_token',
        'password',
        'token',
        'access_token',
        'secret',
        'api_key',
    ],
],

use Spodnet\HttpClientReplay\Facades\HttpClientReplay;

// Fetch and cache response for 1 hour; refreshes live when expired
$response = HttpClientReplay::remember('strava/activities', 3600, function () {
    return Http::get('https://www.strava.com/api/v3/athlete/activities');
});

// Or pass DateTimeInterface
HttpClientReplay::remember('stripe/rates', now()->addDay(), function () {
    return Http::get('https://api.stripe.com/v1/exchange_rates');
});

// Cache indefinitely
HttpClientReplay::rememberForever('github/org', function () {
    return Http::get('https://api.github.com/orgs/laravel');
});

use Illuminate\Support\Facades\Http;

// Specify TTL directly on outbound requests:
$response = Http::withOptions(['replay_ttl' => 3600])
    ->get('https://api.example.com/data');

// Or via header:
$response = Http::withHeaders(['X-Replay-TTL' => 3600])
    ->get('https://api.example.com/data');

// Apply TTL to next request or active cassette
HttpClientReplay::ttl(now()->addMinutes(30))->useCassette('rates', function () {
    Http::get('https://api.example.com/rates');
});

// Mark next cassette to never expire
HttpClientReplay::forever()->useCassette('static-geo-data');

// Extend/refresh timestamp of an existing stored cassette
HttpClientReplay::touch('strava/activities', now()->addDay());

// Only record / replay requests matching these patterns
'scopes' => [
    'https://www.strava.com/*' => ['ttl' => 3600], // 1 hour TTL for Strava
    '*.stripe.com/*' => ['ttl' => 86400],           // 24 hour TTL for Stripe
    'https://api.github.com/*',                     // falls back to driver/global TTL
],

// Always pass through untouched
'ignore' => [
    '127.0.0.1*',
    'localhost*',
],

HttpClientReplay::scope('https://www.strava.com/*', ['ttl' => 3600]);
HttpClientReplay::ignore(['localhost*']);
bash
php artisan vendor:publish --tag="http-client-replay-config"
bash
php artisan vendor:publish --tag="http-client-replay-migrations"
php artisan migrate
bash
php artisan http-client-replay:list