PHP code example of hakone / psr7-serializer

1. Go to this page and download the library: Download hakone/psr7-serializer 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/ */

    

hakone / psr7-serializer example snippets




use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use function Hakone\Psr7Serializer\serializePsr7;
use function Hakone\Psr7Serializer\unserializeResponse;

// 1. Send HTTP Request
$http_client = Psr18ClientDiscovery::find();
$response = $client->sendRequest(
    Psr17FactoryDiscovery::findRequestFactory()
        ->createRequest('GET', 'http://httpbin.org/get?foo=bar')
);

// 2. Serialize and cache HTTP response
$serialized_data = serializePsr7($serializer->serializeResponse($response));
file_put_contents(__DIR__ . '/cache.txt', $serialized_data);

// 3. Load and unserialize HTTP response
$loaded_serialized_data = file_get_contents(__DIR__ . '/cache.txt');
$unserialized_response = unserializePsr7($loaded_serialized_data);

use Nyholm\Psr7\Factory\Psr17Factory;

$psr17Factory = new Psr17Factory();
$unserialized_response = unserializePsr7($loaded_serialized_data, $psr17Factory, $psr17Factory);

use Laminas\Diactoros\{ResponseFactory, StreamFactory};

$unserialized_response = unserializePsr7($loaded_serialized_data, new ResponseFactory(), new StreamFactory());