How do I get the HTML code of a web page in PHP?


I want to retrieve the HTML code of a link (web page) in PHP. For example, if the link is

then I want the HTML code of the page which is served. I want to retrieve this HTML code and store it in a PHP variable.

How can I do this?

5/23/2017 12:26:33 PM

Accepted Answer

If your PHP server allows url fopen wrappers then the simplest way is:

$html = file_get_contents('');

If you need more control then you should look at the cURL functions:

$c = curl_init('');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
//curl_setopt(... other options you want...)

$html = curl_exec($c);

if (curl_error($c))

// Get the status code
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);

5/30/2020 8:26:36 PM

Also if you want to manipulate the retrieved page somehow, you might want to try some php DOM parser. I find PHP Simple HTML DOM Parser very easy to use.


You may want to check out the YQL libraries from Yahoo:

The task at hand is as simple as

select * from html where url = ''

You can try this out in the console at: (requires login)

Also see Chris Heilmanns screencast for some nice ideas what more you can do:


Simple way: Use file_get_contents():

$page = file_get_contents('');

Please note that allow_url_fopen must be true in you php.ini to be able to use URL-aware fopen wrappers.

More advanced way: If you cannot change your PHP configuration, allow_url_fopen is false by default and if ext/curl is installed, use the cURL library to connect to the desired page.


you could use file_get_contents if you are wanting to store the source as a variable however curl is a better practive.

$url = file_get_contents('');
echo $url; 

this solution will display the webpage on your site. However curl is a better option.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]