Archive

Posts Tagged ‘ajax’

Fetch Google Pages Indexed Using PHP and AJAX

April 1st, 2010 No comments

Here’s a quick and dirty solution I threw together to fetch how many pages of a site google has index. I needed this for one of my control panels, so my work group could track this and other metrics on the fly.

gpindex.php

<?php
	// Some arbitrary access code so not everyone can access it. Not "real" security :)
	if($_GET['accessCode']!="some_password") { echo "Access Denied"; exit; }
 
	$site = $_GET['site'];
	$gurl = "http://www.google.com/search?q=site:";
 
	$curl_handle=curl_init();
	curl_setopt($curl_handle,CURLOPT_URL,$gurl.$site);
	curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
	curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
	$buffer = curl_exec($curl_handle);
	curl_close($curl_handle);
 
	preg_match_all("#Results(.*)of about <b>(.*)</b> from <b>$site</b>#",$buffer,$matches,0);
	echo $matches[2][0];
?>

And here’s the Ajax via jQuery that is used to fetch the information:

<script type="text/javascript" src="js/patch/jquery.min.js"></script>
<script type="text/javascript">
   $(document).ready(function(){
      $.ajax({
         url:"http://www.example.com/path/to/gpindex.php?accessCode=some_password&site=example.com",
         success:function(d){$("#googleIndexed").html(d+" Pages");}
      });
   });
</script>
<p style="font-size:11px;">Indexed on Google</p>
<p style="font-size:11px; font-weight:bold;" id="googleIndexed">( Fetching Data )</p>
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Reddit
  • StumbleUpon
Categories: PHP Tags: , ,