Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send a HTTP/2 POST request in PHP

I found a similar question at Sending HTTP/2 POST request in Ruby But I want to update my server with PHP

The new Apple push notification HTTP/2 based API described here: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/APNsProviderAPI.html

Anyone with HTTP/2 experience help me with making a request as a client in PHP.

like image 776
mthuong Avatar asked Jan 05 '16 08:01

mthuong


2 Answers

The CURL extension for PHP >= 5.5.24 has support for HTTP/2. (since this commit)

You also need a libcurl installed — the underlying library that the curl functions use — with HTTP/2 support enabled. That means a libcurl newer than 7.38.0 but really, the newer the better. Libcurl has to have been built with HTTP/2 support explicitly enabled, using the --with-nghttp2 flag at compile time.

Just use curl as you'd normally use it, and set the CURLOPT_HTTP_VERSION option to use HTTP/2 by passing in CURL_HTTP_VERSION_2_0. Then you'll get the request upgraded to version 2 if the client and server both support it.

Prior to PHP 5.5.24, if libcurl has been built with HTTP/2 support, you can pass in the int value of CURL_HTTP_VERSION_2_0 explicitly as PHP will still pass it through to libcurl. Currently, it has a value of 3 — this should not change, but could.

if (!defined('CURL_HTTP_VERSION_2_0')) {
    define('CURL_HTTP_VERSION_2_0', 3);
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
like image 107
Daniel Stenberg Avatar answered Sep 29 '22 06:09

Daniel Stenberg


Having PHP >= 5.5.24 is not enough to make a HTTP/2 request with curl, even if CURL_HTTP_VERSION_2_0 is defined. You will get an error message like the following if you try to make a request to APNS (Apple Push Notification Service):

?@@?HTTP/2 client preface string missing or corrupt. Hex dump for received bytes: 504f5354202f332f6465766963652f616538666562613534

Since curl is a binding for libcurl, you must also have curl with http/2 enabled.

For a sample code, see my answer to a similar question here on SO

For install procedure, you can follow this tutorial

like image 45
tiempor3al Avatar answered Sep 29 '22 07:09

tiempor3al