file_get_contents() or copy() in php4

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
neogeek
Forum Newbie
Posts: 24
Joined: Sun May 14, 2006 4:24 am

file_get_contents() or copy() in php4

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

fopen()?

What's wrong with file_get_contents() anyways?
User avatar
neogeek
Forum Newbie
Posts: 24
Joined: Sun May 14, 2006 4:24 am

Post 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');

?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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)
User avatar
neogeek
Forum Newbie
Posts: 24
Joined: Sun May 14, 2006 4:24 am

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
neogeek
Forum Newbie
Posts: 24
Joined: Sun May 14, 2006 4:24 am

Post 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?
User avatar
neogeek
Forum Newbie
Posts: 24
Joined: Sun May 14, 2006 4:24 am

Post 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; }

}
Post Reply