PHP code example of avto-dev / guzzle-url-mock

1. Go to this page and download the library: Download avto-dev/guzzle-url-mock 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/ */

    

avto-dev / guzzle-url-mock example snippets




use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use AvtoDev\GuzzleUrlMock\UrlsMockHandler;

$handler = new UrlsMockHandler;
$client  = new Client([
    'handler' => HandlerStack::create($handler),
]);

$handler->onUriRequested('https://goo.gl', 'get', new Response(
    200, ['foo' => ['bar']], '<h1>All looks fine!</h1>'
));

$handler->onUriRegexpRequested('~https:\/\/goo\.gl\/.*~', 'post', new Response(
    404, [], 'Nothing found'
));

$client->request('get', 'https://goo.gl')->getBody()->getContents(); // '<h1>All looks fine!</h1>'
$client->request('post', 'https://goo.gl/foo', ['http_errors' => false])->getBody()->getContents(); // 'Nothing found'