【问题标题】:file_get_contents() works differently on different machinesfile_get_contents() 在不同机器上的工作方式不同
【发布时间】:2018-12-11 13:02:20
【问题描述】:

我已经编写了一段 php 代码来使用 file_get_contents() 从站点下载 .js 文件并尝试从 2 台不同的机器上运行代码,它们会产生不同的结果。代码是:

$link = "https://www.scotchwhiskyauctions.com/scripting/store-scripting_frontend.js";

$options = array(
  'http'=>array(
     'method'=>"GET",
     'header'=>"Accept-language: en\r\n" .
          "User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n"  ),
  'ssl'=>array(
     'verify_peer'=>false,
     'verify_peer_name'=>false),
);
$context = stream_context_create($options);
$line = file_get_contents($link, false, $context);
var_dump($http_response_header);
echo $line;
exit;

当我在 Debian 8.11 机器上运行这段代码时,它会产生以下错误:

PHP Warning:  file_get_contents(https://www.scotchwhiskyauctions.com/scripting/store-scripting_frontend.js): failed to open stream: Connection timed out in /var/www/test.php on line 4
PHP Notice:  Undefined variable: http_response_header in /var/www/test.php on line 4
NULL

但是,当我在另一台机器(Debian 4.16.12-1kali1)上运行完全相同的代码时,它可以获得文件内容,变量$http_response_header 包含所有响应头。两台机器都使用php7.2。在花了几天时间试图找出导致 Debian 8.11 机器无法读取文件的原因后,我在两台机器上都使用了wget,并再次注意到 Debian 8.11 (jessie) 机器无法读取文件。

我怀疑它与 ssl 证书有关,所以我跑了

sudo update-ca-certificates
sudo update-ca-certificates --fresh

但它根本没有帮助。

谁能指点我的方向?

【问题讨论】:

  • 你在哪里定义$http_response_header
  • @Dormilich $http_response_header 会自动填充 - 您不必定义它:php.net/manual/de/reserved.variables.httpresponseheader.php
  • allow_url_fopen 在两台机器的 php.ini 文件中设置为 On,仅供参考。
  • @albertma789 你能访问 URL 吗? (使用浏览器 f.e.)
  • @Xatenev 是的,我可以。

标签: php ssl file-get-contents


【解决方案1】:

最后我通过关注某人对this post的评论解决了这个问题

echo 0 > /proc/sys/net/ipv4/tcp_timestamps

我在Linux Advanced Routing & Traffic Control HOWTO 文章中发现了以下内容。

/proc/sys/net/ipv4/tcp_timestamps

时间戳用于防止 包装序列号。可以想象一个 1 Gb 的链路 再次遇到具有线外值的先前序列号, 因为它是上一代的。时间戳会让它 认出这个“古包”。

但是我不知道它为什么起作用。谁能解释一下?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    相关资源
    最近更新 更多