PHP GET HTML source code WebPages using DOM Parser

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!.

Read Also:  Laravel Blade check if variable is empty/isset

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;


Read Also:  Get python absolute value | Python abs() Function with examples

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. Read Also:  how to connect to phpmyadmin database in php?
  2. Read Also:  laravel where date between multiple columns
  3. Read Also:  How to check array is empty or not in php?
  4. Read Also:  How to get base url in laravel using php?
  5. Read Also:  PHP Domdocument Get selectors Class Tags Table img Elements
  6. Read Also:  how to remove html tags in php from mysql?
  7. Read Also:  Angular 9/8 Radio Button on Form Submit
  8. Read Also:  Laravel 7 Implement Flash Messages with example
  9. Read Also:  How to Select VueJs Check All Uncheck All Checkboxes
  10. Read Also:  Laravel 6 Print Last Query(Debugging Queries)