我正在尝试在服务器上放一个文件,就像从from从from(" nall_file.php")发送到同一服务器上的另一个文件(" processing_file.php"))。这是代码:
$url = 'https://www.example.com/path/to/processing_file.php';
$method = 'POST';
$eol = "rn";
// Create a stream
$opts = array(
'http' => array(
'timeout' => 30,
'method' => $method,
'header' => 'Content-Type: application/x-www-form-urlencoded' . $eol,
'content' => http_build_query($xmit)
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$result = file_get_contents($url, false, $context); //filename, use_include_path, context
//echo($result);
$result_decode = json_decode($result,true);
$ XMIT看起来像这样:
array(
'dx' => json_encode('[{"business_date":"2015-09-29","check_avg":10.749122993789,"guests":"422"},{"business_date":"2015-09-30","check_avg":36.735215894737,"guests":"461"},{"business_date":"2015-10-01","check_avg":12.2527145,"guests":"288"},{"business_date":"2015-10-02","check_avg":18.405998957055,"guests":"263"},{"business_date":"2015-10-03","check_avg":20.385927936508,"guests":"289"}]'),
'period' => 5,
'phase_code' => 3,
'site_id' => 155
)
当我回荡$选择时,一切看起来都正确。HTTP_BUILD_QUERY($ XMIT)正在创建看起来有效的发布数据的内容。我在processing_file.php和calle_file.php中有此集合:
ini_set('allow_url_include', '1');
ini_set('allow_url_fopen', '1');
当我粘贴时: http://www.example.com/path/to/processing_file.php
直接在我的浏览器中,该文件以200个状态打开,所有状态都不错。
我尝试将processing_file的所有者设置为Apache用户,并将CHMOD设置为775。
但是,当Cally_file.php尝试使用上述代码调用processing_file.php时,我得到了:
Warning: file_get_contents(https://www.example.com/path/to/processing_file.php): failed to open stream: Connection timed out in /var/www/example.com/functions/path/to/other/file/calling_file.php on line 219
第219行是:
$result = file_get_contents($url, false, $context); //filename, use_include_path, context
我不知道为什么看不到文件。
我在周末遇到了这个问题,发现它在Archlinux上影响了我,而Fedora 25的全新安装。
对于更多的上下文,我发现在file_get_contents()
上进行的任何网络调用都在计时,在使用curl进行等效的网络调用时,我会成功。此外,ping
也遇到了同样的问题。
file_get_contents()
在内部使用的一些低级别,系统范围内的实用程序是通过进行IPv6查找并随后的时间来解决DNS。奇怪的是,我所有其他应用程序(卷曲,网络浏览器等)都使用IPv4并正常工作。
我通过禁用IPv6系统宽来解决此问题。
为了文档的缘故,这些是我的ping
S,证明了问题:
ping,使用默认调用:
[hpierce@localhost ~]$ ping www.google.com
PING www.google.com(ord38s04-in-x04.1e100.net (2607:f8b0:4009:813::2004)) 56 data bytes
^C
--- www.google.com ping statistics ---
147 packets transmitted, 0 received, 100% packet loss, time 149492ms
ping,使用IPv4的标志:
[hpierce@localhost ~]$ ping www.google.com -4
PING www.google.com (172.217.0.4) 56(84) bytes of data.
64 bytes from ord38s04-in-f4.1e100.net (172.217.0.4): icmp_seq=1 ttl=54 time=18.9 ms
64 bytes from ord38s04-in-f4.1e100.net (172.217.0.4): icmp_seq=2 ttl=54 time=16.9 ms
^C
--- www.google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 16.958/17.939/18.921/0.990 ms