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?
If your PHP server allows url fopen wrappers then the simplest way is:
$html = file_get_contents('https://stackoverflow.com/questions/ask');
If you need more control then you should look at the cURL functions:
$c = curl_init('https://stackoverflow.com/questions/ask'); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); //curl_setopt(... other options you want...) $html = curl_exec($c); if (curl_error($c)) die(curl_error($c)); // Get the status code $status = curl_getinfo($c, CURLINFO_HTTP_CODE); curl_close($c);
Read more... Read less...
You may want to check out the YQL libraries from Yahoo: http://developer.yahoo.com/yql
The task at hand is as simple as
select * from html where url = 'http://stackoverflow.com/questions/ask'
You can try this out in the console at: http://developer.yahoo.com/yql/console (requires login)
Also see Chris Heilmanns screencast for some nice ideas what more you can do: http://developer.yahoo.net/blogs/theater/archives/2009/04/screencast_collating_distributed_information.html
Simple way: Use
$page = file_get_contents('http://stackoverflow.com/questions/ask');
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,
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('http://example.com'); echo $url;
this solution will display the webpage on your site. However curl is a better option.
look at this function: