Page 1 of 1
file_get_contents() or copy() in php4
Posted: Tue May 23, 2006 9:44 pm
by neogeek
Does anyone know of a way to retrieve files from another web site in php4? I have tried file_get_contents() and copy() and have had nothing but headaches. I know that this is possible in php5, but I'm looking for a solution that can be used by both php4 and php5 supported servers. If anyone has any ideas as to how to do this, it would be appreciated.
Posted: Tue May 23, 2006 9:46 pm
by John Cartwright
fopen()?
What's wrong with
file_get_contents() anyways?
Posted: Tue May 23, 2006 9:50 pm
by neogeek
I keep crashing my local server and getting this error on another:
Warning: file_get_contents(
http://www.neo-geek.net/favicon.ico): failed to open stream: No route to host in
/home2/neogeek/public_html/file.php on line 3
And this is the code that is returning that error:
Code: Select all
<?php
file_get_contents('http://www.neo-geek.net/favicon.ico');
?>
Posted: Wed May 24, 2006 1:34 am
by timvw
You might want to verify if your webhosts allows outgoing connections first.. (eg: can you ping the 'external' domain?)
(
http://www.php.net/curl would be the de-facto standard for performing http requests with php)
Posted: Wed May 24, 2006 3:11 am
by neogeek
Thanks I will look into that. However I am trying to write a code that will be distributed to different people that might not have access to adding plugins to their servers. Is there any other possible way to retrieve files using php4? Something like this seems like it should be very simple (like using copy(), but only in php5). I hope that if there is a solution that it is found here because I can imagine that I am not the only one who has run into a problem such as this.
Posted: Wed May 24, 2006 3:17 am
by onion2k
file_get_contents() and copy() are basically the same in PHP 4 and 5 as of 4.3. If you're experiencing differences then it's almost certainly due to the way you have PHP 4 set up in php.ini.
Posted: Wed May 24, 2006 3:25 am
by neogeek
During trying to get this to work I came across
this. I looked at my local and remote php.ini values using ini_get('allow_url_fopen ') and the value was set at 1. Could there be other forces at work here besides just allow_url_fopen?
Posted: Wed May 24, 2006 10:14 pm
by neogeek
Thank you everyone for your help with this matter. After some more messing around in PHP I think I finally have a solution:
Code: Select all
define('lnbr', "\n");
function fetch_remote_file($file) {
$path = parse_url($file);
$fs = @fsockopen($path['host'], 80);
if ($fs) {
$header = 'GET ' . $path['path'] . ' HTTP/1.0' . lnbr;
$header .= 'Host: ' . $path['host'] . str_repeat(lnbr, 2);
fwrite($fs, $header);
$buffer = '';
while ($tmp = fread($fs, 1024)) { $buffer .= $tmp; }
preg_match('/Content-Length: ([0-9]+)/', $buffer, $matches);
return substr($buffer, -$matches[1]);
} else { return false; }
}