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
  ?>

Liked It? Get Free updates in your Email

Delivered by feedburner

18 thoughts on “PHP script to count Google backlinks and Indexed Pages

  1. Nitin Gupta
    #

    BackLinks are not working, might be version needs to be changed

    Reply
  2. Alya Soraya
    #

    I have tried to do the same thing as what you did but it doesn’t work

    Reply
  3. majid
    #

    Thanks Dear,
    your script is the first correct script…
    Thanks…

    Reply
  4. Randeep Hooda
    #

    does it also show dofollow and nofollow ???

    Reply
  5. stach
    #

    to get Google backlinks count need replace url

    $url="http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=link:%20".$domain."&filter=0";

    Reply
  6. miilli
    #

    thankyou sir… this script helped me a lot

    Reply
    1. pakgaol
      #

      nice method, I will try to my site… thanks anyway

      Reply
  7. ulexu
    #

    Hello,
    How do i use these scripts?
    In which file do i put it?

    Reply
  8. ani
    #

    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 ! ‘ ;
    }

    Reply
  9. Saeed
    #

    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?

    Reply
  10. Liems Eoling
    #

    Do I need additional IP’s or proxy to run this script sir?

    Reply
    1. O.o
      #

      Hi, @Atef
      I have same problems.
      Do you have any solutions?

      Reply
  11. Drew
    #

    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!

    Reply
  12. Lucas
    #

    Thanks but not working

    Reply

Leave a Reply

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