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;
	}
 
}