How to Create Google Page Rank Script Using PHP

google-pagerank

Page rank is created by Google to rank website based on popularity. Now you can create your own page rank look up script using PHP. I get this script from phurix.net website and modify it to have security code. For original source code, please check https://github.com/phurix/pagerank

First of all, we are going to create HTML form to capture URL and security code.

html-form

This is the index.php file contains the HTML form:

<?php
session_start();

/**
* PageRank Lookup (Based on Google Toolbar for Internet Explorer)
*
* @copyright 2011 HM2K <hm2k@php.net>
* @link http://pagerank.phurix.net/
* @author James Wade <hm2k@php.net>
* @version $Revision: 1.5 $
* @require PHP 4.3.0 (file_get_contents)
* @updated 06/10/11
*/

class PageRank {

/**
* @var string $host The Google hostname for PageRank
*/
var $host='toolbarqueries.google.com';

/**
* Returns the PageRank of a URL
*
* @param string $q The query/URL
* @param string $context A stream_context_create() context resource (optional).
*
* @return string
*/
function PageRank ($q,$context=NULL) {
$ch=$this->checksum($this->makehash($q));
$url='http://%s/tbr?client=navclient-auto&ch=%s&features=Rank&q=info:%s';
$url=sprintf($url,$this->host,$ch,$q);
$get_data = file_get_contents($url,false,$context);
$pr = explode(":", $get_data);
return $pr[2];
}

// Convert a string to a 32-bit integer
function strtonum($str, $check, $magic) {
$int32unit = 4294967296; // 2^32
$length = strlen($str);
for ($i = 0; $i < $length; $i++) {
$check *= $magic;
/* If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31),
* the result of converting to integer is undefined.
* @see http://www.php.net/manual/en/language.types.integer.php
*/
if ($check >= $int32unit) {
$check = ($check - $int32unit * (int) ($check / $int32unit));
//if the check less than -2^31
$check = ($check < -2147483648) ? ($check + $int32unit) : $check;
}
$check += ord($str{$i});
}
return $check;
}

// Genearate a hash for query
function makehash($string) {
$check1 = $this->strtonum($string, 0x1505, 0x21);
$check2 = $this->strtonum($string, 0, 0x1003f);
$check1 >>= 2;
$check1 = (($check1 >> 4) & 0x3ffffc0 ) | ($check1 & 0x3f);
$check1 = (($check1 >> 4) & 0x3ffc00 ) | ($check1 & 0x3ff);
$check1 = (($check1 >> 4) & 0x3c000 ) | ($check1 & 0x3fff);
$t1 = (((($check1 & 0x3c0) << 4) | ($check1 & 0x3c)) <<2 ) | ($check2 & 0xf0f);
$t2 = (((($check1 & 0xffffc000) << 4) | ($check1 & 0x3c00)) << 0xa) | ($check2 & 0xf0f0000);
return ($t1 | $t2);
}

// Genearate a checksum for the hash string
function checksum($hashnum) {
$checkbyte = 0;
$flag = 0;
$hashstr = sprintf('%u', $hashnum) ;
$length = strlen($hashstr);
for ($i = $length - 1; $i >= 0; $i --) {
$re = $hashstr{$i};
if (1 === ($flag % 2)) {
$re += $re;
$re = (int)($re / 10) + ($re % 10);
}
$checkbyte += $re;
$flag ++;
}
$checkbyte %= 10;
if (0 !== $checkbyte) {
$checkbyte = 10 - $checkbyte;
if (1 === ($flag % 2) ) {
if (1 === ($checkbyte % 2)) {
$checkbyte += 9;
}
$checkbyte >>= 1;
}
}
return '7'.$checkbyte.$hashstr;
}
}

?>

<form method="post">
<table>
<tr>
<td>Website URL</td>
<td><input type=text name=q>&nbsp;<i>example: colekcolek.com</i></td>
</tr>
<tr>
<td valign="top">Security Code</td>
<td><input type=text name=c size="5">&nbsp;<img src="code.php" style="vertical-align:bottom"></td>
</tr>
<tr>
<td></td>
<td><input type=submit value="Check Page Rank"></td>
</tr>
</table>
</form>

<?php
$website_url = htmlentities($_POST['q']);
$website_url = addslashes($website_url);

if (isset($_POST['q']) && !empty($_POST['c'])) {
if ($_SESSION['rx_code'] == $_POST['c']) {

$pr = new PageRank($website_url);
$get_pr = $pr->PageRank($website_url);

?>
<table>
<tr>
<td>Website URL</td>
<td> : <?php echo $website_url?></td>
</tr>
<tr>
<td>Page Rank</td>
<td> : <?php echo $get_pr?>/10</td>
</tr>
</table>
<?php
} else {
echo "Wrong code !";
echo $_SESSION['rx_code'];
}

}  //eof
?>

And here is the code.php to display security code:

<?
header("Content-type: image/png");
session_cache_limiter('nocache');
session_start();

$rx = str_replace("0", "1", substr(md5(time()), 30));
$_SESSION['rx_code'] = $rx;

$im = ImageCreate(130,25);
$bgcolor = ImageColorAllocate($im, 255, 255, 255);
$fgcolor = ImageColorAllocate($im, 0, 0, 0);

ImageString($im, 5, 18, 5, "Code: ".$rx, $fgcolor);
ImagePng($im);

ImageDestroy($im); // free memory
?>

Now test the script, type in URL: facebook.com and security code which appears on screen and click Check Page Rank button.

Website URL : facebook.com
Page Rank : 9 /10

Update April 23, 2012:

Found another page rank script. Credit to www.w3lessons.com.

The usage is still the same: echo GetPageRank($url);


function GetPageRank($url) {
$url = strstr($url,"http://")? $url:"http://".$url;
$fp = fsockopen("toolbarqueries.google.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)&lt;br /&gt;\n";
} else {
$out = "GET /tbr?client=navclient-auto&amp;ch=".CheckHash(HashURL($url))."&amp;features=Rank&amp;q=info:".$url."&amp;num=100&amp;filter=0 HTTP/1.1\r\n";
$out .= "Host: toolbarqueries.google.com\r\n";
$out .= "User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114-big; Windows XP 5.1)\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);

while (!feof($fp)) {
$data = fgets($fp, 128);
$pos = strpos($data, "Rank_");
if($pos === false){} else{
$pagerank = substr($data, $pos + 9);
}
}
fclose($fp);
return (int)$pagerank;
}
}

function StrToNum($Str, $Check, $Magic) {
$Int32Unit = 4294967296; // 2^32
$length = strlen($Str);
for ($i = 0; $i &lt; $length; $i++) {
$Check *= $Magic;
if ($Check &gt;= $Int32Unit) {
$Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
$Check = ($Check &lt; -2147483648)? ($Check + $Int32Unit) : $Check;
}
$Check += ord($Str{$i});
}
return $Check;
}

function HashURL($String) {
$Check1 = StrToNum($String, 0x1505, 0x21);
$Check2 = StrToNum($String, 0, 0x1003F);
$Check1 &gt;&gt;= 2;
$Check1 = (($Check1 &gt;&gt; 4) &amp; 0x3FFFFC0 ) | ($Check1 &amp; 0x3F);
$Check1 = (($Check1 &gt;&gt; 4) &amp; 0x3FFC00 ) | ($Check1 &amp; 0x3FF);
$Check1 = (($Check1 &gt;&gt; 4) &amp; 0x3C000 ) | ($Check1 &amp; 0x3FFF);
$T1 = (((($Check1 &amp; 0x3C0) &lt;&lt; 4) | ($Check1 &amp; 0x3C)) &lt;&lt; 2 ) | ($Check2 &amp; 0xF0F );
$T2 = (((($Check1 &amp; 0xFFFFC000) &lt;&lt; 4) | ($Check1 &amp; 0x3C00)) &lt;&lt; 0xA) | ($Check2 &amp; 0xF0F0000 );
return ($T1 | $T2);
}

function CheckHash($Hashnum) {
$CheckByte = 0;
$Flag = 0;
$HashStr = sprintf('%u', $Hashnum) ;
$length = strlen($HashStr);
for ($i = $length - 1; $i &gt;= 0; $i --) {
$Re = $HashStr{$i};
if (1 === ($Flag % 2)) {
$Re += $Re;
$Re = (int)($Re / 10) + ($Re % 10);
}
$CheckByte += $Re;
$Flag ++;
}
$CheckByte %= 10;
if (0!== $CheckByte) {
$CheckByte = 10 - $CheckByte;
if (1 === ($Flag % 2) ) {
if (1 === ($CheckByte % 2)) {
$CheckByte += 9;
}
$CheckByte &gt;&gt;= 1;
}
}
return '7'.$CheckByte.$HashStr;
}

Filed Under: FeaturedPHPTechnologyTutorial

RSSComments (13)

Leave a Reply | Trackback URL

  1. Intivar says:

    Thanks, your code works like charm :)

  2. Thanks, its really working…

  3. Mario says:

    Script still works? I get bool FALSE

  4. Thomas says:

    Hi,
    I have testet your code. It workes perfect.

    Could u help me to change this cod tu new version?

    function get_pr($domain,$host=’toolbarqueries.google.com’,$context=NULL)
    {
    $domain = $domain.”;
    $seed = “Mining PageRank is AGAINST GOOGLE’S TERMS OF SERVICE. Yes, I’m talking to you, scammer.”;
    $result = 0×01020345;
    $len = strlen($domain);

    for ($i=0; $i> 23) & 0x1ff) | $result << 9;
    }

    $ch=sprintf('8%x', $result);
    $url='http://%s/tbr?client=navclient-auto&ch=%s&features=Rank&q=info:%s&#039;;
    $url=sprintf($url,$host,$ch,$domain);
    $pr=file_get_contents($url,false,$context);
    return $pr?substr(strrchr($pr, ':'), 1):false;
    }

  5. Joseph says:

    I’ve been wondering why google guys decided to implement a value of “Mining PageRank is AGAINST GOOGLE’S TERMS OF SERVICE. Yes, I’m talking to you, scammer.”

    What’s the sense behind the string?

    Any hints?

    Joseph.
    Author and Publisher,
    http://www.josytal.com – Free Software for Small Business.
    http://www.directorygist.com – Directory of Web Directories with Online submission tools.

    • kumkum says:

      $seed variable could be any string value. I think that’s a creative way from the programmer to deliver his message :)

  6. gib says:

    Just last week my script identical to the pr part of your script stopped working. Mine updates the PR for domains in a database and all are showing PR 0 now.

    Anyone have this problem?

  7. gib says:

    Well i decided to try your code and replaced what you have originally that does not work and it works great. Thanks for sharing.

    Gib

  8. James Wade says:

    You should report the issue here:

    https://github.com/phurix/pagerank/issues

    Rather than using an old script.

  9. stiff says:

    nice script, leave comment :D

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.