PHP GET HTML source code WebPages using DOM Parser

Mysql Mysqli php Programming

PHP GET HTML source code WebPages using DOM Parser

In this Post We Will Explain About is PHP GET HTML source code WebPages using DOM Parser With Example and Demo.Welcome on – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to how to get html source code of a website using phpExample

In this post we will show you Best way to implement Parse Web Pages with PHP Simple HTML DOM Parser, hear for PHP to get html source, then parse values within certain DIV tagswith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

PHP: How to get (view) html source code of a website

$your_domain = '';
$file_handel = fopen($your_domain, 'r');
$data_content = stream_get_contents($file_handel);

echo "<textarea rows='20' cols='80'>$data_content</textarea>";

Read source code with PHP

$ch = curl_init('');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$data_content = curl_exec($ch);
echo $data_content;

Using PHP to parse html tables and extract values

$data_content = '<ul>
 <li>item 1</li>
 <li>item 2</li>
 <li>item 3</li>
 <li>pakainfo 1 2 3..</li>
 <li>pakainfo 1 2 3..</li>

$dom = new DOMDocument();
$userdataList = $dom->getElementsByTagName('ul');
foreach ($userdataList->item(0)->getElementsByTagName('li') as $userdataList) {
    echo $userdataList->nodeValue.'<br />';                    

Step 1 : curl using PHP to parse with PHP file data Get


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//all result
$result = curl_exec($ch);
//data json format
$obj = json_decode($result, TRUE);
//count total records
$total_load_record = count($obj['items']);

Step 2 : curl using PHP parse to HTML get source code

$string_data = str_replace("(titaldata)","",$obj['items'][6]['title']);
	$u_id = $obj['items'][$i]['id'];
	$string = str_replace(' ', '_', $string_data);
	$url = "".$string;
	$ch = curl_init();

	// Tell simple curl what URL we want.
	curl_setopt($ch, CURLOPT_URL, $url);

	// Set simple this if you ant don't want the content simple header.
	curl_setopt($ch, CURLOPT_HEADER, 0);

	// your Download the HTML part from the URL.
	$data_content = curl_exec($ch);

	/* Insert Records*/
	$dom = new DOMDocument();
	$dom->validateOnParse = true; 
	$dom->preserveWhiteSpace = false;
	$xpath = new \DOMXpath($dom);
	$rows  = $xpath->query('//tr[@class="cardtablerow"]');
	$value = "";
	$total_row = $rows->length;


I hope you have Got What is PHP HTML source code DOM Parser And how it works.I would Like to have FeadBack From My Blog( readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article( Are Most Always Welcome.

Related FAQ

Here are some more FAQ related to this Article:

  1.   PHP cURL Download File From Url Example
  2.   Laravel 6 CSRF Protection Token Examples
  3.   PHP Domdocument Get selectors Class Tags Table img Elements
  4.   Angular 7|8|9 Validate Mobile number With 10 Digits Example
  5.   PHP Laravel delete files from Public Folder
  6.   Radio button Component in VueJS
  7.   Custom Pagination Using WooCommerce REST API
  8.   Laravel 6 crud Insert Update Delete operations Example
  9.   jquery form validation without plugin
  10.   Laravel 6 Eloquent ORM MVC Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *