Google Ranking Checker Class in PHP

Important notice: Check google rankings become a very complicated task today. So many environment variables are important so that this simple ranking checker class don’t work any more. But there are some commercial tools that can still track your rankings like sistrix or seolytics.

The only goal for an SEO is a good or very good google ranking. To ensure this you have to monitor your rankings and compare it to the positions of your competitors. With the Google AJAX Search API and my little PHP Class you can easy build a Google Ranking Checker …

The class needs an Google API key for the AJAX Search API (get it here) … it’s just one field and a click and you can start. You can check multiple keywords for multiple domains or urls, just pass this two arrays to the check() method.

Here the complete source code of the Ranking Checker, you can test it here.

googleApiKey = $googleApiKey;
		$this->checkPageCount = $checkPageCount;
	}
 
	/**
	 * get rankings
	 * @example $checker->check(array('bohuco'), array('bohuco.net'));
	 * @param array $keywords search these keywords
	 * @param array $domains domains to compare against
	 */
	public function check($keywords, $domains) {
 
		$rankings = array();
 
		if (! is_array($keywords)) { throw new Exception('Keywords array is no array'); }
		if (! is_array($domains)) { throw new Exception('Domains array is no array'); }
 
		foreach($keywords as $keyword) {
			$keyword = trim($keyword);
			$rows = array();
 
			if ($keyword) {
			    for($i=0;$i<$this->checkPageCount;$i++) {
		    		$start = $i*8;
		    		$url = sprintf('%s&hl=de&gl=AT&q=%s&rsz=8&key=%s&start=%s', $this->googleBaseUrl, urlencode($keyword), $this->googleApiKey, $start);
 
		    	    if ($result = file_get_contents($url)) {
		    			$result = json_decode($result);
		    			$rows = array_merge($rows, $result->responseData->results);
		    	    }
		    	}
 
		    	foreach($domains as $url) {
		    		$rankings[$keyword][$url] = '-';
		    		foreach($rows as $position => $row) {
		    			if (strpos($row->url, trim($url)) !== false) {
		    				$rankings[$keyword][$url] = $position+1;
		    				break;
		    			}
		    		}
		    	}
			}
		}	
 
		return $rankings;
	}
 
}

31 thoughts on “Google Ranking Checker Class in PHP”

  1. HI,

    This is very useful. Have you licensed this as GPL or MIT etc. Meaning may I use this in my own projects, for clients etc

    1. hi, thanks for concern, the class has no license it is just free to use and modify for everyone. maybe when i have time i choose a gpl or lgpl license for it …

  2. I checked out the test script and its not working. I tried a few keywords and came up with no results then i tried the most obvious keyword ‘Google’ and its coming up as 9th position? it should be #1 position.

    1. Thanks for comment, i have checked the code and it works fine … but: my class is using the german language version and austria as country so google.at is on first position.

      you can easily change that in the class … and maybe i will add this feature to the online demo … when i have time …

  3. you’re absolutely right… i was thinking about that on my way home from work. If the server the script is located on is somewhere else the results will be different. i’ll have to play with your script.

    thanks for the quick feedback :)

  4. but its not very accuracy because i prove with keyword ‘deportes’ and domain ‘clarin.com’ and not results!!! (-) :S

  5. Hey DerFichtl,
    The new version works great! I see a call to RankingChecker.php. Do you have a link to the code in that file somewhere?

    I tried copying the code above to create a RankingChecker.php file to use with the new version, however I get this error:

    Fatal error: Call to undefined method RankingChecker::setLocale() in /home/wadmin/public_html/index.php on line 17

    Thank you, I really appreciate it.

  6. The Zune concentrates on being a Portable Media Player. Not a web browser. Not a game machine. Maybe in the future it’ll do even better in those areas, but for now it’s a fantastic way to organize and listen to your music and videos, and is without peer in that regard. The iPod’s strengths are its web browsing and apps. If those sound more compelling, perhaps it is your best choice.

  7. Pingback: reusal
  8. Pingback: keyword statistics
  9. Thanks for your valuable posting.I have collect more than information from your website. It is really wonderful blog. please added more than tips. i’m working in Erp Software in india.Here providing very low price and Quality ERP,Cloud ERP, CMS , responsive webdesign and ERP. you have any more than information kindly make me call this number 044-42127512 or send your mail info@excelanto.com.

Comments are closed.