How to Get Top Keywords for a Website from Alexa.com using PHP

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

You want to see a website with the top keywords visited, you can go to alexa.com, ahrefs.com, similarweb.com ... Today I will guide you how to get the top keywords visited from alexa.com of a website.

The knowledge to know before doing this article is as follows:

  1. curl_init(): Get the data directly from alexa.com.
  2. DomDocument: Analyze the html structure to retrieve the Top Keywords.
  3. foreach: Create a loop to retrieve the keyword.

Bootstrap Library

<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> Get Top Keywords Now</button>
</div>
</form>

Self-built functions used in this article:

<?
function getTopKeywords($domain) {
global $keywordValue;
	$doc = new DomDocument;
	@$doc-> loadHTML(curlGet('http://www.alexa.com/siteinfo/'.$domain));
	$dataKeyword = @$doc->getElementById('keywords_top_keywords_table');
	$myDataKeyword = $dataKeyword->getElementsByTagName('tr');
		foreach ($myDataKeyword as $nodeKeyword) {
		   foreach($nodeKeyword->getElementsByTagName('span') as $spanKeywordValue) { $ikeyword++;		  
			  $keywordValue[$ikeyword][] = $spanKeywordValue->nodeValue;	  	   
		   }
		   $ikeyword = 0;
		}   
}
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 Full Source Code

<?
if(isset($_POST['submit'])) {

	$domain = trim($_POST['domain']);
    getTopKeywords($domain);
	echo '<p><pre>Top Keywords for '.$domain.'</pre></p>';
	echo '<div class = "row">';
	foreach($keywordValue[2] as $keyword) { $i++;
      $perS = $keywordValue[3][$i-1];
	  echo '<div class="col-sm-12">';
	  echo '<p>'.$i.'. '.$keyword.' <span class="pull-right"><strong>'.$keywordValue[3][$i-1].'</strong></span></p>';
	  echo '<div class="progress"><div class="progress-bar progress-bar-info progress-bar-striped active" role="progressbar" aria-valuenow="'.$perS.'" aria-valuemin="0" aria-valuemax="100" style="width:'.$perS.'"></div></div>';
	  echo '</div>';
	}
echo '</div>';
}
?>

After performing the above steps and check with domain "fallingdownbeer.com", the results are as shown below:

How to Get Top Keywords for a Website from Alexa.com using PHP


If you have any questions, please leave a message below