Php 函数文件_get_content don';我不偶尔工作
当我使用Php 函数文件_get_content don';我不偶尔工作,php,Php,当我使用文件\u get\u content下载php脚本执行结果时: //mainfile.php $ctx = stream_context_create(array( 'http' => array( 'timeout' => $this->cfg['gp_timeout']/2 ) ) ); $url_xml='http://test.server.eu/we
文件\u get\u content
下载php脚本执行结果时:
//mainfile.php
$ctx = stream_context_create(array(
'http' => array(
'timeout' => $this->cfg['gp_timeout']/2
)
)
);
$url_xml='http://test.server.eu/webgatescript.php?getphoto¶m1=1&test=1¶m2=2¶m3=3';
$webgateResponse=file_get_contents($url_xml,false,$ctx);
据我所知,函数file\u get\u content
返回string
-如果文件不存在,则文件内容返回FALSE
尽管我100%确信远程文件返回了内容(在我的情况下,xml文件我有访问权和日志),但函数file\u get\u content
会不时返回FALSE
。此外,file\u get\u content
在远程脚本执行结束后返回FALSE
几微秒,但比设置超时所需的时间更短
部分日志:
10:22:04<?xml version="1.0" encoding="UTF-8"?>
10:22:16<response><htlName/><lang/><texts/></response>
10:22:46<?xml version="1.0" encoding="UTF-8"?>
10:22:58<response><htlName/><lang/><texts/></response>
10:23:28<?xml version="1.0" encoding="UTF-8"?>
10:23:29<response><htlName/><lang/><texts/></response>
10:23:59<?xml version="1.0" encoding="UTF-8"?>
10:23:59<response><htlName/><lang/><texts/></response>
10:24:29<?xml version="1.0" encoding="UTF-8"?>
10:24:29<response><htlName/><lang/><texts/></response>
Warning: file_get_contents(http://test.server.eu/webgatescript.php?getphoto¶m1=1&test=1¶m2=2¶m3=3): failed to open stream: HTTP request failed! in /home/www/mainfile.php on line 22
Call Stack:
0.0002 93616 1. {main}() /home/www/mainfile.php:0
175.5346 109072 2. file_get_contents(string(172), bool, resource(14) of type (stream-context)) /home/www/mainfile.php:22
10:24:5910:25:46<?xml version="1.0" encoding="UTF-8"?>
10:25:47<response><htlName/><lang/><texts/></response>
运行
webgatescript.php
的每个人都在进行日志记录。我不会依赖文件获取内容
许多(共享)Web服务器不允许file\u get\u contents
从其他服务器获取文件,但允许使用
这不容易,但非常可靠
我(可能)找到了答案!正如我提到的,您可以设置一个选项,允许或不允许file\u get\u contents
(以及一些其他函数)从URL获取文件
我已经找到了答案。allow\u url\u fopen
设置可用于允许或禁止使用fopen
打开url,这也适用于文件获取内容。它应该在默认情况下启用,所以我不知道为什么它对您不起作用
不管怎样,我仍然支持卷曲;) 你试过没有暂停吗<代码>$webgateResponse=file\u get\u contents($url\u xml)代码>是否显示所有警告?当返回FALSE时,file\u get\u内容也会发出警告,告诉您更多发生的事情。所有这些测试都是在while(TRUE)循环中使用相同的url地址进行的,所以每次请求的url都是完全相同的,而answear olso是(如果是由file\u get\u内容返回的)。@gd1:是的,我知道。甚至我使用了$http\u response\u header
变量来显示响应头,响应代码是200,但我第一次使用这个变量,我不确定这个变量是否显示了这个文件的头request@HamZaDzCyberDeV:是的,这是同样的结果。你有任何证据支持你的说法吗“更可靠"? 我在http流包装器中使用file_get_内容已经很多年了,没有任何问题。谢谢你,它工作得很好,所以它一定是有问题的file_get_内容
函数。。。还有一个问题:file\u get\u contents
使用HTTP通信协议,所以应该在不同的服务器之间建立良好的连接。“为什么写信给你说不是呢?”戈登我唯一的证据就是经验。共享Web服务器通常不允许跨服务器文件获取内容,但允许使用cURL。这取决于服务器的配置。@Skamielina如果它对您有效,请按答案左侧的大“V”标记我的答案为正确。你的回答率将会提高,让人们更好地回答你的问题。@TimS。谢谢OP的主机允许从远程位置获取内容,因此建议cURL仍然只是治标不治本。不过我会撤销dv。
exec("php webgatescript.php >> tom2.log");
$url = 'http://www.stackoverflow.com';
// start cURL
$ch = curl_init($url);
// set options (http://www.php.net/manual/en/function.curl-setopt.php)
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4); // four seconds until connect timeout
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // we want the response in a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 4); // four seconds until timeout
$response = curl_exec($ch); // get contents
curl_close($ch); // close the cURL handle