我不知道为什么,但是我有一个不超时的特定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证书问题:无法获取本地颁发者证书(60)
这意味着,如果
file_get_contents()
设置为follow_location
,则每次发生此错误false
都不会超时。对我来说听起来像个虫子。更新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错误消息:错误:14090086:SSL
例程:SSL3_GET_SERVER_CERTIFICATE:证书验证失败
第12行的/usr/www/users//t051.php
警告:file_get_contents():无法启用加密
第12行的/usr/www/users//t051.php
警告:file_get_contents(https://..。):
无法打开流:操作失败
第12行的/usr/www/users//t051.php
众所周知,
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
以避免更新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,如果再次发生,我将更新我的问题。
最佳答案
也许您应该在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/