PHP GooGle PageRank程式

<?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/showpost.php?p=7769&postcount=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/showpost.php?p=7769&postcount=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&ntilde;ada nuevas funciones para conseguir mas informacion de Google.
*/

// Function GetPagerank
$a = GetPagerank(‘http://tw.yahoo.com/’);
echo $a;
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
?>