PHP script to count Google backlinks and Indexed Pages

To get backlinks and indexed page count from Google most webmasters use scrapping Google search page. It is not a good idea because it fails some time and very slow.

So we can use Google Ajax search API instead of scrapping whole page. It will return data in JSON object which is much smaller then html page.
Although Ajax search API is rate limited to 100 queries per IP per Day but still it’s a good way.

PHP code to get Google backlinks count

  <?
  function GoogleBL($domain){
  $url="http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=link:".$domain."&filter=0";
  $ch=curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt ($ch, CURLOPT_HEADER, 0);
  curl_setopt ($ch, CURLOPT_NOBODY, 0);
  curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  $json = curl_exec($ch);
  curl_close($ch);
  $data=json_decode($json,true);
  if($data['responseStatus']==200)
  return $data['responseData']['cursor']['resultCount'];
  else
  return false;
  }
  ?>

PHP code to get Google Indexed Page count

  <?
  function GoogleIP($domain){
  $url="http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:".$domain."&filter=0";
  $ch=curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt ($ch, CURLOPT_HEADER, 0);
  curl_setopt ($ch, CURLOPT_NOBODY, 0);
  curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  $json = curl_exec($ch);
  curl_close($ch);
  $data=json_decode($json,true);
  if($data['responseStatus']==200)
  return $data['responseData']['cursor']['resultCount'];
  else
  return false;
  }
  ?>

How to use?

Both function take domain name as parameter and will return backlink or indexed page count on success or Boolean false on error.

  <?
  $domain="toolspot.org"; //your domain name
  echo GoogleBL($domain); //get backlinks
  echo GoogleIP($domain); //get indexed page
  ?>

Published by Sunny

A Mechanical engineer by education and web developer by passion and profession. He lives in Lucknow, India. Currently earning his livings by hitting keyboad. ;)

Join the Conversation

17 Comments

  1. Hi,
    This is my code. and it is giving me “No result”. Can you please help me whats wrong here –

    $domain = ‘ayunutrients.com’ ; // Url of your desired site to check backlinks.
    $url=”http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=link:”.$domain.”&filter=0″;
    $ch=curl_init(); // cURL started here
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER[‘HTTP_USER_AGENT’]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt ($ch, CURLOPT_HEADER, 0);
    curl_setopt ($ch, CURLOPT_NOBODY, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    $kv_json = curl_exec($ch);
    curl_close($ch);
    $results=json_decode($kv_json,true);
    if($results[‘responseStatus’]==200) {
    echo $results[‘responseData’][‘cursor’][‘resultCount’];
    } else {
    echo ‘ Sorry No reults ! ‘ ;
    }

  2. Kindly anyone can tell me that it just returns true or false i need to return value of counted google indexed pages in integer value how i could do that?

  3. Hi! Can this be done using Google Apps Script instead of PHP so the data can be added to a Google spreadsheet? Or no?

    Thanks!

Leave a comment

Your email address will not be published. Required fields are marked *