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

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 |  

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


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.


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.


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.


Example for downloading with cURL can be seen bellow.

$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);

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.


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
  Click to open user profile  
pgslot898, 22nd Dec 2021, 7:42 AM
im waiting for ur sharaing again.
<a href="https://pgslot898.com">it is my info, check for fun, thanks</a>
  Click to open user profile  
idasanka, 6th Feb 2022, 2:47 PM
Cant wait to find this love, soo great! https://customclosetsdesignalbuquerque.c....
  Click to open user profile  
thefansbuzz, 19th Feb 2023, 7:32 PM
Cheap SMM Panel Social Media Marketing is a huge field with a lot of competition. If you want to stand out from the crowd, you need to know how to find ways to make your content go viral. If you are interested in using social media to market your product, this service is perfect for you. The panel gives you access to hundreds of different social media sites, so that you can find new ways to market your business. Visit our website at: https://thefansbuzz.com/
  Click to open user profile  
social media , 29th Nov 2023, 11:44 AM
Online platforms called [url=https://smmpakpanel.com/https://smmpakpanel.com/[/url]] provide a variety of services to increase social media participation. These services include purchasing likes, followers, comments, and views, maintaining social media accounts, and scheduling postings.
  Click to open user profile  
racehorseoner, 4th Dec 2023, 4:43 AM
Affordable Social Media Marketing Panel The domain of Social Media Marketing is vast and very competitive. To distinguish yourself from others, it is essential to possess the knowledge of discovering methods to achieve widespread popularity of your material.
  Click to open user profile  
boles9, 11th Dec 2023, 8:23 PM
A well-executed 백링크 strategy is a game-changer for your website's SEO. Quality over quantity is the mantra here. Understand the nuances of backlink acquisition to stand out in the digital crowd.https://backlinkhigh.com
  Click to open user profile  
boles9, 14th Dec 2023, 4:21 PM
If you're in Hongdae, 홍대 룸 is the place to stay. Exceptional value for the price.http://hongdaeseven.com
  Click to open user profile  
smmpanelindia, 19th Dec 2023, 2:08 PM
Social media marketing services tailored to the Indian market are becoming increasingly important, as evidenced by the term . Businesses and individuals are realizing more and more that they require professional SMM panels in order to manage the complex and ever-changing social media world as India experiences a digital revolution. These panels provide a number of functions,
  Click to open user profile  
boles9, 20th Dec 2023, 1:41 PM
강남 토닥이 is a women's haven, a holistic escape from stress. Edge Therapy's skilled therapists make it an exceptional experience.https://todack22.com
online learning made for people