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 t开发者_Go百科ried 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.
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.
You can try PHP's function file_get_contents
echo file_get_contents($url);
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?
If you are using a URL as a file name, allow_url_fopen must be enabled in php.ini. I would start there.
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);
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.
精彩评论