There are many tools available to check keyword position on Google. I have always been curious about how it’s done so tried various methods and found a simple way that I will build on for later purposes.

The example below will grab a “Search Engine Result Page” (SERP) from Google using the keywords entered then check the position of our domain name, as long as it’s in the top 100.
It was made more for example purposes than use in a live environment but should return fairly accurate results for low volume queries.
I will update this a fair amount as I will be using it from time to time.
At the moment it gets confused with results that have “site links” and Places results. Better defining the xpath should help avoid this.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Checking keyword position with PHP</title>
<style type="text/css">
body{font-family:Verdana, Geneva, sans-serif; color:#666;}
#center{width:350px;margin:5px auto 0 auto;}
form { float:left; width:380px; }
input, label { width:70%; clear:left; float:left; margin-top:5px; }
input{color:#999;}
input[type="submit"] { width:30%; clear:left; float:left; margin-top:20px; color:#666; }
.small{ font-size:0.7em; color:#999999}
h3{font-size:1.4em;}
#kwdz{ width:400px; position:relative; top:5px;}
#domainz{width:400px; margin-top:10px;}
#result{margin-top:20px;}
</style>
</head>
<body>
<div id="center">
<form action="index.php" method="post">
<div id="domainz">
<label>Domain:</label>
<input name="domain" value="elevatelocal.co.uk" onclick="this.value=''" />
</div>
<div id="kwdz">
<label>Keywords:</label>
<input name="keywords" value="internet marketing agency" onclick="this.value=''" />
</div>
<input type="submit" name="check" value="Get position" />
</form>
<br style="clear:both" />
<?php
$i = 1; $hit = 0;
if($_POST) {
// Clean the post data and make usable
$domain = filter_var($_POST['domain'], FILTER_SANITIZE_STRING);
$keywords = filter_var($_POST['keywords'], FILTER_SANITIZE_STRING);
// Remove begining http and trailing /
$domain = substr($domain, 0, 7) == 'http://' ? substr($domain, 7) : $domain;
$domain = substr($domain, -1) == '/' ? substr_replace($domain, '', -1) : $domain;
// Replace spaces with +
$keywords = strstr($keywords, ' ') ? str_replace(' ', '+', $keywords) : $keywords;
// Grab the Google page using the chosen keywords
$html = new DOMDocument();
@$html->loadHtmlFile('http://www.google.co.uk/search?q='.$keywords.'&num=100');
$xpath = new DOMXPath($html);
// Store the domains to nodes
$nodes = $xpath->query('//div[1]/cite');
// Loop through the nodes to look for our domain
$hit = 2;
foreach ($nodes as $n){
// echo '<div style="font-size:0.7em">'.$n->nodeValue.'<br /></div>'; // Show all links
if (strstr($n->nodeValue, $domain)) {
$message = 'Position '.$i.'<br />'; $hit = 1;
}
else { ++$i; }
}
}
?>
<div id="result">
<?php // Echo the result
if ($hit == 1) { echo '<h2>'.$message.'</h2>'; }
else if ($hit >= 2) { echo '<h2>Not found!</h2>'; }
?>
</div>
</div>
</body>
</html>Please let me know if you have any issues or feedback as it will all help to improve the code!