php - file_get_contents() 不会超时/返回 HTTPS url 错误

标签 php ssl https file-get-contents

我不知道为什么,但我有一个特定的 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,它可以正常工作。

更新 1
file_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 certificate problem: unable to get local issuer certificate (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 并得到了这个结果:


Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /usr/www/users//t051.php on line 12

Warning: file_get_contents(): Failed to enable crypto in /usr/www/users//t051.php on line 12

Warning: file_get_contents(https://...): failed to open stream: operation failed in /usr/www/users//t051.php on line 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
https://www.example.com/foo.html 的 cURL 返回这个标题:

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返回: enter image description here

更新 9
我通过外部服务器尝试了相同的脚本和 URL,并且可以正常工作。当然它需要将 verify_peer 设置为 false 以避免 Update 5 中发布的 ssl 证书错误。我检查了 phpinfo()。有一些差异。但是两者都通过 CGI/FastCGI 使用 SSL Version OpenSSL/1.0.1ePHP Version 5.5.23,但是我服务器上的一个设置吸引了我注意:

Registered Stream Socket Transports tcp, udp, unix, udg, ssl, sslv3, tls

国外服务器还有sslv2。会不会是这个原因?

更新 10 - 暂时解决
在与我的托管服务提供商交换了一些电子邮件后,一位技术人员想尝试一个新脚本(follow_location=falseverify_peer=false)并通过控制台执行它:

php -d allow_url_fopen=On ./t052.php |less

然后他给我发邮件说他没问题。我自己通过浏览器对其进行了测试,令人惊讶的是,他和我所有其他的测试脚本又能正常工作了?!他向我证实他没有改变任何东西。这真的很令人沮丧,因为它现在看起来像是一种随机行为。由于该项目仍在运行并收集 URL,如果它再次发生,我将更新我的问题。

最佳答案

也许您应该在您的 cURL 请求中禁用 SSL 验证:

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

也设置超时:

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,30); 
curl_setopt($ch, CURLOPT_TIMEOUT, 400);

更新:

对于file_get_contents,根据this answer ,必须启用 php_openssl 扩展并且 allow_url_fopen 必须处于事件状态

关于php - file_get_contents() 不会超时/返回 HTTPS url 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29628594/

相关文章:

php - 计算从mysql到php表的不同值

php - 警告 : array_merge(): Argument #2 is not an array Warning: array_merge(): Argument #1 is not an array

google-chrome - 阻止不安全的内容 - Google Chrome

php - 我的应用程序缓存 list 测试有什么问题?

java - 无法使用 HTTPS 连接

php - Laravel include 导致错误:Method Illuminate\View\View::__toString() 不得抛出异常

php - 使用 HTML Mime Mail for PHP 发送电子邮件,但需要通过 Exchange 服务器进行身份验证

ssl - OpenSSL 证书 : How to sign csr directly to pem file?

ssl - 发送到 https url 的消息是否可以免受中间人攻击?

java - 为什么 HttpUrlConnection 在移动数据连接上抛出 SSLException?