Windows php file_get_contents在localhost上返回504

时间:2017-05-04 08:48:30

标签: php curl nginx fastcgi http-status-code-504

我遇到PhP问题时,在做

之类的事情时会超时
file_get_contents("http://127.0.0.1/test.php")

我也尝试过使用CURL和其他东西,当我尝试从其他地方接收数据时,它可以正常工作,但当它是本地主机时,我得到了504。

我在Windows 10 x64上 Php版本是7.0.18 nginx版本是1.8.0

到目前为止,我已经尝试了所有目录的设置权限,我尝试了不同的用户代理等,我甚至尝试复制与浏览器中的Chrome相同的请求,但是我运气不好。

我在error.log中收到此nginx错误。

2017/05/04 10:26:33 [error] 7732#5384: *5 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 127.0.0.1, server: _, request: "GET /test.php HTTP/1.0", upstream: "fastcgi://[::1]:9123", host: "127.0.0.1"

这是我从access.log获得的。

127.0.0.1 - - [04/May/2017:10:25:33 +0200] "GET /indexsss.php HTTP/1.1" 504 584 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36"
127.0.0.1 - - [04/May/2017:10:25:33 +0200] "GET /favicon.ico HTTP/1.1" 404 570 "http://127.0.0.1/indexsss.php" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36"
127.0.0.1 - - [04/May/2017:10:26:33 +0200] "GET /test.php HTTP/1.0" 504 182 "-" "PHP"

这是我从php_errors.log获得的php错误

[04-May-2017 10:26:33 Europe/Copenhagen] PHP Warning:  file_get_contents(http://127.0.0.1/test.php): failed to open stream: HTTP request failed!  in C:\Servers\nginx-1.8.0\html\local\indexsss.php on line 208
[04-May-2017 10:26:33 Europe/Copenhagen] PHP Fatal error:  Maximum execution time of 30 seconds exceeded in C:\Servers\nginx-1.8.0\html\local\indexsss.php on line 208
使用浏览器访问它时,

http://127.0.0.1/test.php工作正常。所以我很无能,我希望有人可以帮助我。

indexsss.php包含大量注释代码(因此执行的行号) 唯一没有注释的代码就是这个。

$test = file_get_contents("http://127.0.0.1/test.php");

test.php的

"Hello, this is a test"

如果您需要更多信息,请与我们联系。

2 个答案:

答案 0 :(得分:0)

maximum_execution_time(默认为30秒)设置为更大的值 - 例如你的php.ini文件60秒。或者更好地优化您的indexsss.php脚本 - 因为它会导致此错误。

更新:

<?php
$options = ['http'=>[
    'method'=>"GET",
    'header'=>"Accept-language: dk\r\n"
  ]
];

$ctx = stream_context_create($options);

$test = file_get_contents('http://127.0.0.1/test.php', false, $ctx);
?>

http://php.net/manual/en/info.configuration.php#ini.max-execution-time

http://php.net/manual/en/function.stream-context-create.php

答案 1 :(得分:0)

您是否配置了任何调试(xdebug f.e.)。如果是,则需要将其配置为支持同时线程。