2

I am trying to read an external file, hosted on another server, but am having issues.

Here, I identify the variables:

$variable1 = "test";
$variable2 = "testing";

Here, I identify the URL to read:

$url = "http://test.com/page.php?v1=" . $variable1 . "&v2=" . $variable2;

Here, I read the external file:

$content = file($url);
$reading = $content[0];
echo $reading;

The odd thing is nothing comes out. I have tried entering the actual URL and it does work, however, when PHP tries to do it, it comes out blank.

I know it is not a fatal error as I tried just putting some regular text in there. So,there is something wrong that just is not reading the file correctly. Furthermore, I did make sure the reading server is no blocked by the server with the file on it.

Any suggestions would be appreciated.

Jacob
  • 21
  • 1
  • 2
  • 1
    I would recommend wrapping your variables with `urlencode`, and `print_r` the content array to see if it starts with a blank line. You might also consider `file_get_contents` depending on your end goal. – Brandon Horsley Sep 04 '11 at 01:29
  • 2
    Make sure allow_url_fopen is set to true. You can test this with ini_get() – Steve Buzonas Sep 04 '11 at 01:31

6 Answers6

0

Furthermore, I did make sure the reading server is no blocked by the server with the file on it.

How did you make sure?


First, ensure allow_url_fopen is on in php.ini.

If it is on, try setting a user agent. The remote file may expect a valid user agent.

ini_set('user_agent', 'Your browser\'s user agent or whatever');

If it is off, try using the cURL library to request the remote file. You can use cURL to set the user agent too.

alex
  • 479,566
  • 201
  • 878
  • 984
  • Allow_url_fopen is on. I tried setting the user agent with the agent "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9" Unfortunately, that did not work. :( Any other tips? – Jacob Sep 04 '11 at 01:32
  • @Jacob Weird. The remote server may be expecting all of the typical *browser* headers. Did you try cURL? Are you able to make the remote URL public so I can test? – alex Sep 04 '11 at 01:35
0

You can try PHP's function file_get_contents

echo file_get_contents($url);
Drew
  • 6,736
  • 17
  • 64
  • 96
  • `file_get_contents()` will route through where `file()` does, so if `file()` doesn't work I doubt any of the native PHP file functions will. – alex Sep 04 '11 at 01:34
0

In order to read files and parse them from remote URLs you should make sure that the option allow_url_fopen is enabled in your php.ini configuration.

Please note that this option is disabled by default for security reasons.

http://php.net/manual/en/filesystem.configuration.php

Should I allow 'allow_url_fopen' in PHP?

Community
  • 1
  • 1
Bassem
  • 3,135
  • 2
  • 25
  • 41
0

If you are using a URL as a file name, allow_url_fopen must be enabled in php.ini. I would start there.

Brent Friar
  • 10,588
  • 2
  • 20
  • 31
0

If you are fetching a website, and it starts with something like <html>, then echoing $content[0] will not show up when viewed from a browser.

Try:

echo htmlentities($content[0]);

Or:

print_r($content);
Brandon Horsley
  • 7,956
  • 1
  • 29
  • 28
0

file() loads data into an array. You then echo out only the first line of this document (echo $reading[0]). That first line is likely to be only an <html> tag or perhaps a doctype declaration.

If you're simply trying to echo out the full contents of the file, then do:

echo file_get_contents($url);

That fetches the specified URL and returns it as a single string - no line parsing, no array creation. Simply a plain string with the entire file in it.

Marc B
  • 356,200
  • 43
  • 426
  • 500