简体   繁体   English

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

[英]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 在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 此处描述了基于HTTP / 2的新Apple推送通知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的客户端身份进行请求。

The CURL extension for PHP >= 5.5.24 has support for HTTP/2. PHP> = 5.5.24的CURL扩展支持HTTP / 2。 (since this commit ) (由于此提交

You also need a libcurl installed — the underlying library that the curl functions use — with HTTP/2 support enabled. 您还需要安装libcurl(curl函数使用的基础库)并启用HTTP / 2支持。 That means a libcurl newer than 7.38.0 but really, the newer the better. 这意味着libcurl比7.38.0更新,但实际上,更新越好。 Libcurl has to have been built with HTTP/2 support explicitly enabled, using the --with-nghttp2 flag at compile time. 必须在编译时使用--with-nghttp2标志,在显式启用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 . 只需CURL_HTTP_VERSION_2_0使用CURLOPT_HTTP_VERSION ,然后通过传入CURL_HTTP_VERSION_2_0来将CURLOPT_HTTP_VERSION选项设置为使用HTTP / 2。 Then you'll get the request upgraded to version 2 if the client and server both support it. 然后, 如果客户端和服务器均支持该请求,则将请求升级到版本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. 在PHP 5.5.24之前,如果libcurl是使用HTTP / 2支持构建的,则可以显式传递CURL_HTTP_VERSION_2_0的int值,因为PHP仍会将其传递给libcurl。 Currently, it has a value of 3 — this should not change, but could . 当前,它的值为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);

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. 使PHP> = 5.5.24不足以发出带有curl的HTTP / 2请求,即使已定义CURL_HTTP_VERSION_2_0。 You will get an error message like the following if you try to make a request to APNS (Apple Push Notification Service): 如果您尝试向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的绑定 ,因此还必须启用curl并启用http / 2。

For a sample code, see my answer to a similar question here on SO 有关示例代码,请在此处查看我对类似问题的回答

For install procedure, you can follow this tutorial 有关安装过程,您可以按照本教程进行操作

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: 如果安装了正确的php和curl版本,则第三方库Guzzle https://github.com/guzzle/guzzle支持HTTP / 2:

use GuzzleHttp\Client;

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

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. 查看我为此目的构建的ApacheCLI PHP Docker映像,这些映像将HTTP / 2支持添加到了官方PHP 5.6 docker库中。 This gets rid of any HTTP/2 client preface string missing or corrupt errors. 这消除了所有HTTP/2 client preface string missing or corrupt错误。

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可与APN完美配合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM