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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34606801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:07:23  来源:igfitidea点击:

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

phpcurlpush-notificationapple-push-notificationshttp2

提问by mthuong

I found a similar question at Sending HTTP/2 POST request in RubyBut 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 推送通知基于 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 客户端的身份发出请求。

回答by Daniel Stenberg

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

PHP >= 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-nghttp2flag 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_VERSIONoption to use HTTP/2 by passing in CURL_HTTP_VERSION_2_0. Then you'll get the request upgraded to version 2 ifthe client and server both support it.

只需像通常使用的那样使用 curl,并CURLOPT_HTTP_VERSION通过传入CURL_HTTP_VERSION_2_0. 然后,如果客户端和服务器都支持它,您就会将请求升级到版本 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_0explicitly 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);

回答by tiempor3al

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 推送通知服务)发出请求,您将收到如下错误消息:

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

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

由于 curl 是libcurl的绑定,因此您还必须启用 curl 并启用 http/2。

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

有关示例代码,请参阅我在 SO 上对类似问题的回答

For install procedure, you can follow this tutorial

对于安装过程,您可以按照本教程

回答by Gennadiy Litvinyuk

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

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

There is an ideato 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/guzzlesupports HTTP/2, if the correct php and curl version are installed:

第 3 方库 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,
]);

回答by Norbert

Check out the Apacheand CLIPHP 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 corrupterrors.

查看我为此目的构建的ApacheCLIPHP Docker 映像,这些映像为官方 PHP 5.6 docker 库添加了 HTTP/2 支持。这消除了任何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/joseto work perfectly with APNs.

一旦你有了合适的环境,在尝试了几个用于 PHP 的 JWS/JWT 库后,我只发现Spomky-Labs/jose可以与 APNs 完美配合。