When I am using a file_get_content to download result of php script execution:
//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);
As I know, function file_get_content returns string – content of file, if file is not exist returns FALSE.
From time to time function file_get_content returns FALSE, despite I am in 100% sure that remote file returned content (xml file in my case, I have access and logs). Furthermore file_get_content returns FALSE few microsecond after end of execution remote script, but it takes less time then timeout was setted.
Part of logs:
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>
I made some tests, one of them was running file mainfile.php in exec (I have both files on this same test server, but on production server that will be two separated systems)
exec("php webgatescript.php >> tom2.log");
and everyone case of running webgatescript.php is logging.
I wouldn’t rely on
file_get_contents.A lot of (shared) webservers don’t allow
file_get_contentsto fetch files from other servers, but do allow the use of cURL.It’s not easy, but is very reliable.
I (probably) found the answer! As I mentioned, you can set an option that allows or disallows
file_get_contents(and some other functions) to get files from an URL.I have found the Filesystem and Streams Configuration Options. The
allow_url_fopensetting can be used to allow or disallow opening URL’s usingfopen, which applies tofile_get_contentstoo. It should be enabled on default, so I don’t know why it doesn’t work for you.Either way, I’m still pro-cURL 😉