php - 如何在 HTTP/1.1 中使用 PHP cURL

标签 php curl server-sent-events php-curl

我的服务器 cURL 版本在进行远程调用时比客户端的版本新,客户端的服务器自动切换到 http/2,有什么办法可以强制使用 curl 和 http/1.1
如何将 cURL HTTP 版本设置为 1.1,我通过在代码中添加以下设置进行了测试,但添加后 curl 停止工作。各种帮助都是可观的。提前致谢。

 curl_setopt($ch, CURLOPT_HTTP_VERSION, '1.1');
 curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
有什么办法可以在 HTTP 1.1 中使用 curl 吗?
我的服务器版本是
PHP 版本 7.2.16
curl 信息 7.62.0
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

//curl_setopt($ch, CURLOPT_HTTP_VERSION, '1.1');
//curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
curl_setopt($ch, CURLOPT_TIMEOUT, 2400);  */
curl_setopt($ch,CURLOPT_HTTPHEADER, array(
   //  "http:1.1",
  'http' => array(
     'timeout' => 5,
     'protocol_version' => 1.1,
     'header' => 'Connection: close'
   ),
   "Content-Type: text/event-stream",
   "cache-control:no-cache",
   "Access-Control-Allow-Origin:*",
   'Authorization: Basic '. base64_encode($user.':'.$pass)
 ));


 $error  = 0;
 $result = curl_exec($ch);
 $info  = curl_getinfo($ch);
 $err   = curl_errno($ch);
客户的技术团队回复:问题是 -> 您从 1.1 版开始,然后更改为 2 版
使用HTTP2,服务器支持多用
连接状态改变(HTTP/2 确认)

最佳答案

根据documentation of curl_setopt()编码

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
应该是正确的。 “ curl 停止工作”是什么意思?你得到的实际错误是什么?您还可以尝试提取有关错误的更多详细信息:
# before curl_exec():
error_reporting(~0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $filehandle);
# maybe also: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
# maybe also: curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

# after curl_exec():
$curl_error = curl_error($ch);
$curl_error_number = curl_errno($ch);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

关于php - 如何在 HTTP/1.1 中使用 PHP cURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55214930/

相关文章:

php - 如何在 HTML 表中创建一个按钮,使 PHP 执行 MySQL 数据库查询而不需要单独的 js 文件?

javascript - 服务 JavaScript 时使用哪个字符集与 PHP 的 header

linux - 在调用 curl 的脚本中使用变量

javascript - 单击提交按钮后,jQuery 选择器未显示复选框的选定值,并且我收到错误 [object Object]

php - jquery更多按钮问题

json - 在 Linux Bash 中从 cURL 获取 JSON 值

php - 如何将 SOAP 调用交换到 cURL,以在allow_url_fopen 限制内工作?

Scala:避免可变实例变量

javascript - 带有服务器发送事件的 AngularJS

ios - 尽管我的应用程序未运行,是否可以使用服务器端事件来通知 iOS 用户,而不使用 Apple 推送通知服务?