Google Ranking Checker Class in PHP
Bookmark on del.icio.us
Tweet
Tweet
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.
<?php Class RankingChecker { private $googleApiKey = null; private $googleBaseUrl = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0'; private $checkPageCount = null; /** * get an api key from: http://code.google.com/apis/ajaxsearch/signup.html * @param string $googleApiKey * @param int $checkPageCount */ public function __construct($googleApiKey, $checkPageCount = 5) { $this->googleApiKey = $googleApiKey; $this->checkPageCount = $checkPageCount; } /** * get rankings * @example $checker->check(array('phpasks'), array('phpasks.com')); * @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; } } //$keywords[0] = 'adult dating'; //$website[0] = 'adultxdating.com'; ?> <html> <head> <title>Google Ranking Checker Class in PHP5 by PHPASKS</title> <style type="text/css"> body { font:12px arial,helvetica,sans-serif; padding:20px; } #info { margin-bottom:20px; } form { float:left; background:#f1f1f1; padding:20px; background:#f1f1f1; width:380px; } form textarea { width:100%; } table { float:left; width:380px; margin-left:10px; } table td { border:1px solid #eee; padding:5px; } .user { background:#fff; padding:5px; list-style-type:none; } h1 { font-size:20px; margin:0; } h2 { margin:0 0 10px 0; } button { position:absolute; right:0; bottom:0; font-size:20px; padding:5px 10px; margin:40px; } </style> </head> <body id="body" onload="init()"> <div id="info"> <h1>Google Ranking Checker Class <br /></h1> </div> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> <label>Keywords <small>(max. 5)</small>:</label> <textarea rows="5" cols="50" name="keywords"><?=$_POST["keywords"]?></textarea> <label>Domains:</label> <textarea rows="5" cols="50" name="website"><?=$_POST["website"]?></textarea> <input type="submit" name="check" value="Check now" /> </form> <table> <?php if(isset($_POST["check"])) { $keywords = explode(",",$_POST["keywords"]); $website = explode(",",$_POST["website"]); $RankingChecker = new RankingChecker('ABQIAAAAT0QYHPdhh7eyjzLSB5OrohQDB0iR9tbFmh1rKcNxqCRfRwKkyBQlv_4XYljSF-KiMYSm1bu_oiXDrw', 5); $rankings = $RankingChecker->check($keywords,$website); //echo "<pre>".print_r($rankings)."</pre>"; while(list($key,$val) = each($rankings)) { echo "<tr>"; echo "<td>".$key."</td>"; while(list($key1,$val1) = each($val)) { echo "<td>".$key1."</td>"; echo "<td>".$val1."</td>"; } echo "</tr>"; } } ?> </table> </body> </html>
No comments yet.