How to Extract Title and Meta Tags from Website URL using PHP Code

Authors: CodeToday | PHP Code | Views: 860 | Posted: 04 AM: 09/19/2017

Every website url contains tags like title, description, keywords ...You can get these tags directly on the site but it will take a lot of time.

Today I will guide you how to Extract Title and Meta Tags from Website URL using PHP Code. 

Bootstrap Library

Use the bootstrap library only for the purpose of formatting submit form:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

HTML

<form action="" method="post" class="form-inline">
<div class="form-group">
<input type="text" name="domain" id="domain" value="<?=$_POST['domain']?>" size="80" class="form-control" placeholder = "Enter domain here ..." required/>
</div>
<div class="form-group">
<button class="btn btn-info" type="submit" name="submit"><i class="glyphicon glyphicon-search"></i> Extract Tags Now</button>
</div>
</form>

Self-built functions

function get_meta_tag($html)	{
	$doc = new DOMDocument();
	@$doc->loadHTML($html);
	$nodes = $doc->getElementsByTagName('title');		
	if(isset($nodes->item(0)->nodeValue))
		$title = $nodes->item(0)->nodeValue;
	else
		$title="";		
		$response=array();
		$response['title']=$title;
		$metas = $doc->getElementsByTagName('meta');
		for ($i = 0; $i < $metas->length; $i++)
		{
			$meta = $metas->item($i);
			if($meta->getAttribute('name')!='')
				$response[$meta->getAttribute('name')] = $meta->getAttribute('content');
		}

		return $response;
}
function curlGet($url){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
	curl_setopt($ch, CURLOPT_HTTPGET, 1 );
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1 );
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1 );
	curl_setopt($ch, CURLOPT_URL, $url );
	curl_setopt($ch, CURLOPT_REFERER, $ref );
	curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);     
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}

PHP Source Code

if(isset($_POST['submit'])) {
    
	$url  = 'http://www.'.trim($_POST['domain']);
	$data = curlGet($url);	
	$metaTags = get_meta_tag($data);
	echo '<div class = "row"><div class="col-sm-12">';
	echo '<h3 class="text-primary" style="margin-top:5px">Title & MetaTags</h3>';
	echo '<table class="table table-hover ">';
	foreach($metaTags as $infoKey=>$infoValue) {
		echo '<tr><td class="label label-primary" style="font-size: 12px;margin-left:10px"><strong>'.$infoKey.':</strong></td><td>'.$infoValue.'</td></tr>';
	}
	echo '</table>';
	echo '</div></div>';

}
After completing the above steps and try the domain: "sitelinks.info" you will see the picture below:

How to Extract Title and Meta Tags from Website URL using PHP Code


If you have any questions, please leave a message below