Dreevoo.com | Online Learning and Knowledge Sharing
 
Home | Programs | Programming languages | PHP & MySQL | Learn How To Download a Web Page With PHP
Guest
Click to view your profile
Topics
Programs
Languages
Recipes
Home
Shortcuts
 
 

Learn How To Download a Web Page With PHP

We will learn two different ways how to download a page in PHP from a web URL, learn how to use cURL and how to set a few additional options and finally show contents on your page.

 
  Author: podtalje | Version: php.net | 8th October 2013 |  
 
 
1.
 

The easiest way to download a web page is to use the  file_get_contents function.

Example:

<?php
$content=file_get_contents("http://dreevoo.com/info.php");
echo $content;
?>


The example will download the document from http://www.dreevoo.com/info.php and store it into $content variable.

We have also added line echo $content which will output the result to the browser.

 
 
2.
 

If we open our page in the browser you will be able to see the output.


The downloaded document will be without pictures and styles, because these are usually in different locations and you need to download them separately.

 
 
3.
 

In the following example we will use cURL library to download a page.

By using cURL we have more control and can set a few additional options.

Usually cURL extension is already enabled on web servers. If you will be testing this on your local server, you must enable extension=php_curl.dll in the php.ini file.

 
 
4.
 

Example for downloading with cURL can be seen bellow.

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.dreevoo.com/info.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0");
curl_setopt($ch, CURLOPT_REFERER, "http://www.dreevoo.com");

$output = curl_exec($ch);
curl_close($ch);

echo $output;
?>


In this example we have also set a few additional options.

CURLOPT_URL - URL to fetch.
CURLOPT_RETURNTRANSFER - If we want to store a result in a variable.
CURLOPT_HEADER - If header will be included in result.
CURLOPT_FOLLOWLOCATION - Redirects on the page will be followed.
CURLOPT_USERAGENT - Identify as standard browser.
CURLOPT_REFERER - The URL we are coming from.

 
 
5.
 

We can now again open our page in the browser and we will be able to see the output of the requested web page.





Also recommended:

 
 
 
   
  Please login to post a comment
   
 
 
online learning made for people
Dreevoo.com | CONTRIBUTE | FORUM | INFO