PSR-7 Standard – Part 2 – Request and URI

PSR-7 Standard – Part 2 – Request and URI

This post is part of series:

  • Part 1: Overview
  • Part 2: Request and URI
  • Part 3: Response
  • Part 4: File Uploads
  • Part 5: HTTP-Client
  • Part 6: Server Request
  • Part 7: Middleware
  • Part 8: Usage in a Magento module

In the last blog post we described the history of PSR-7. The standard contains only interfaces. Today we start with the first two interfaces. The RequestInterface and the UriInterface.

What is a HTTP Request?

To start we create a little server simulation script with this content:

After the creation we start the server script with PHP’s internal server by running this in our command line:

php -S 127.0.0.1:8080 server.php

This will start a server on our local machine listening on port 8080. Now, we have a little server to test some example requests.

Example GET Request

GET Requests are really simple. We can call the URL /mypath?foo=bar&baz=zoz with this simple text snippet.

To simulate a call we can make use of the popular command line tool “curl”. If it is not installed on your machine, you should install it.

curl 'http://127.0.0.1:8080/mypath?foo=bar&baz=zoz'

If our server is running, we should see the response in the command line:

Curl command to simulate:

curl -X POST -d foo=bar -d baz=zoz 'http://127.0.0.1:8080/mypath'

Example POST Request

We can also post data. This is mostly done in HTML forms. The parameters are now part of a body. We need to tell the server the length of the data like in this snippet:

As you can see, sending requests to a HTTP server is quite simple.

Sending Requests in PHP

In PHP we have several possibilities to send data to a remote server. The simplest way is to use one of the build-in functions like fopen, file_get_contents.

Let’s create a file named client.php and this content:

The output of the server should be the same as in our previous curl example. That was easy, but the usage of this functions has one drawback. The function was build to make calls to a local filesystem. It is possible to prohibit calls to remote servers by disabling it with the php.ini directive allow_url_fopen.

Another popular way is the CURL PHP Module which adds additional PHP functions.

But what if the module is not installed?

To deal with this issues there are a lot of userland PHP clients in the wild. Most of the clients have an own abstraction of the request. That’s where the PSR-7 standards can help us in several ways.

  1. Harmonize the way how a request is built.
  2. Re-use code across applications.
  3. Dealing with the local environment (available PHP-Modules).

RequestInterface

The following code snippet shows the RequestInterface. The RequestInterface allows us to describe a RFC 7230 HTTP message.

Every library that creates an implementation of this interfaces should be used to create a HTTP request for every HTTP (1.1) server on the planet.

To find an existing implementation we can use packagist.org. There is a virtual package with the name “psr/http-message-implementation” available. Feel free to use one of the implementations. As PHP developers we do not like to reinvent the wheel. https://packagist.org/providers/psr/http-message-implementation

In our example at the end of the article we use the Guzzle Guzzle library. It is also possible to only install the PSR-7 implementation of the library (package: guzzlehttp/psr7) which implements the virtual package. If you are not familiar what a virtual package is, think of it like an interface for Composer packages. There is a good article which describes virtual packages.

UriInterface

The UriInterface describes the URI which should be called by our Request.

One important thing you should know is that the implementation of the URI is always immutable. This means that every method returns a new instance of the object instead of a reference to the existing object.

Wrong script:

Correct script:

Using Guzzle

As last part of this article we make use of the popular HTTP client library Guzzle. The library offers all the stuff HTTP provides. In our example we explicitly make use of the Guzzle PSR-7 implementation of the RequestInterface. The class we use is \GuzzleHttp\Psr7\Request.

If you like to test the script, please feel free to install Guzzle using composer in your local directory (where all the other files mentioned above are) :

composer.phar require guzzlehttp/guzzle:~6.0

The code we use is this:

The Response of the script should be the same as before. What are the advantages of this approach?

  1. We create a PSR-7 compatible Request with Guzzle.
  2. The Guzzle HTTP Client follows the standard. So the Request must not be created by the Guzzle library. Use whatever you want.

The next part of the series describes the HTTP Response.

– Creator of n98-magerun
– Fan of football club @wormatia
– Magento user since version 0.8 beta
– 3x certified Magento developer
– PHP Top 1.000 developer (yes, I’m PHP4 certified and sooooo old)
– Chief development officer at netz98

One thought on “PSR-7 Standard – Part 2 – Request and URI

Leave a Reply

%d bloggers like this: