【发布时间】:2015-06-20 03:17:17
【问题描述】:
我不知道为什么,但我有一个不会超时的特定 URL(测试了数千个其他 URL 没有问题):
<?php
$url = 'https://....';
$context = stream_context_create(array(
'http' => array(
'follow_location' => false,
'timeout' => 2,
)
));
file_get_contents($url, false, $context);
?>
如果我将follow_location 设置为true,它可以正常工作。
更新 1file_get_contents 的调用不尊重'timeout' => 2,也不尊重PHP max_execution_time,但服务器本身似乎有第三次超时。它会在 10 (!) 分钟后返回 Internal Server Error 500。
更新 2
我用 cURL 尝试了相同的 URL,没有超时问题,但它返回 false:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($ch);
var_dump($contents);
?>
需要什么让cURL返回false?
更新 3
好的,现在我抓住了它:
if ($contents === false) {
echo curl_error($ch) . ' (' . curl_errno($ch) . ')' . PHP_EOL;
}
返回:
SSL 证书问题:无法获取本地颁发者证书 (60)
这意味着如果follow_location 设置为false,则每次发生此错误时file_get_contents() 都不会超时。对我来说听起来像是一个错误。
更新 4
正如@huggilou 建议的那样,我尝试了几个额外的设置,设置完之后我就可以加载 URL:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
file_get_contents() 有类似的设置吗?
更新 5
现在我尝试了以下方法:
$context = stream_context_create(array(
'http' => array(
'follow_location' => false,
'timeout' => 2,
),
'ssl' => array(
'verify_peer' => false,
),
));
echo file_get_contents($url, false, $context);
这导致了超时问题。之后我将verify_peer 更改为true 并得到了这个结果:
警告:file_get_contents():SSL 操作失败 代码1. OpenSSL 错误信息:error:14090086:SSL 例程:SSL3_GET_SERVER_CERTIFICATE:证书验证失败 /usr/www/users//t051.php 第 12 行
警告:file_get_contents():启用失败加密货币 /usr/www/users//t051.php 第 12 行
警告:file_get_contents(https://...) : 无法打开流:操作失败 /usr/www/users//t051.php在第12行
据我们所知:verfiy_peer 默认设置为 false。
提醒一下:如果我将verify_peer 设置为false 并将follow_location 设置为true,则网站已加载!?
更新 6
正如@huggilou 指出的那样:
<?php
$w = stream_get_wrappers();
echo 'allow_url_fopen: ', ini_get('allow_url_fopen') ? 'yes':'no', PHP_EOL;
echo 'openssl: ', extension_loaded('openssl') ? 'yes':'no', PHP_EOL;
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', PHP_EOL;
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', PHP_EOL;
echo 'wrappers: ', var_dump($w);
?>
返回:
allow_url_fopen: yes
openssl: yes
http wrapper: yes
https wrapper: yes
wrappers: array(12) {
[0]=>
string(5) "https"
[1]=>
string(4) "ftps"
[2]=>
string(13) "compress.zlib"
[3]=>
string(14) "compress.bzip2"
[4]=>
string(3) "php"
[5]=>
string(4) "file"
[6]=>
string(4) "glob"
[7]=>
string(4) "data"
[8]=>
string(4) "http"
[9]=>
string(3) "ftp"
[10]=>
string(4) "phar"
[11]=>
string(3) "zip"
}
更新 7
cURL 返回 https://www.example.com/foo.html 这个标头:
string(138) "HTTP/1.1 301 Moved Permanently
Location: http://example.com/foo.html
Content-Length: 0
Content-Type: text/html; charset=UTF-8
"
更新 8
https://www.ssllabs.com/ssltest/analyze.html?d=example.com 返回:
更新 9
我通过外国服务器尝试了相同的脚本和 URL,它在那里工作。当然它需要将verify_peer 设置为false 以避免Update 5 中发布的ssl 证书错误。我检查了phpinfo()。有一些差异。但是两者都使用SSL Version OpenSSL/1.0.1e 和PHP Version 5.5.23 到CGI/FastCGI,但是我服务器上的一个设置引起了我的注意:
Registered Stream Socket Transports tcp, udp, unix, udg, ssl, sslv3, tls
国外服务器还有sslv2。会不会是这个原因?
更新 10 - 暂时解决
在与我的托管服务提供商交换了一些电子邮件后,一位技术人员想尝试一个新脚本(follow_location=false 和 verify_peer=false)并通过控制台执行它:
php -d allow_url_fopen=On ./t052.php |less
然后他给我发电子邮件说他没有问题。我通过浏览器自己测试了它,令人惊讶的是他和我所有其他的测试脚本又能正常工作了?!他向我证实他没有改变任何东西。这真的很令人沮丧,因为它现在看起来像是一种随机行为。由于该项目仍在运行并收集 URL,如果再次发生,我将更新我的问题。
【问题讨论】:
-
我对此进行了测试,它在 2 秒内加载,这就是为什么它对我来说没有超时。
-
好的,你应该关闭这个问题
-
在我知道问题出在哪里之后,我会这样做或报告更新。
-
SO中有一篇关于
file_get_contentswith SSL的帖子 -
@mgutt 从技术上讲,这不是“随机”行为 :) PHP 的加密 https 流 从未(直到最近)观察到套接字超时设置。我知道这一点是因为我一直在努力解决这个问题。这是 PHP 5.4 中的“无法修复”,因为该分支现在只接收安全修复。但是,从 5.5.23 和 5.6.7 开始,在读取加密流时应该遵守超时。如果您在 >= 5.5.23 或 5.6.7 版本中遇到意外行为,请在 bugs.php.net 上创建新的错误报告
标签: php ssl https file-get-contents