【问题标题】:HTTP/1.1 426 Upgrade Required - file_get_contents (PHP) on Apache2 server (debian 9)需要 HTTP/1.1 426 升级 - Apache2 服务器 (debian 9) 上的 file_get_contents (PHP)
【发布时间】:2021-10-24 19:06:54
【问题描述】:

在这里的第一篇文章,我遇到了在本地机器/w XAMPP 上工作时没有遇到的问题。

我已经通过全新的 Apache2 和 PHP7 安装在我的 VPS 上推送了一些基本的 PHP 代码,其中一个文件实际上正在执行 file_get_contents 以在 Trello 的 API 上执行 GET 请求:

$lists = json_decode(file_get_contents("https://api.trello.com/1/boards/<<HIDDEN>>/lists?key={$id}&token={$token}"), true);

问题是我这样做时收到了这个 HTTP 响应:

[Tue Aug 24 14:27:14.132294 2021] [:error] [pid 17956] [client <<HIDDEN>>] PHP Warning:  file_get_contents(https://api.trello.com/1/boards/<<HIDDEN>>/lists?key=<<HIDDEN>>&amp;token=<<HIDDEN>>): failed to open stream: HTTP request failed! HTTP/1.1 426 Upgrade Required\r\n in <HIDDEN>/trello.php on line 7, referer: <<HIDDEN>>

我做了一些研究,发现我需要将调用升级到另一个 HTTP 协议,但到目前为止,我不知道如何进行......

有什么建议吗?

谢谢!

【问题讨论】:

  • 谷歌帮助:github.com/ConvertAPI/convertapi-php/issues/22 - $context = stream_context_create(array('http'=&gt;array('protocol_version'=&gt;'1.1')));
  • 这个问题已经有一个较旧的副本:stackoverflow.com/q/64059867/367456 - 它已经包含在问题中使用 curl 的解决方法。
  • @hakre 这个问题(你在链接中提到的)没有被接受的答案!
  • 是的,但它表明已经使用 curl 作为解决方法。也许值得在那里改进它,这样信息就不会那么分散。至少它已经是一个交叉引用。

标签: php http apache2


【解决方案1】:

好的,所以我仍然不知道如何使用file_get_contents 来解决这个问题,但我找到了一个似乎可以使用curl 的解决方法。

如果有些人也有此问题,我已将初始请求替换为:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.trello.com/1/boards/<<HIDDEN>>/lists?key={$id}&token={$token}");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2);
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $lists = json_decode(curl_exec($ch), true);
    curl_close($ch);

看起来curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2); 正在做这项工作。

这并没有回答根本问题(如何使用 file_get_contents 做到这一点),但如果它可以提供帮助...

谢谢!

【讨论】:

  • 也许在本地你有 curl 作为 HTTP 流包装器(流包装器由 file_get_contents('https?://... 使用)但不在服务器上,这将解释不同的行为。 php.net/manual/en/wrappers.http.php - php.net/manual/en/context.curl.php
  • 我喜欢你的回答是它展示了如何使用 CURL_HTTP_VERSION_2 来做到这一点。我想知道 HTTP 1.1 版是否足以使用 api.trello.com 服务,以及是否有一个选项可以让 curl 自动处理 HTTP/1.1 426 Upgrade Required。你能看看你的 XAMPP 设置是否有 CURL fopen/stream 包装器吗?
猜你喜欢
  • 2017-07-08
  • 1970-01-01
  • 2019-07-29
  • 2021-11-03
  • 2023-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多