|Back to examples
The following script allows you to retrieve the google page position for your
site for a given search phrase. The script performs the search, and automatically
pages through the results until it finds your domain.
A few things to note about this script:
The URLs are extracted by searching for a specific font tag combination which
Google currently uses to display the URL name on the search results page. This
tag may change in future.
The script current asks for the results in batches of 10 hits per page. You
can easily modify it to ask for more - ie 100, which will speed the whole process
up. However, you will get different rankings. The more hits you ask for per page,
the more Google will "expand" internal domain results or display similar
results within the same site, thus dropping your site down the virtual rankings.
The script also counts two ranking types - the physical position of your site
in the search results, plus the "site" position. The "site"
position is how your site is ranked once the internal domain results / similar
results are removed ( remember, a single site can postential occupy two result
// $searchquery is the value to search for.
// The script replaces the spaces and ampersands and
// converts them to values that google is expecting.
// $searchurl is the url to find - ie www.web-max.ca
// Do not pass http:// - you don't need it.
// $searchquery = "ontario inns";
// $searchurl = "www.selectontarioinns.com";
if(!empty($searchquery) && !empty($searchurl))
$query = str_replace(" ","+",$searchquery);
$query = str_replace("%26","&",$query);
// How many results to search through.
$total_to_search = 100;
// The number of hits per page.
$hits_per_page = 10;
// Obviously, the total pages / queries we will be doing is
// $total_to_search / $hits_per_page
// This will be our rank
$position = 0;
// This is the rank minus the duplicates
$real_position = 0;
$found = NULL;
$lastURL = NULL;
for($i=0;$i<$total_to_search && empty($found);$i+=$hits_per_page)
// Open the search page.
// We are filling in certain variables -
// $query,$hits_per_page and $start.
$filename = "http://www.google.com/search?as_q=$query".
$file = fopen($filename, "r");
echo "<p>Unable to open remote file $filename.\n";
// Now load the file into a variable line at a time
$var = fgets($file, 1024);
// Try and find the font tag google uses to show the site URL
// If we find it take out any <B> </B> tags - google does
// highlight search terms within URLS
$out = strtolower(strip_tags($out));
// Get the domain name by looking for the first /
$x = strpos($out,"/");
// and get the URL
$url = substr($out,0,$x);
// If you want to see the hits, set $trace to something
// If the last result process is the same as this one, it
// is a nest or internal domain result, so don't count it
// on $real_position
$lastURL = $url;
// Else if the sites match we have found it!!!
$found = $position;
// We quit out, we don't need to go any further.
print("The URL $searchurl is at position $found ".
"( $real_position ) for the term $searchquery");