Reputation: 802
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.
Upvotes: 2
Views: 314
Reputation: 13853
I wouldn't rely on file_get_contents
.
A lot of (shared) webservers don't allow file_get_contents
to fetch files from other servers, but do allow the use of cURL.
$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
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_fopen
setting can be used to allow or disallow opening URL's using fopen
, which applies to file_get_contents
too. 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 ;)
Upvotes: 1