Reputation: 1653
How do i retrieve the entire page source info of a particular web page in a string variable and echo it in php. i am new to php and have no idea of doing so can any one give me the complete source code of it. Following is my source code:
<?php
$dom = new DOMDocument;
$dom->loadHtmlFile('http://www.google.com');
$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";
}
}
?>
I have replaced the above code with
just changed the url to: 'http://www.flipkart.com/professional-android-2-application-development-8126525894/p/itmdytmwpjzyhade?pid=9788126525898&ref=8a47bf68-7558-43ce-a9b2-17c1ac119e84'
but it gives the following error: Warning: file_get_contents(http://www.flipkart.com/professional-android-2-application-development-8126525894/p/itmdytmwpjzyhade?pid=9788126525898&ref=8a47bf68-7558-43ce-a9b2-17c1ac119e84) [function.file-get-contents]: failed to open stream: HTTP request failed! in C:\wamp\www\displaycontentswebpage.php on line 2
Expected Result:(Page source)
<title>Professional Android 2 Application Development 8126525894: Book: Reto Meier (9788126525898) | Flipkart.com</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
<!--<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> -->
<meta property="fb:page_id" content="102988293558" />
<meta property="fb:admins" content="658873552,1412400758,624500995,100000233612389"/>
<meta name="Keywords" content="professional android 2 application development, buy professional android 2 application development, professional android 2 application development india, professional android 2 application development review, reto meier, 8126525894, 9788126525898" />
<meta name="Description" content="Professional Android 2 Application Development by Reto Meier. Rs.449, Save 25%. Buy Professional Android 2 Application Development, All India Free Home Delivery. 8126525894, 9788126525898 |" />
<link rel="canonical" href="http://www.flipkart.com/professional-android-2-application-development-8126525894/p/itmdytmwpjzyhade" />
<link rel='shortcut icon' href='http://img5.flixcart.com/www/prod/images/favicon-18354.ico' />................something something..........................
src="http://googleads.g.doubleclick.net/pagead/viewthroughconversion/1017598645/?value=0&label=9tgBCLOv-QIQtaWd5QM&guid=ON&script=0"/>
</div>
</noscript></div>
Kindly help.
Upvotes: 0
Views: 1645
Reputation: 100
$dom = new DOMDocument ('1.0');
@$dom->loadHTMLfile ('https://mp3skull.cr');
$thisi=$dom->saveHTML();
echo htmlentities($thisi);
this will print the html source of the page
Upvotes: 0
Reputation: 29932
That is achieved via a single function in PHP file_get_contents()
(http://php.net/file-get-contents) This returns the contents of a file as string.
// print source to current output
echo file_get_contents( 'http://www.google.com' );
// print content as readble format
echo htmlspecialchars( file_get_contents( 'http://www.google.com' ), ENT_SUBSTITUTE );
The ENT_SUBSTITUTE
- or ENT_IGNORE
-flag is necessary for htmlspecialchars()
if the input contains an invalid code unit sequence within the given encoding. See http://php.net/htmlspecialchars#refsect1-function.htmlspecialchars-returnvalues
Upvotes: 0