如何在PHP中发送HTTP / 2 POST请求 [英] How do I send a HTTP/2 POST request in PHP

查看:2031
本文介绍了如何在PHP中发送HTTP / 2 POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在发现了一个类似的问题我在ruby中发送HTTP / 2 POST请求
但是我想用PHP更新我的服务器



新的Apple推送通知HTTP / 2的API: https://developer.apple.com/ library / ios / documentation / NetworkingInternet / Conceptual / RemoteNotificationsPG / Chapters / APNsProviderAPI.html



任何拥有HTTP / 2体验的人都可以帮我提出请求

解决方案

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 - 这不应更改,但可以



< pre class =lang-php prettyprint-override> 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);

这篇关于如何在PHP中发送HTTP / 2 POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆