iammordaty / guzzle-urlencoded-response-middleware

简单的Guzzle 6.x URL编码响应中间件

0.1 2019-10-24 15:19 UTC

This package is auto-updated.

Last update: 2024-09-29 05:09:08 UTC


README

简单的Guzzle 6.x URL编码响应中间件。

安装

$ composer require iammordaty/guzzle-urlencoded-response-middleware

要求

  • PHP 7.1

示例用法

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleUrlEncodedResponseMiddleware\UrlEncodedResponseMiddleware;
use function GuzzleHttp\Psr7\stream_for;

$body = 'id=78349&name=John%20Smith&username=%40smith&email=hello%40smith.com&phone=%2B1-202-555-0192&website=smith.dev';

// IRL: fetch data from server
$mock = new MockHandler([
    (new Response())->withBody(stream_for($body))
]);

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

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

$response = $client->get('/');
$contents = $response->getBody()->getUrlDecodedParsedContents();

print_r($contents);

/*
Outputs:

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

$response->getBody()->rewind();

echo $response->getBody()->getUrlDecodedContents();

// Outputs: id=78349&name=John Smith&username=@smith&email=hello@smith.com&phone=+1-202-555-0192&website=smith.dev

许可证

iammordaty/guzzle-urlencoded-response-middleware遵循MIT许可证。