【问题标题】:How to use PHP cURL with HTTP/1.1如何在 HTTP/1.1 中使用 PHP cURL
【发布时间】:2019-08-08 10:35:51
【问题描述】:

远程调用时,我的服务器 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 确认)

【问题讨论】:

  • "curl 停止工作" 错误信息是什么?有日志吗?
  • 感谢您的回复,抱歉,我使用时没有生成日志 // curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); // curl_setopt($ch, CURLOPT_HTTP_VERSION, '1.1');它停止并且不输出 $info = curl_getinfo($ch);
  • 看起来 curl 有效,但服务器忽略了它。您是否在 telnet 终端中尝试过原始请求?
  • 是的,这适用于命令提示符。 : [ curl.exe -ivk --http1.1 -u USERNAME:PASSWORD server.com/stream ] 客户的教学团队:您的 curl 版本比我的更新,显然会自动尝试切换到不起作用的 http/2跨度>
  • 设置CURLOPT_VERBOSE为ON,有助于调试。更多信息:curl.haxx.se/libcurl/c/CURLOPT_VERBOSE.html

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


【解决方案1】:

根据documentation of curl_setopt()的代码

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

应该是正确的。 “卷曲停止工作”是什么意思?你得到的实际错误是什么?您还可以尝试提取有关错误的更多详细信息:

# 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);

【讨论】:

    猜你喜欢
    • 2016-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2017-12-08
    • 2012-10-12
    • 2012-08-01
    相关资源
    最近更新 更多