File Get Contents Not Working on Remote Server

I am trying to use the file_get_contents function with no success.

I have a php file on ANOTHER server that echoes two numbers like so:

456,756

It puts those out in text format, so those are the only characters in the file.

From my other server I am trying to file_get_contents that PHP file and put the set of numbers into a variable.


$fcontents = file_get_contents("http://www.myothersite.com/canvar.php?tid=635");
echo $fcontents;

I know the link works in the browser, but nothing is echoed. What am I doing wrong here?

Thanks
Ryan

Run this and see if “http” is in the list.


<?php
print_r(stream_get_wrappers());
?>

I tried your code and it just blanks the rest of the page. It puts out nothing as far as I can tell.

Ryan

I just noticed, I don’t have PHP 5, still using one of the 4s

Ryan

Try
<?php
phpinfo();
?>

see what
allow_url_fopen is set to.

both are on ‘ON’

YA you need php5 to use file_get_contents. You’ll have to go the long route and use fopen.

use

$fcontents = readfile("http://www.myothersite.com/canvar.php?tid=635");
echo $fcontents;

or

$fcontents = implode('', file("http://www.myothersite.com/canvar.php?tid=635"));
echo $fcontents;

or

$filename = "http://www.myothersite.com/canvar.php?tid=635";
$handle = fopen($filename, "r");
$fcontents = fread($handle, filesize($filename));
fclose($handle);
echo $fcontents;

Errr - v4.3 onwards, I think you’ll find.

http://uk.php.net/file_get_contents

Mike

You need PHP5 for [fphp]file_put_contents[/fphp] but not [fphp]file_get_contents[/fphp].

ah! my mistake

file_get_contents is a good quick way to get file contents through the file system or over a supported protocol. The problem with it is, that it really doesn’t do very well at simulating an actual web client.

There is an optional module for Apache called mod_security, which uses special rules to filter out what might be bad requests.

One of the many rules that can be used, is one that stipulates that the user-agent string cannot be blank. file_get_contents sends only minimal headers, and presumably does not send a user-agent string of any kind.

If you run into that particular problem, try using CUrl or an http client class like Snoopy. You may have better luck with something like that, because it fully simulates a web client in a way that the file system functions were not designed to.

You can make the fopen wrappers send a user agent string. Set this in your php.ini file:

user_agent=“Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3”

Thanks for the help.

Yes, I remember successfully using file get contents before on a separate project, but it has been a while since using it.

Wish it worked, would have been easiest. :wink:

Ryan