Error: Could not fetch http://thedevnet.com/wp-content/plugins/gd-star-rating/css/gdsr.css.php?t=0&s=a10i10m20k20c05r05%23121620243046%23121620243240%23s1pchristmas%23s1pcrystal%23s1pdarkness%23s1poxygen%23s1goxygen_gif%23s1pplain%23s1ppumpkin%23s1psoft%23s1pstarrating%23s1pstarscape%23t1pclassical%23t1pstarrating%23t1gstarrating_gif&ver=1.7.2 for caching. You might need to exclude this file in WP Minify options. The Developer Network – Get Google Page Rank of a Site

Get Google Page Rank of a Site

Quite often as a webmaster we like to do trades with other webmasters based on the mysterious Google pagerank algorithm. Usually this number is between 0-10. Great for automating trade scripts.

Snippet:

  1. <?
  2.  // This snippet will return the google pagerank for a given site
  3.  // http://thedevnet.com/php/snippets/get-google-page-rank-of-a-site/
  4.  // usage: $pagerank = getpagerank('http://www.google.com');
  5.  
  6.  function StrToNum($Str, $Check, $Magic)
  7.  {
  8.   $Int32Unit = 4294967296; // 2^32
  9.  
  10.   $length = strlen($Str);
  11.   for ($i = 0; $i < $length; $i++)
  12.   {
  13.    $Check *= $Magic;
  14.    
  15.    // If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31),
  16.    // the result of converting to integer is undefined
  17.    // refer to http://www.php.net/manual/en/language.types.integer.php
  18.    if ($Check >= $Int32Unit)
  19.    {
  20.     $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
  21.     //if the check less than -2^31
  22.     $Check = ($Check < -2147483648) ? ($Check + $Int32Unit) : $Check;
  23.    }
  24.    
  25.    $Check += ord($Str{$i});
  26.   }
  27.   return $Check;
  28.  }
  29.  
  30.  
  31.  
  32.  // Genearate a hash for a url
  33.  
  34.  function HashURL($String)
  35.  {
  36.   $Check1 = StrToNum($String, 0×1505, 0×21);
  37.   $Check2 = StrToNum($String, 0, 0×1003F);
  38.  
  39.   $Check1>>=2;
  40.   $Check1=(($Check1 >> 4) & 0×3FFFFC0 ) | ($Check1 & 0×3F);
  41.   $Check1=(($Check1 >> 4) & 0×3FFC00 ) | ($Check1 & 0×3FF);
  42.   $Check1=(($Check1 >> 4) & 0×3C000 ) | ($Check1 & 0×3FFF);
  43.  
  44.   $T1 = (((($Check1 & 0×3C0) << 4) | ($Check1 & 0×3C)) <<2 ) | ($Check2 & 0xF0F );
  45.   $T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0×3C00)) << 0xA) | ($Check2 & 0xF0F0000 );
  46.  
  47.   return ($T1 | $T2);
  48.  }
  49.  
  50.  
  51.  
  52.  // generate a checksum for the hash string
  53.  function CheckHash($Hashnum)
  54.  {
  55.   $CheckByte=0;
  56.   $Flag=0;
  57.  
  58.   $HashStr=sprintf('%u', $Hashnum);
  59.   $length=strlen($HashStr);
  60.  
  61.   for($i=$length-1; $i>=0; $i)
  62.   {
  63.    $Re=$HashStr{$i};
  64.    if(1===($Flag % 2))
  65.    {
  66.     $Re+=$Re;
  67.     $Re=(int)($Re/10)+($Re%10);
  68.    }
  69.    $CheckByte+=$Re;
  70.    $Flag++;
  71.   }
  72.  
  73.   $CheckByte %= 10;
  74.   if (0 !== $CheckByte)
  75.   {
  76.    $CheckByte=10-$CheckByte;
  77.    if (1 === ($Flag % 2) )
  78.    {
  79.     if (1 === ($CheckByte % 2))
  80.     {
  81.      $CheckByte += 9;
  82.     }
  83.     $CheckByte >>= 1;
  84.    }
  85.   }
  86.  
  87.   return '7'.$CheckByte.$HashStr;
  88.  }
  89.  
  90.  function getpagerank($url)
  91.  {
  92.   $fp = fsockopen("toolbarqueries.google.com", 80, $errno, $errstr, 30);
  93.   if(!$fp)
  94.   {
  95.    echo "$errstr ($errno)
  96.   \n";
  97.   } else {
  98.    $out="GET /search?client=navclient-auto&ch=".CheckHash(HashURL($url))."&features=Rank&q=info:".$url."&num=100&filter=0 HTTP/1.1\r\n";
  99.    $out.="Host: toolbarqueries.google.com\r\n";
  100.    $out.="User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114-big; Windows XP 5.1)\r\n";
  101.    $out.="Connection: Close\r\n\r\n";
  102.    
  103.    fwrite($fp, $out);
  104.    
  105.    while(!feof($fp))
  106.    {
  107.     $data=fgets($fp, 128);
  108.     $pos=strpos($data, "Rank_");
  109.     if($pos===false)
  110.     {}
  111.     else
  112.     {
  113.      $pagerank = substr($data, $pos + 9);
  114.      return $pagerank;
  115.     }
  116.    }
  117.    fclose($fp);
  118.   }
  119.  }
  120. ?>

Example:

  1. <?
  2.  $pagerank = getpagerank('http://www.google.com');
  3. ?>
GD Star Rating
loading...

Originally posted 2009-10-17 01:10:44.

Popularity: 12%

Posted by irbobo   @   18 January 2010
Tags : ,

Like this post? Share it!

RSS Digg Twitter StumbleUpon Delicious Technorati Facebook

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

You must be logged in to post a comment.

Previous Post
« Simple PHP Caching Snippet
Next Post
Putting AJAX Into Your PHP »
Powered by Wordpress   |   Lunated designed by ZenVerse