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

时间:2022-12-01 16:31:26

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

在Ruby中发送HTTP/2 POST请求时,我发现了一个类似的问题,但我想用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

新的基于Apple push notification的HTTP/2 API: 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.

任何有HTTP/2经验的人都可以帮助我以PHP客户机的身份发出请求。

4 个解决方案

#1


13  

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

PHP >= 5.5.5.24的CURL扩展支持HTTP/2。(因为这个提交)

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.

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

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.

只需像通常那样使用curl,并通过传入CURL_HTTP_VERSION_2_0设置CURLOPT_HTTP_VERSION选项来使用HTTP/2。然后,如果客户端和服务器都支持请求,您将把请求升级到版本2。

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.

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

#2


6  

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

即使已经定义了CURL_HTTP_VERSION_2_0,拥有PHP >= 5.5.24也不足以使用curl发出HTTP/2请求。如果您试图向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.

因为curl是libcurl的绑定,所以还必须启用了http/2。

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

有关示例代码,请参见我对类似问题的回答

For install procedure, you can follow this tutorial

对于安装过程,您可以遵循本教程

#3


2  

At the current moment there is no direct HTTP/2 support in PHP.

目前在PHP中没有直接的HTTP/2支持。

There is an idea to add such a support in the future direct to PHP: https://wiki.php.net/ideas/php6#http2_support

将来有一种方法可以直接将这种支持添加到PHP: https://wiki.php.net/ideas/php6#http2_support

The 3rd Party library Guzzle https://github.com/guzzle/guzzle supports HTTP/2, if the correct php and curl version are installed:

第三方库Guzzle https://github.com/guzzle/guzzle支持HTTP/2,如果安装了正确的php和curl版本:

use GuzzleHttp\Client;

$client = new Client();
$client->get('https://http2.akamai.com/demo/tile-0.png', [
    'version' => 2.0,
    'debug' => true,
]);

#4


0  

Check out the Apache and CLI PHP Docker images I built for this purpose that add HTTP/2 support to the official PHP 5.6 docker library. This gets rid of any HTTP/2 client preface string missing or corrupt errors.

查看我为此目的构建的Apache和CLI PHP Docker映像,这些映像为PHP 5.6 Docker库添加了HTTP/2支持。这将消除任何HTTP/2客户端字符串丢失或损坏错误。

Once you have the right environment, having tried several JWS/JWT libraries for PHP I only found Spomky-Labs/jose to work perfectly with APNs.

一旦您有了合适的环境,在为PHP尝试了几个JWS/JWT库之后,我只发现Spomky-Labs/jose在使用APNs时非常出色。

#1


13  

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

PHP >= 5.5.5.24的CURL扩展支持HTTP/2。(因为这个提交)

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.

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

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.

只需像通常那样使用curl,并通过传入CURL_HTTP_VERSION_2_0设置CURLOPT_HTTP_VERSION选项来使用HTTP/2。然后,如果客户端和服务器都支持请求,您将把请求升级到版本2。

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.

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

#2


6  

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

即使已经定义了CURL_HTTP_VERSION_2_0,拥有PHP >= 5.5.24也不足以使用curl发出HTTP/2请求。如果您试图向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.

因为curl是libcurl的绑定,所以还必须启用了http/2。

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

有关示例代码,请参见我对类似问题的回答

For install procedure, you can follow this tutorial

对于安装过程,您可以遵循本教程

#3


2  

At the current moment there is no direct HTTP/2 support in PHP.

目前在PHP中没有直接的HTTP/2支持。

There is an idea to add such a support in the future direct to PHP: https://wiki.php.net/ideas/php6#http2_support

将来有一种方法可以直接将这种支持添加到PHP: https://wiki.php.net/ideas/php6#http2_support

The 3rd Party library Guzzle https://github.com/guzzle/guzzle supports HTTP/2, if the correct php and curl version are installed:

第三方库Guzzle https://github.com/guzzle/guzzle支持HTTP/2,如果安装了正确的php和curl版本:

use GuzzleHttp\Client;

$client = new Client();
$client->get('https://http2.akamai.com/demo/tile-0.png', [
    'version' => 2.0,
    'debug' => true,
]);

#4


0  

Check out the Apache and CLI PHP Docker images I built for this purpose that add HTTP/2 support to the official PHP 5.6 docker library. This gets rid of any HTTP/2 client preface string missing or corrupt errors.

查看我为此目的构建的Apache和CLI PHP Docker映像,这些映像为PHP 5.6 Docker库添加了HTTP/2支持。这将消除任何HTTP/2客户端字符串丢失或损坏错误。

Once you have the right environment, having tried several JWS/JWT libraries for PHP I only found Spomky-Labs/jose to work perfectly with APNs.

一旦您有了合适的环境,在为PHP尝试了几个JWS/JWT库之后,我只发现Spomky-Labs/jose在使用APNs时非常出色。