PHP code example of ikwattro / guzzle-stereo

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

    

ikwattro / guzzle-stereo example snippets




use Ikwattro\GuzzleStereo\Recorder;

$recorder = new Recorder(__DIR__.'/var/records', __DIR__.'/stereo.yml');

$stack = \GuzzleHttp\HandlerStack::create();
$stack->push(\Ikwattro\GuzzleStereo\RecorderMiddleware::record($recorder));

$client = new \GuzzleHttp\Client(['handler' => $stack]);

for ($i = 0; $i < 10; $i++) {
	try {
		$client->get('https://api.github.com/events');
	} catch (RequestException $e) {
		// Do what you want
	}
}

$recorder->dump();

use Ikwattro\GuzzleStereo\Player;

$player = Player::replayFromTape('/path/to/tape.json');

$player->get('/');
// will return you the first response record present in the tape


namespace Acme\MyApp\Filter;

use Ikwattro\GuzzleStereo\Filter\FilterInterface;
use Psr\Http\Message\ResponseInterface;

class ActorFilter implements FilterInterface
{
	const FILTER_NAME = "actor_filter";
	
	protected $users;
	
	public function__construct(array $users = array())
	{
		$this->users = $users;
	}
	
	public static function getName()
	{
		return self::FILTER_NAME;
	}
	
	public function isIncluded(ResponseInterface $response)
	{
		$body = json_decode((string) $response->getBody());
		foreach ($body as $event) {
			$actor = $event['actor']['login'];
			if (in_array($actor, $this->users)) {
				return true;
			}
		}
		
		return false;
	}
}
yaml
marker_header: true