Home > PHP > PHP get Google backlinks for domain/site

PHP get Google backlinks for domain/site

February 20th, 2010

Function below allow you to get count of Google backlinks for requested page

function getGoogleOuterLinksCount($url) {
 $domain = preg_replace('/^www\./','',array_shift(split('/',$url)));

 /*  I strongly recommend you to use www.google.<you country zone> version of Google */
 $html = "http://www.google.com/search?q=link:".$domain."&hl=en"; 

 $content = getRemoteFile($html);
 if (preg_match('/<div id=resultStats>(\d+) results/i',$content,$arr)) {
   $t = $arr[1];
   $t = str_replace(' ','',$t);
   $t = str_replace(',','',$t);
   return (int)$t;
 } else
   return 0;
}

function getRemoteFile($url) {
   $ch = curl_init($url);
   curl_setopt($ch, CURLOPT_HEADER, 1);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

   $data = curl_exec($ch);

   if(curl_errno($ch)) {
      echo "Error!  Error code:".curl_errno($ch)." Error:".curl_error($ch);
      curl_close($ch);
      return false;
   }

   list($header, $data) = preg_split("/\r?\n\r?\n/", $data, 2);

   return $data;
}
Categories: PHP Tags: ,
Comments are closed.