How to Get the HTML Code of a Web Page in PHP

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

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

How to get HTML source of any website with PHP?

That is because facebook runs on HTTPS (SSL Protocol) . Add this to your existing cURL parameters

curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, false);

How to get websites source code in to webpage as result using PHP

If you want to get the HTML source code of an URL, you can use cURL :

<?php
$ch = curl_init("http://www.example.com");
$html= curl_exec($ch);
curl_close($ch);
echo htmlspecialchars($html);
?>

more info here cUrl - getting the html response body and here php: Get html source code with cURL

How to retrieve the contents of PHP page in HTML page?

PHP cannot modify the content of the page after it has been served to the browser. However this would be trivial with a JavaScript Library such as jQuery. or by using AJAX to call the php script to update an element. There are examples on StackOverFlow like: Change DIV content using ajax, php and jQuery

get html code from aspx page with php?

If you want to get just the plain html of that particular site. You can use file_get_contents() on this one and then use htmlentities(). Consider this example:

<?php

$url = 'https://ph.godaddy.com/hosting/website-builder.aspx?ci=88060';
$contents = htmlentities(file_get_contents($url));
echo $contents;

?>

Sample Output

Get HTML source code of page with PHP

If you already have the parsing sorted, just use file_get_contents(). You can pass it a URL and it will return the content found at the URL, in this case, the html. Or if you have the file locally, you pass it the file path.

PHP code to read a web page's source and get attribute from a tag

By far the best way to do this is with the DOM extension to PHP.

$dom = new DOMDocument;
$dom->loadHtmlFile('your URL');

$xpath = new DOMXPath($dom);

$elements = $xpath->query('//input[@name="session_id"]');
if ($elements->length) {
echo "found: ", $elements->item(0)->getAttribute('value');
} else {
echo "not found";
}


Related Topics



Leave a reply



Submit