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 Pakainfo.com – 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 = 'https://www.pakainfo.com';
$file_handel = fopen($your_domain, 'r');
$data_content = stream_get_contents($file_handel);
fclose($file_handel);

echo "";

Read source code with PHP

$ch = curl_init('http://www.Pakainfo.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

Using PHP to parse html tables and extract values

 <?php
$data_content = '
  • item 1
  • item 2
  • item 3
  • pakainfo 1 2 3..
  • another
      • pakainfo 1 2 3..
      • another
        • '; //var_dump($data_content); $dom = new DOMDocument(); $dom->loadHTML($data_content); $userdataList = $dom->getElementsByTagName('ul'); foreach ($userdataList->item(0)->getElementsByTagName('li') as $userdataList) { echo $userdataList->nodeValue.'
          '; } ?>

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

$url="https://www.pakainfo.com/api/v1/newuserlist";

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

//all result
$result = curl_exec($ch);
curl_close($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 = "http://yugioh.wikia.com/wiki/".$string;
		
	$ch = curl_init();

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

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

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

Example

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(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

Leave a Comment