Benefiting from HTTP2 Server Push and other HTTP 2 Performance Advantages in PHP – HTTP2 Server Push PHP Class package blog

Author: Mohamed Elbahja
Posted on: 2017-10-25
Package: HTTP2 Server Push PHP Class

HTTP 2 is the newer HTTP protocol version that provides several types of advantages in terms of performance.

Read this article to learn how to take advantage of the HTTP2 Server Push feature to improve the performance of PHP sites.

Contents

HTTP2 Performance Advantages

HTTP2 Requirements

How to Install the HTTP2 Server Push PHP Class

How to Use the HTTP2 Server Push PHP Class

Contributing and Support

HTTP2 Performance Advantages

HTTP/2 is a newer version of the HTTP protocol that provides several enhancements for achieving better data access performance like for example using only one TCP connection, as well the server push feature to push all files needed by a page before they are requested like CSS, JavaScript, images etc..

The server push features works by sending special headers to the browser to let it know what are the resources the page needs to be loaded and rendered. Then the browser will load those CSS, JavaScript and Image files in parallel while the current page is still being loaded.

In simple terms, since the HTTP/2 server push can make the server return all the page resources using a single network connection, this feature will make your Web site faster.

In this article I will show you how to use the HTTP2 Server Push PHP Class package to take advantage of the HTTP/2 server push feature.

HTTP2 Requirements

In order to take advantage of HTTP2 features there several requirements that must be met:

1. Server support for HTTP/2

2. Browser support for HTTP/2

3. HTTPS connections using TLS 1.2: although the specification of HTTP/2 does not require HTTPS support, most browsers require HTTPS to support HTTP/2

If any of these conditions are not met, the requests will fallback to HTTP/1.1.

How to Install the HTTP2 Server Push PHP Class

The  HTTP2 Server Push PHP Class can be installed using the Composer tool following instructions in the download page.

Alternatively you can also download the package ZIP archive from the same download page and manually include the class file in your application scripts.

How to Use the HTTP2 Server Push PHP Class

The first thing you need to do in a PHP script to use this package is to retrieve an instance of the class:
Then you can add a CSS file using the link method:
You can also add JavaScript files using the src method:
Images can be added using the img method:
You can add multiple resources using the method chaining style:
After adding all resources you want to push use the method push to make the class send Link headers to the current request response. This will signal the browser to preload all the resources for the current page in parallel, thus making the whole page load faster.

$pusher->push();

Contributing and Support

If you liked this package and would to contribute to its development, you can send your pull requests to the package repository in Github.

if you have any questions or remarks about the package post a comment here below.

Fonte: https://www.phpclasses.org/blog/package/10553/post/1-Benefiting-from-HTTP2-Server-Push-and-other-HTTP-2-Performance-Advantages-in-PHP.html

Facebook Comments
Rate this post