Wednesday, October 5, 2016

From HTTP Messages to PSR-7: What’s It All About?

The PHP Framework Interoperability Group (PHP-FIG) has relatively recently approved another proposal, the PSR-7: HTTP Messages Interface. The document crystallizes HTTP messages into 7 interfaces which a PHP library should implement if they subscribe to the specification. In [PSR-7 By Example] (http://ift.tt/15E6n2t), Matthew Weier O'Phinney, editor of the PSR, gives an interesting overview of the specification. So what is it?

Image of digital question mark, indicating confusion and pending explanation

If you type bbc.co.uk in your browser, you go straight to the homepage of the BBC, but a number of things might have taken place between the time the browser sent an HTTP request to the server and it getting a response back.

Here's a sample raw request.

GET / HTTP/1.1
Host: bbc.co.uk
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Accept: */*
Referer:

It's always made up of a request line (GET / HTTP/1.1), a number of header field lines listed as <key>: value, a single empty line and an optional body for any data payload (for example query parameters or post data).

The blank line after the zero or more header lines must be a CRLF by itself. This means 2 characters - an ASCII 13 (Carriage Return), followed by ASCII 10 (Line Feed) or \r\n.

Let's send this request from the command line via curl and see the response:

curl -i -H "Host: bbc.co.uk" -H "User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)" -H "Accept: */*" -X GET http://bbc.co.uk

HTTP/1.1 301 Moved Permanently
Content-Type: text/html
Date: Sun, 02 Oct 2016 20:49:42 GMT
Location: http://www.bbc.co.uk/
Connection: Keep-Alive
Content-Length: 0

Moved? There was a redirect. Then, let's follow the trail and make a request to http://www.bbc.co.uk/ instead:

Continue reading %From HTTP Messages to PSR-7: What’s It All About?%


by Deji Akala via SitePoint

No comments:

Post a Comment