PHP code example of mtk3d / http-vcr

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

    

mtk3d / http-vcr example snippets


$vcr = new VcrClient($realClient, cassette: 'shopify/get-product');

$response = $vcr->sendRequest($request);
// first run:  the real request happens, and is recorded to
//             tests/Cassettes/shopify/get-product.yaml
// every run after: no network call — the recorded response is replayed

use HttpVcr\Bridge\PHPUnit\InteractsWithCassettes;
use HttpVcr\Bridge\PHPUnit\UseCassette;

final class ShopifyClientTest extends TestCase
{
    use InteractsWithCassettes;

    #[UseCassette('shopify/get-product')]
    public function testGetProduct(): void
    {
        $shopify = new ShopifyClient($this->vcrClient(), new GuzzleHttp\Psr7\HttpFactory());

        $product = $shopify->getProduct('123');

        $this->assertSame('T-Shirt', $product['title']);
    }
}

#[UseCassette(
    'shopify/checkout',
    mode: RecordMode::ExtendCassette,
    strictMode: StrictMode::InOrder,
    staleAfter: new DateInterval('P7D'),
    

use HttpVcr\VcrClient;

$vcr = new VcrClient(
    inner: new GuzzleHttp\Client(),
    cassette: 'shopify/get-product',
);

new VcrClient($inner, cassette: 'shopify/get-product', matchers: [
    new MethodMatcher(),
    new UriMatcher(),
    new QueryStringMatcher(),
    new HeadersMatcher(['X-Shop-Domain']),
]);

(new BodyJsonMatcher())
    ->ignoreJsonField('/transactionId')                  // any value counts as equal
    ->matchJsonField('/requestId', '/^[0-9a-f-]{36}$/'); // must look like a UUID

(new QueryStringMatcher)
    ->ignoreQueryParam('timestamp')                      // a nonce, a cache-buster, a signature
    ->matchOnlyQueryParams(['id', 'version']);           // or name the few that decide the match

interface RequestMatcherInterface {
    public function matches(RecordedRequest $recorded, RecordedRequest $incoming): bool;
}

$vcr->redact('<SHOPIFY_API_KEY>', fn () => $_ENV['SHOPIFY_API_KEY']);

$vcr->redactHeader('X-Api-Key');
$vcr->redactJsonField('/customer/email');
$vcr->redactQueryParam('api_key');       // ?api_key=xxx in the URL itself
$vcr->redactFormField('client_secret');  // form-encoded body



use HttpVcr\Config;
use HttpVcr\Provider;

return Config::create(
    cassetteDirectory: __DIR__ . '/tests/Cassettes',
    providers: [
        'shopify' => new Provider(hosts: ['*.myshopify.com'], 

VcrClient::configure(cassetteDirectory: __DIR__ . '/Cassettes');