Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent function for file_get_contents()?

I want to parse some information out of a html page. Currently I solve the problem like this:

header("Content-type: text/plain");    
$this->pageSource = file_get_contents ($this->page);
header("Content-type: text/html");

$this->page is the url of the website. This works fine on XAMPP, but when I upload my script on my webserver, I get the following error message:

Warning: file_get_contents() [function.file-get-contents]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0

So obviously I am not allowed to execute that function on my webserver.

So is there an equivalent function to solve my problem?

like image 828
oopbase Avatar asked Jan 11 '11 09:01

oopbase


People also ask

What is the function file_get_contents () useful for?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string.

What will the file_get_contents () return?

Description ¶ This function is similar to file(), except that file_get_contents() returns the file in a string, starting at the specified offset up to length bytes. On failure, file_get_contents() will return false . file_get_contents() is the preferred way to read the contents of a file into a string.

What is the difference between file_get_contents () function and file () function?

The file_get_contents() function reads a file into a string. The file_put_contents() function writes data to a file.

How do I echo content in PHP?

readfile("/path/to/file"); This will read the file and send it to the browser in one command. This is essentially the same as: echo file_get_contents("/path/to/file");


3 Answers

Actually the function file_get_contents is not disabled,
but allow_url_fopen is disabled

you can replace it with curl

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->page);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$this->pageSource = curl_exec($ch);
curl_close($ch);

However, if you server block outgoing traffic, curl does not help too

like image 191
ajreal Avatar answered Oct 12 '22 11:10

ajreal


Use curl().

like image 28
Klemen Slavič Avatar answered Oct 12 '22 12:10

Klemen Slavič


cURL is the usual standard solution.

like image 22
Amber Avatar answered Oct 12 '22 10:10

Amber