php http options,使用PHP发出HTTP / 2请求

有没有办法强制PHP与另一台服务器建立HTTP2连接只是为了查看该服务器是否支持它?

我试过了:

$options = stream_context_create(array(

'http' => array(

'method' => 'GET',

'timeout' => 5,

'protocol_version' => 1.1

)

));

$res = file_get_contents($url, false, $options);

var_dump($http_response_header);

并试过:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_HEADER, 1);

curl_setopt($ch, CURLOPT_NOBODY, 1);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HTTP_VERSION, 3);

$response = curl_exec($ch);

var_dump($response);

curl_close($ch);

但是,如果我使用以下网址https://www.google.com/#q=apache 2.5 http / 2,这两种方式都会给我一个HTTP1.1响应

我正在从启用HTTP / 2 SSL的域发送请求.我究竟做错了什么?

解决方法:

据我所知,cURL是PHP中唯一支持HTTP 2.0的传输方法.

您首先需要测试您的cURL版本是否可以支持它,然后设置正确的版本标头:

if (curl_version()["features"] & CURL_VERSION_HTTP2 !== 0) {

$url = "https://www.google.com/";

$ch = curl_init();

curl_setopt_array($ch, [

CURLOPT_URL =>$url,

CURLOPT_HEADER =>true,

CURLOPT_NOBODY =>true,

CURLOPT_RETURNTRANSFER =>true,

CURLOPT_HTTP_VERSION =>CURL_HTTP_VERSION_2_0,

]);

$response = curl_exec($ch);

if ($response !== false && strpos($response, "HTTP/2") === 0) {

echo "HTTP/2 support!";

} elseif ($response !== false) {

echo "No HTTP/2 support on server.";

} else {

echo curl_error($ch);

}

curl_close($ch);

} else {

echo "No HTTP/2 support on client.";

}

标签:php,curl,http2

来源: https://codeday.me/bug/20190527/1166866.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP支持多种HTTP请求方法,包括常见的GET、POST、PUT、DELETE等。下面是各种方法的简要介绍: 1. GET方法:用于从服务器获取资源,通常将参数附加在URL末尾发送请求。 ```php $url = 'http://example.com/api/resource'; $response = file_get_contents($url); ``` 2. POST方法:用于向服务器提交数据,通常用于表单提交等操作。 ```php $url = 'http://example.com/api/resource'; $data = array('key1' => 'value1', 'key2' => 'value2'); $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query($data), ), ); $context = stream_context_create($options); $response = file_get_contents($url, false, $context); ``` 3. PUT方法:用于向服务器更新资源,通常用于更新已有的数据。 ```php $url = 'http://example.com/api/resource'; $data = array('key1' => 'new_value1', 'key2' => 'new_value2'); $options = array( 'http' => array( 'method' => 'PUT', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query($data), ), ); $context = stream_context_create($options); $response = file_get_contents($url, false, $context); ``` 4. DELETE方法:用于从服务器删除资源,通常用于删除已有的数据。 ```php $url = 'http://example.com/api/resource'; $options = array( 'http' => array( 'method' => 'DELETE', ), ); $context = stream_context_create($options); $response = file_get_contents($url, false, $context); ``` 注意:以上示例使用`file_get_contents()`函数发送HTTP请求,也可以使用其他HTTP请求库,如cURL。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值