Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using GuzzleHttp/Psr7/Response Correctly

Tags:

php

guzzle

psr-7

Not sure what is the correct way to display in a php page a Psr7 Guzzle Response.

Right now, I am doing:

use GuzzleHttp\Psr7\BufferStream;
use GuzzleHttp\Psr7\Response;

class Main extends \pla\igg\Main
{
    function __construct()
    {

        $stream = new BufferStream();
        $stream->write("Hello I am a buffer");

        $response = new Response();
        $response = $response->withBody($stream);
        $response = $response->withStatus('201');
        $response = $response->withHeader("Content-type", "text/plain");
        $response = $response->withAddedHeader("IGG", "0.4.0");

        //Outputing the response
        http_response_code($response->getStatusCode());

        foreach ($response->getHeaders() as $strName => $arrValue)
        {
            foreach ($arrValue as $strValue)
            {
                header("{$strName}:{$strValue}");
            }
        }


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

    }
}

Is there a more OOP way to display the response?

like image 456
Martin T. Avatar asked Jun 16 '26 04:06

Martin T.


1 Answers

A more OOP way of doing the same thing is to create a Sender object that requires a ResponseInterface in its constructor. This class would be responsible for setting headers, clearing buffers and render the response:

use Psr\Http\Message\ResponseInterface;

class Sender
{
    protected $response;

    public function __construct(ResponseInterface $response)
    {
        $this->response = $response;
    }

    public function send(): void
    {
        $this->sendHeaders();
        $this->sendContent();
        $this->clearBuffers();
    }

    protected function sendHeaders(): void
    {
        $response = $this->response;
        $headers  = $response->getHeaders();
        $version  = $response->getProtocolVersion();
        $status   = $response->getStatusCode();
        $reason   = $response->getReasonPhrase();

        $httpString = sprintf('HTTP/%s %s %s', $version, $status, $reason);

        // custom headers
        foreach ($headers as $key => $values) {
            foreach ($values as $value) {
                header($key.': '.$value, false);
            }
        }

        // status
        header($httpString, true, $status);
    }

    protected function sendContent()
    {
        echo (string) $this->response->getBody();
    }

    protected function clearBuffers()
    {
        if (function_exists('fastcgi_finish_request')) {
            fastcgi_finish_request();
        } elseif (PHP_SAPI !== 'cli') {
            $this->closeOutputBuffers();
        }
    }

    private function closeOutputBuffers()
    {
        if (ob_get_level()) {
            ob_end_flush();
        }
    }
}

Use it like this:

$sender = new Sender($response);
$sender->send();

Better yet, you could inject the Sender into your app object and transform it in a class variable, so you'd call it like this:

function renderAllMyCoolStuff()
{
    $this->sender->send();
}

I'll leave it as a reader's exercise to implement getters and setters for the Response object, plus a method to receive some content string and transform it into a Response object internally.

like image 195
Rafael Beckel Avatar answered Jun 17 '26 17:06

Rafael Beckel