Questions tagged [psr-7]

PSR-7 is the FIG standard for HTTP messages in PHP. It provides abstractions of HTTP messages and their components and defines a consistent set of interfaces to work with them.

The purpose of PSR-7 is to provide PHP with a set of common interfaces for HTTP messages as described in RFC 7230 and RFC 7231, and URIs as described in RFC 3986 (in the context of HTTP messages).

All HTTP messages consist of the HTTP protocol version being used, headers, and a message body. A Request builds on the message to include the HTTP method used to make the request, and the URI to which the request is made. A Response includes the HTTP status code and reason phrase.

In PHP, HTTP messages are used in two contexts:

  • To send an HTTP request, via the ext/curl extension, PHP's native stream layer, etc., and process the received HTTP response. In other words, HTTP messages are used when using PHP as an HTTP client.
  • To process an incoming HTTP request to the server, and return an HTTP response to the client making the request. PHP can use HTTP messages when used as a server-side application to fulfil HTTP requests.
94 questions
5
votes
2 answers

Emit a response with PSR-7

I'm trying to understand how PSR-7 works and I get stuck! Here is my code: $app->get('/', function () { $stream = new Stream('php://memory', 'rw'); $stream->write('Foo'); $response = (new Response()) ->withHeader('Content-Type',…
Seb Bizeul
  • 968
  • 1
  • 10
  • 18
5
votes
2 answers

Slim 3 - How to add 404 Template?

In Slim 2, I can over write the default 404 page easily, // @ref: http://help.slimframework.com/discussions/problems/4400-templatespath-doesnt-change $app->notFound(function () use ($app) { $view = $app->view(); …
Run
  • 54,938
  • 169
  • 450
  • 748
4
votes
2 answers

Call to undefined method GuzzleHttp\Psr7\Response::isSuccessful()

So I have installed the Guzzle library version 6 according to TeamUp calendar documentation. However, when I try to run the code below I get Fatal error: Call to undefined method GuzzleHttp\Psr7\Response::isSuccessful() code:
bmm
  • 207
  • 1
  • 5
  • 18
4
votes
1 answer

Sf2 RedirectResponse not working

I am using the HttpFoundation component in my project without using the full Symfony2 framework. I try to make a RedirectResponse if some credentials are true and redirect the user (like stated in the documentation), but the return statement is not…
Ivan
  • 5,139
  • 11
  • 53
  • 86
4
votes
2 answers

Where to put new objects generated by middleware?

PSR-7 is going to be standardized soon (I believe). That's got me thinking about middlewares, such as used by Phly, StackPHP, and ConnectJS. The way ConnectJS works is that it modifies the request object when a middleware needs to add something. For…
mpen
  • 272,448
  • 266
  • 850
  • 1,236
3
votes
0 answers

How PSR-7 Request works?

I'm confused about how PSR-7 based request works. As PSR-7, a request must implement Psr\Http\Message\RequestInterface. By this interface a request must have a body and it must return body as an object that implements StreamInterface. When I look…
Teoman Tıngır
  • 2,766
  • 2
  • 21
  • 41
3
votes
4 answers

How to do a redirect from a Slim 4 middleware?

I've been testing the new Slim 4 framework and redirects work fine for me in normal classes, but I cannot seem to get them working in middleware, where a response is dynamically generated (apparently?) by the Request Handler. When I try to redirect…
ConleeC
  • 337
  • 6
  • 13
3
votes
0 answers

How to detect MIME-type from PSR-7 UploadedFileInterface

I have a simple controller that handles image uploads. Before saving the image/file to its final destination i would like to validate the mimeType of that file. My controller uses Psr\Http\Message\ServerRequestInterface and calls getUploadedFiles()…
A.L.
  • 129
  • 1
  • 7
3
votes
0 answers

Writing PSR-7 streams to file

I'm building a read-through image cache and I'm looking for a way to write PSR-7 streams to files on the server. The documentation isn't wonderfully clear on how to do this (it mostly focuses on serving files, rather than writing them). My current…
thelastshadow
  • 3,406
  • 3
  • 33
  • 36
3
votes
2 answers

How to install PSR-7 http-message interfaces with Composer

Is there maybe a way to install the PSR-7 HTTP message interfaces using Composer? I can't seem to find any information on the official github page, nor on the web. Beeing a Composer-beginner, I would also like to ask, if it is possible to install…
user7941334
3
votes
2 answers

Using GuzzleHttp/Psr7/Response Correctly

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() { …
Martin T.
  • 113
  • 2
  • 8
3
votes
2 answers

new (Json)Response shows blank page (Symfony HttpFoundation)

I am using the HttpFoundation in my small project: use \Symfony\Component\HttpFoundation\JsonResponse as JsonResponse; Unfortunately all my responses (tried JsonResponse, Response and BinaryFileResponse) only return a blank page, no errors and the…
PrimuS
  • 2,505
  • 6
  • 33
  • 66
3
votes
1 answer

PHP - why HTTP message implementations?

This may be a daft question but I have been seeing and reading HTTP messages or PSR-7 in the current trend on PHP for development. Why out of a sudden we need HTTP message implementations in PHP for the modern web development? For instance, with…
Run
  • 54,938
  • 169
  • 450
  • 748
2
votes
0 answers

Mutation of the received response (Without violating PSR)

I am making an SDK client application that will work with a third-party API, and during the development process I try to adhere to PSR standards. I have two questions that I don't fully understand, and I hope to get an answer. Composer depends: { …
wnull
  • 217
  • 6
  • 21
2
votes
1 answer

Why is a PSR-7 Response’s Body mutable?

Since a PSR-7 Response is supposed to be immutable, why can I write this disturbingly "mutating" piece of code? public function controller(Response $response): Response { $response->getBody()->write("Hey."); return $response; } It seems to…
sylbru
  • 1,461
  • 1
  • 11
  • 19