Google Page Rank Script - Seen it Yet?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ghank
Forum Commoner
Posts: 35
Joined: Tue Apr 06, 2004 3:21 pm

Google Page Rank Script - Seen it Yet?

Post by ghank »

feyd | You've been here long enough to know to use

Code: Select all

tags when posting code. [b]LAST WARNING[/b] Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I have seen this a couple places now and found a free public script.  But I cannot get it to work.  Has anybody implemented this yet?  This would be nice to have to show URL Page Ranks on my site.  But running the script returns nothing.  I copied his html form but it returns nothing also.  Any ideas?

This guy has the code and has it working on his site:

[url]http://www.zenitram.th4y.com/pagerank/[/url]

Here is the code:

Code: Select all

<?php
// Google PageRank Calculator function by ZeNiTRaM - version 0.1
// Licensed under the GPL License
// eMail me at zenitram [AT] dubmail.net
// Uses code from The Google Checksum Calculator, by Alex Stapleton, Andy Doctorow, Vijay "Cyberax" Bhatter, and a few others, licensed under the public domain (http://www.mobileread.com/forums/showpo ... stcount=87) and XMLize.php by Hans Anderson (http://www.hansanderson.com/php/xml/).
// Use: $pagerank = GetPagerank("http://www.google.es");
// GetPagerank returns a STRING with the Pagerank number directly from Google

//	S	P	A	N	I	S	H-----------------------------------------------------------------------------------
// Calculadora Google Pagerank por ZeNiTRaM - version 0.1
// Licenciado bajo la licencia GPL.
// Contacta conmigo en zenitram [AT] dubmail.net
// Usa codigo de The Google Checksum Calculator, por Alex Stapleton, Andy Doctorow, Vijay "Cyberax" Bhatter, y unos cuantos otros mas, licenciado bajo dominio publico (http://www.mobileread.com/forums/showpo ... stcount=87) y XMLize.php por Hans Anderson (http://www.hansanderson.com/php/xml/).




/*
Changelog
0.1:	Started counting versions and tidied all the code. Maybe I will add some new functions to get more information from Google. 
	He empezado a contar versiones y limpiado un poco todo el codigo. Quizas añada nuevas funciones para conseguir mas informacion de Google.
*/


// Function GetPagerank
function GetPagerank($urlo) {
define('GOOGLE_MAGIC', 0xE6359A60);

$url = 'info:'.$urlo; 
$ch = GoogleCH(strord($url)); 
$chf = "6$ch"; 

$file = file("http://www.google.com/search?client=navclient-auto&ch=$chf&q=$url");
$file_str = implode("", $file);
$xml = xmlize($file_str); 

$pr = $xml["GSP"]["#"]["RES"][0]["#"]["R"][0]["#"]["RK"][0]["#"];
if(!$pr) $pr = 0;
return $pr;
}

//Starts code from XMLize.php
function xmlize($data, $WHITE=1) {

    $data = trim($data);
    $vals = $index = $array = array();
    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $WHITE);
    if ( !xml_parse_into_struct($parser, $data, $vals, $index) )
    {
	die(sprintf("XML error: %s at line %d",
                    xml_error_string(xml_get_error_code($parser)),
                    xml_get_current_line_number($parser)));

    }
    xml_parser_free($parser);

    $i = 0; 

    $tagname = $vals[$i]['tag'];
    if ( isset ($vals[$i]['attributes'] ) )
    {
        $array[$tagname]['@'] = $vals[$i]['attributes'];
    } else {
        $array[$tagname]['@'] = array();
    }

    $array[$tagname]["#"] = xml_depth($vals, $i);

    return $array;
}

/* 
 *
 * You don't need to do anything with this function, it's called by
 * xmlize.  It's a recursive function, calling itself as it goes deeper
 * into the xml levels.  If you make any improvements, please let me know.
 *
 *
 */

function xml_depth($vals, &$i) { 
    $children = array(); 

    if ( isset($vals[$i]['value']) )
    {
        array_push($children, $vals[$i]['value']);
    }

    while (++$i < count($vals)) { 

        switch ($vals[$i]['type']) { 

           case 'open': 

                if ( isset ( $vals[$i]['tag'] ) )
                {
                    $tagname = $vals[$i]['tag'];
                } else {
                    $tagname = '';
                }

                if ( isset ( $children[$tagname] ) )
                {
                    $size = sizeof($children[$tagname]);
                } else {
                    $size = 0;
                }

                if ( isset ( $vals[$i]['attributes'] ) ) {
                    $children[$tagname][$size]['@'] = $vals[$i]["attributes"];
                }

                $children[$tagname][$size]['#'] = xml_depth($vals, $i);

            break; 


            case 'cdata':
                array_push($children, $vals[$i]['value']); 
            break; 

            case 'complete': 
                $tagname = $vals[$i]['tag'];

                if( isset ($children[$tagname]) )
                {
                    $size = sizeof($children[$tagname]);
                } else {
                    $size = 0;
                }

                if( isset ( $vals[$i]['value'] ) )
                {
                    $children[$tagname][$size]["#"] = $vals[$i]['value'];
                } else {
                    $children[$tagname][$size]["#"] = '';
                }

                if ( isset ($vals[$i]['attributes']) ) {
                    $children[$tagname][$size]['@']
                                             = $vals[$i]['attributes'];
                }			

            break; 

            case 'close':
                return $children; 
            break;
        } 

    } 

	return $children;

}


/* function by acebone@f2s.com, a HUGE help!
 *
 * this helps you understand the structure of the array xmlize() outputs
 *
 * usage:
 * traverse_xmlize($xml, 'xml_');
 * print '<pre>' . implode("", $traverse_array . '</pre>';
 *
 *
 */ 

function traverse_xmlize($array, $arrName = "array", $level = 0) {

    foreach($array as $key=>$val)
    {
        if ( is_array($val) )
        {
            traverse_xmlize($val, $arrName . "[" . $key . "]", $level + 1);
        } else {
            $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . ""\n";
        }
    }

    return 1;

}
//Ends code from XMLize.php

//Starts code from The Google Checksum Calculator
//unsigned shift right
function zeroFill($a, $b)
{
    $z = hexdec(80000000);
        if ($z & $a)
        {
            $a = ($a>>1);
            $a &= (~$z);
            $a |= 0x40000000;
            $a = ($a>>($b-1));
        }
        else
        {
            $a = ($a>>$b);
        }
        return $a;
}


function mix($a,$b,$c) {
  $a -= $b; $a -= $c; $a ^= (zeroFill($c,13));
  $b -= $c; $b -= $a; $b ^= ($a<<8);
  $c -= $a; $c -= $b; $c ^= (zeroFill($b,13));
  $a -= $b; $a -= $c; $a ^= (zeroFill($c,12));
  $b -= $c; $b -= $a; $b ^= ($a<<16);
  $c -= $a; $c -= $b; $c ^= (zeroFill($b,5));
  $a -= $b; $a -= $c; $a ^= (zeroFill($c,3));  
  $b -= $c; $b -= $a; $b ^= ($a<<10);
  $c -= $a; $c -= $b; $c ^= (zeroFill($b,15));
  
  return array($a,$b,$c);
}

function GoogleCH($url, $length=null, $init=GOOGLE_MAGIC) {
    if(is_null($length)) {
        $length = sizeof($url);
    }
    $a = $b = 0x9E3779B9;
    $c = $init;
    $k = 0;
    $len = $length;
    while($len >= 12) {
        $a += ($url[$k+0] +($url[$k+1]<<8) +($url[$k+2]<<16) +($url[$k+3]<<24));
        $b += ($url[$k+4] +($url[$k+5]<<8) +($url[$k+6]<<16) +($url[$k+7]<<24));
        $c += ($url[$k+8] +($url[$k+9]<<8) +($url[$k+10]<<16)+($url[$k+11]<<24));
        $mix = mix($a,$b,$c);
        $a = $mix[0]; $b = $mix[1]; $c = $mix[2];
        $k += 12;
        $len -= 12;
    }

    $c += $length;
    switch($len)              /* all the case statements fall through */
    {
        case 11: $c+=($url[$k+10]<<24);
        case 10: $c+=($url[$k+9]<<16);
        case 9 : $c+=($url[$k+8]<<8);
          /* the first byte of c is reserved for the length */
        case 8 : $b+=($url[$k+7]<<24);
        case 7 : $b+=($url[$k+6]<<16);
        case 6 : $b+=($url[$k+5]<<8);
        case 5 : $b+=($url[$k+4]);
        case 4 : $a+=($url[$k+3]<<24);
        case 3 : $a+=($url[$k+2]<<16);
        case 2 : $a+=($url[$k+1]<<8);
        case 1 : $a+=($url[$k+0]);
         /* case 0: nothing left to add */
    }
    $mix = mix($a,$b,$c);
    /*-------------------------------------------- report the result */
    return $mix[2];
}

//converts a string into an array of integers containing the numeric value of the char
function strord($string) {
    for($i=0;$i<strlen($string);$i++) {
        $result[$i] = ord($string{$i});
    }
    return $result;
}
//End code from The Google Checksum Calculator
?>

feyd | You've been here long enough to know to use

Code: Select all

tags when posting code. [b]LAST WARNING[/b] Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Post by WaldoMonster »

It works for me :P
For this site it returns 7 and that is exact the same number that the Google toolbar returns.

Code: Select all

<?php
echo 'Google PageRank: ' . GetPagerank('forums.devnetwork.net');
?>
ghank
Forum Commoner
Posts: 35
Joined: Tue Apr 06, 2004 3:21 pm

Post by ghank »

Thanks for the reply. But I am unclear how to set it up. Yep, I am a new to php.

Edit: Wait, ok I added your code and it spit out the PR. Hey, thanks!!!!!!!!!
ghank
Forum Commoner
Posts: 35
Joined: Tue Apr 06, 2004 3:21 pm

Post by ghank »

I wrote a php script to check all of my links I have in a mysql database. I tried using a mysql_fetch_array that has an include statement which has that code but I get the error on line 25:

Fatal error: Cannot redeclare getpagerank() (previously declared in ...

I guess you cannot loop that script and reuse the variable getpagerank. Anybody know how to do it?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're including the function definition multiple times.
ghank
Forum Commoner
Posts: 35
Joined: Tue Apr 06, 2004 3:21 pm

Post by ghank »

Yah, just figured it out. Basically don't loop the whole script, just the ending echo part. Thanks!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I'm confused about page rank.....

what is it.. how is it judged.
ghank
Forum Commoner
Posts: 35
Joined: Tue Apr 06, 2004 3:21 pm

Post by ghank »

It's a huge algorithm Google invented. Basically the more popular your page is, the higher it scores on a scale of 0 - 10. The Google toolbar shows this but now it's been cracked basically and you can find it other ways like this:

http://www.rankwhere.com/google-page-rank.php
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Post by WaldoMonster »

The Google Pagerank Algorithm and How It Works:
http://www.iprcom.com/papers/pagerank/
ghank
Forum Commoner
Posts: 35
Joined: Tue Apr 06, 2004 3:21 pm

Post by ghank »

But I found a problem, when the URL includes a plus sign (+), it returns the following error: XML error: no element found at line 1

I changed the + to it's hex equivalent %2b and it does the same. Any ideas on how to fix it? Most people only want to check a domain PR, but I'd like to check individual pages some which have plus signs.
Post Reply