Google Grabber — Using PHP to Find Out How Many Pages Your Domain Has Listed in Google

Written by David Walsh on Wednesday, January 23, 2008


Most bloggers make an effort to get as many pages listed on Google as possible. Benefits of being listed in Google may include:

  • Increased blog visits
  • Increased ad clicks
  • Broadened visitors / audience
  • Increased article comments
  • Increased referral revenues

Using a short amount of PHP code, you can query Google to retrieve the number of pages your domain has listed in Google.

The Code

/* return result number */
function get_google_results($domain = 'davidwalsh.name')
{
	// get the result content
	$content = file_get_contents('http://www.google.com/search?q=site:'.$domain);

	// parse to get results
	$result = get_match('/Results <b>(.*)from/isU',$content);

	// split the results
	$split1 = explode('of about',$result);

	// return result
	return $split1[1] ? strip_tags($split1[1]) : 0;
}

/* helper: does the regex */
function get_match($regex,$content)
{
	preg_match($regex,$content,$matches);
	return $matches[1];
}

The Usage

/* do it! */
echo 'davidwalsh.name: '.get_google_results('davidwalsh.name'); // 164
echo 'digg.com: '.get_google_results('digg.com'); // 3,790,000
echo 'google.com: '.get_google_results('google.com'); // 19,300,000
echo 'cnn.com: '.get_google_results('cnn.com'); // 2,180,000
echo 'imdb.com: '.get_google_results('imdb.com'); // 19,000,000
echo 'dzone.com: '.get_google_results('dzone.com'); // 484,000
echo 'fark.com: '.get_google_results('fark.com'); // 7,390
echo 'some-domain-that-doesnt-exist.com: '.get_google_results('some-domain-that-doesnt-exist'); // 0

Follow via RSS Epic Discussion

Commenter Avatar March 16 / #

Or, you could just register your site with Google Webmaster tools at: http://www.google.com/webmasters/tools.

David Walsh March 16 / #
david says:

True. I’m not a big fan of Google’s Webmaster Tools. Plus, with my grabber, I can loop through all 100+ customer domains and get the information quicker than navigating through GWT.

Commenter Avatar October 04 / #
Kenny says:

Hi, David,

I don’t know much about php, but your codes seems very usefull, can you tell me how do I put this in action?

thanks.

Commenter Avatar March 17 / #
freepuke says:

I definitely love you … ^^ … thats a quick way :-).

Commenter Avatar April 03 / #
kenny says:

Hi, David,

How do i put it in action please?

thank you.

Commenter Avatar December 29 / #
Concerned... says:

Just a quick one as I stumbled over this whilst looking around…

You do realise that this may count as using the Google servers/services in an automated fashion – which would be against G’s Terms of Service?

Be Heard!

I want to hear what you have to say! Share your comments and questions below.

Name*:
Email*:
Website:  


© David Walsh 2007-2010. Contact David Walsh. Powered by the remarkable MooTools javascript framework.