PHP: Download Web Page using file_get_contents
You can use curl to download a webpage in PHP. It is also possible to download a web page using file_get_contents().
<?php
function downloadURL($URL) {
$webpage = file_get_contents ($URL);
return $webpage;
}
$webpage = downloadURL("http://www.mozilla.org/");
if ($webpage){
echo $webpage;
}
else {
echo "Error in downloading the webpage\n";
}
?>
$ php download.php <html> .... </body> </html>
In the above example, we try to download the web page of Mozilla. Let’s try to download a non existing web page
<?php
function downloadURL($URL) {
$webpage = file_get_contents ($URL);
return $webpage;
}
$webpage = downloadURL("http://www.mozilla.org/1");
if ($webpage){
echo $webpage;
}
else {
echo "Error in downloading the webpage\n";
}
?>
We find the following error
$ php download.php Warning: file_get_contents(http://www.mozilla.org/1): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /home/prof/samuel/Documents/Dropbox/Personal/Programs/downloadWebpage.php on line 4 Error in downloading the webpage
Comments: