且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在PHP中发送HTTP / 2 POST请求

更新时间:2022-11-27 21:47:23

PHP> = 5.5.24的CURL扩展支持HTTP / 2。 (因为此提交



您还需要安装libcurl - curl函数使用的底层库,启用HTTP / 2支持。这意味着libcurl比7.38.0更新,但真的,新的更好。 Libcurl必须使用显式启用的HTTP / 2支持,在编译时使用 - with-nghttp2 标志。



只需使用curl,就像通常使用curl一样,并设置 CURLOPT_HTTP_VERSION 选项,通过传递 CURL_HTTP_VERSION_2_0 使用HTTP / 2。



在PHP 5.5.24之前的版本中,如果客户端和服务器都支持该请求, libcurl已经建立了HTTP / 2支持,你可以显式传递 CURL_HTTP_VERSION_2_0 的int值,因为PHP仍然会将它传递给libcurl。目前,它的值为 3 - 这不应更改,但可以



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);


I found a similar question at How do I send a 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.

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);