PHP code example of iammordaty / guzzle-json-response-middleware

1. Go to this page and download the library: Download iammordaty/guzzle-json-response-middleware 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/ */

    

iammordaty / guzzle-json-response-middleware example snippets


use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleJsonResponseMiddleware\JsonResponseMiddleware;

$stack = HandlerStack::create();
$stack->push(new JsonResponseMiddleware(), JsonResponseMiddleware::NAME);

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

$response = $client->get('http://www.mocky.io/v2/5db0b9312f00002901c13d8e');

// please note that the `getJson` method, excluding for the first one, accepts the same arguments
// as native PHP's `json_decode` function
$contents = $response->getBody()->getJson(true);

print_r($contents);

/*
Outputs:

Array
(
    [id] => 78349
    [name] => John Smith
    [username] => @smith
    [email] => [email protected]
    [phone] => +1-202-555-0192
    [website] => smith.dev
)
*/