Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

php

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

https://stackoverflow.com/questions/ask

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?

like image 910
djmzfKnm Avatar asked May 04 '09 07:05

djmzfKnm


People also ask

How do I get the PHP code from a website?

If you want to view the source code of a site you control in-browser, consider the FirePHP extension for Firebug, or just access your site files with your preferred method. Show activity on this post. in your apache configuration should do the trick. Note, you will need to save your .

How do I get the HTML code from a website?

To view only the source code, press Ctrl + U on your computer's keyboard. Right-click a blank part of the web page and select Page source from the pop-up menu that appears.


1 Answers

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); 
like image 117
Greg Avatar answered Sep 24 '22 00:09

Greg