Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a PHP script to find the number of indexed pages in Google?

Tags:

php

I need to find the number of indexed pages in google for a specific domain name, how do we do that through a PHP script?

So,

    foreach ($allresponseresults as $responseresult)
    {
        $result[] = array(
            'url' => $responseresult['url'],
            'title' => $responseresult['title'],
            'abstract' => $responseresult['content'],
        );
    }

what do i add for the estimated number of results and how do i do that? i know it is (estimatedResultCount) but how do i add that? and i call the title for example this way: $result['title'] so how to get the number and how to print the number?

Thank you :)

like image 337
user220755 Avatar asked Jan 26 '10 13:01

user220755


2 Answers

I think it would be nicer to Google to use their RESTful Search API. See this URL for an example call:

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:stackoverflow.com&filter=0

(You're interested in the estimatedResultCount value)

In PHP you can use file_get_contents to get the data and json_decode to parse it.

You can find documentation here:

http://code.google.com/apis/ajaxsearch/documentation/#fonje


Example

Warning: The following code does not have any kind of error checking on the response!

function getGoogleCount($domain) {
    $content = file_get_contents('http://ajax.googleapis.com/ajax/services/' .
        'search/web?v=1.0&filter=0&q=site:' . urlencode($domain));
    $data = json_decode($content);
    return intval($data->responseData->cursor->estimatedResultCount);
}

echo getGoogleCount('stackoverflow.com');
like image 94
Blixt Avatar answered Oct 15 '22 23:10

Blixt


You'd load http://www.google.com/search?q=domaingoeshere.com with cURL and then parse the file looking for the results <p id="resultStats" bit.

You'd have the resulting html stored in a variable $html and then say something like

$arr = explode('<p id="resultStats"'>, $html);
$bottom = $arr[1];
$middle = explode('</p>', $bottom);

Please note that this is untested and a very rough example. You'd be better off parsing the html with a dedicated parser or matching the line with regular expressions.

like image 42
Josh K Avatar answered Oct 15 '22 23:10

Josh K