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

By  on  

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

Recent Features

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

  • By
    JavaScript Promise API

    While synchronous code is easier to follow and debug, async is generally better for performance and flexibility. Why "hold up the show" when you can trigger numerous requests at once and then handle them when each is ready?  Promises are becoming a big part of the JavaScript world...

Incredible Demos

  • By
    DWRequest: MooTools 1.2 AJAX Listener &#038; Message Display

    Though MooTools 1.2 is in its second beta stage, its basic syntax and theory changes have been hashed out. The JavaScript library continues to improve and become more flexible. Fellow DZone Zone Leader Boyan Kostadinov wrote a very useful article detailing how you can add a...

  • By
    Use Custom Missing Image Graphics Using MooTools

    Missing images on your website can make you or your business look completely amateur. Unfortunately sometimes an image gets deleted or corrupted without your knowledge. You'd agree with me that IE's default "red x" icon looks awful, so why not use your own missing image graphic? The MooTools JavaScript Note that...

Discussion

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

  2. 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.

  3. Kenny

    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.

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

  5. kenny

    Hi, David,

    How do i put it in action please?

    thank you.

  6. Concerned...

    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?

  7. Tope

    This script does not work again. Google as change their result format. PLEASE, update.

  8. To get it WORKING again:

    change the last three lines with these:

    // parse to get results
    $result = get_match(‘/About (.*) results/i’,$content);

    // split the results
    $split1 = explode(‘ ‘,$result);

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

  9. cmcm63366

    Burberry outlet Bags there, very refreshing taste of summer to use the gray model. Simple and elegant style and practical package, can be described as beautiful share. If a top luxury brand in pursuit of a vanity, it is worth the vanity bags, it is perfect, profound, because the low-key, and can track the fraud out of kitsch; while participation and practice, can not withstand scrutiny , eternal. Check it out, definitely something you like.

  10. I can get result if i give any url.
    But i want to search links so i am passing link:digg.com.
    How to get that number ?

  11. View source code from opera miniserver:source

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!