Get Visitors Located for a Website from Alexa using PHP

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

You can see which country the most visits on a particular website if you check that website on alexa.com. Today I will guide you to use php code to check a website that has visitors from which country. Once you know which country visits your website, you will have a specific business plan for that country.

Next article: How to Get Top Keywords for a Website from Alexa.com using PHP, You can use PHP to Get Visitors Located for a Website from Alexa.com.

The knowledge you need to know in this lesson:

  1. curl_init():Get data from alexa.com.
  2. new DomDocument: Analyze html structure to retrieve country access.
  3. preg_match_all: Filter out country access and put into array.
  4. array(): Country and Percent access.

HTML Form

<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 Visitors Located Now</button>
</div>
</form>

Self-built functions used in this article

<?
function getVisitorsLocated($domain) {
global $topCountries;
	$doc = new DomDocument;
	@$doc-> loadHTML(curlGet('http://www.alexa.com/siteinfo/'.$domain));
	$data = @$doc->getElementById('demographics_div_country_table');
	$my_data = $data->getElementsByTagName('tr');
	$topCountries = array();
	foreach ($my_data as $nodeCt)
	{
		foreach($nodeCt->getElementsByTagName('a') as $href)
		{
			preg_match_all('/([0-9.\%,]+)/',$nodeCt->nodeValue, $match);
			
			if($href->nodeValue == ' sign up and get certified'){
				$topCountries[] = array(
					'country' => '',
					'percent' => '',
				  );
			}else{
				$topCountries[] = array(
					'country' => $href->nodeValue,
					'percent' => $match[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 Source Code

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

	$domain = trim($_POST['domain']);
    getVisitorsLocated($domain);
	echo '<p><pre>Visitors Located for '.$domain.'</pre></p>';
	echo '<div class = "row">';
	echo '<table class = "table table-hover">';
	echo '<tr><td><strong>Country</strong></td><td><strong>Percent</strong></td><td><strong>Rank</strong></td></tr>';
	foreach ($topCountries as $topCountry) {
		echo '<tr><td>'.trim($topCountry[country]).'</td><td>'.$topCountry[percent][0].'</td><td>'.$topCountry[percent][1].'</td></tr>';
	}
	echo '</table>';
	echo '</div>';
}
?>

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

Get Visitors Located for a Website from Alexa using PHP

Note: Getting data directly from alexa.com will limit the number of retrieval, so you need to consider before applying

If you have any questions, please leave a message below