Developing software in the Real World

Using HTTP/2 with PHP 7 on Mac

if you want to use HTTP/2 with PHP on OS X, you need a version of curl compiled with HTTP/2 support. You can then link your PHP's curl extension to this version.

The easiest way to do this is to use Homebrew:

At the time of writing, this will install PHP 7.0.10 with Curl 7.50.1:

Using Curl on the command line

If you want to use your shiny new curl from the command line, then the easiest way to do this is:

You can now do:

and you should get:

(at the time of writing!)

Using Guzzle to test HTTP/2 is working

To prove it works in PHP, use Guzzle!:

testhttp2.php:

Run this PHP code at the command line:

As we've turned on debugging, the output looks like this:

The key things to notice:

We tell the server that we want HTTP/2 (h2), but can accept HTTP/1.1. If the server doesn't support HTTP/2 it will send back HTTP/1.1

This is a good sign!

The response's status line is:

The version number is 2 and we got a 200, so all is OK!

Everything is working as intended.

One thought on “Using HTTP/2 with PHP 7 on Mac

  1. maybe this is a little bit wrong?

    " If the server doesn't support HTTP/2 it will send back HTTP/1.1”

    shouldnt it be " If the client doesn't support HTTP/2 the server will send back HTTP/1.1” ?

    Otherwise thanks for trying this out, I believe it will help many people!

Leave a Reply

Your email address will not be published. Required fields are marked *