【发布时间】: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