Primer de tot, per aquells qui no sapigueu de què parlo, us poso la definició de Pagerank de la Viquipèdia:
PageRank és l’algorisme que utilitza Google per determinar la posició d’una pàgina web a l’hora de fer una consulta mitjançant el seu motor de cerca. Aquest mètode mesura el seu grau d’importància de forma numèrica i permet situar els resultats més fiables en primer lloc. Alhora, reflecteix la probabilitat que hi ha de que un usuari que navega a través d’enllaços de forma aleatòria arribi a una pàgina web concreta.
Doncs bé, he trobat un script en el llenguatge de programació PHP que permet obtenir el Pagerank . L’he transformat en una classe i aquí el teniu:
<?php
/**
* PageRank Lookup class v1.1 by HM2K (update: 31/01/2007)
* based on an algorithm found here: http://pagerank.gamesaga.net/
* @author HM2K
* @version 1.1
* @see http://pagerank.gamesaga.net/
*/
class Pagerank
{
//settings - host and user agent
private static $googlehost = 'toolbarqueries.google.com';
private static $googleua = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5';
/**
* Convert a string to a 32-bit integer
*/
private 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
// refer to 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 a url
* @param string $String
*/
private function hashURL($string)
{
$check1 = self::strToNum($string, 0x1505, 0x21);
$check2 = self::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
* @param string $hashnum
*/
private function checkHash($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;
}
/**
* Return the pagerank checksum hash
* @param string $url
*/
private function getCh($url)
{
return self::checkHash(self::hashURL($url));
}
/**
* Return the pagerank figure
* @param string $url
*/
public function getPagerank($url)
{
$googlehost = self::$googlehost;
$googleua = self::$googleua;
$ch = self::getCh($url);
$fp = fsockopen($googlehost, 80, $errno, $errstr, 30);
if ($fp) {
$out = "GET /search?client=navclient-auto&ch=$ch&features=Rank&q=info:$url HTTP/1.1\r\n";
//echo "<pre>$out</pre>\n"; //debug only
$out .= "User-Agent: $googleua\r\n";
$out .= "Host: $googlehost\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
//$pagerank = substr(fgets($fp, 128), 4); //debug only
//echo $pagerank; //debug only
while (!feof($fp)) {
$data = fgets($fp, 128);
//echo $data;
$pos = strpos($data, "Rank_");
if($pos === false){} else{
$pr=substr($data, $pos + 9);
$pr=trim($pr);
$pr=str_replace("\n",'',$pr);
return $pr;
}
}
//else { echo "$errstr ($errno)<br />\n"; } //debug only
fclose($fp);
}
}
/**
* Generate the graphical pagerank
* @param $url
* @param $width
* @param $method
*/
public function getGraphicalPagerank($url,$width=40,$method='style')
{
if (!preg_match('/^(http:\/\/)?([^\/]+)/i', $url))
{
$url='http://'.$url;
}
$pr=self::getPagerank($url);
$pagerank="PageRank: $pr/10";
//The (old) image method
if ($method == 'image') {
$prpos=$width*$pr/10;
$prneg=$width-$prpos;
$html='<img src="http://www.google.com/images/pos.gif" width='.$prpos.' height=4 border=0 alt="'.$pagerank.'"><img src="http://www.google.com/images/neg.gif" width='.$prneg.' height=4 border=0 alt="'.$pagerank.'">';
}
//The pre-styled method
if ($method == 'style') {
$prpercent=100*$pr/10;
$html='<div style="position: relative; width: '.$width.'px; padding: 0; background: #D9D9D9;"><strong style="width: '.$prpercent.'%; display: block; position: relative; background: #5EAA5E; text-align: center; color: #333; height: 4px; line-height: 4px;"><span></span></strong></div>';
}
$out='<a href="'.$url.'" title="'.$pagerank.'">'.$html.'</a>';
return $out;
}
}
Per usar-la només cal el següent:
require 'Pagerank.php';
echo Pagerank::getPagerank('google.com');
L’exemple mostrarà: 10
Podeu baixar-vos el fitxer amb el codi font fent clic aquí.
Que aprofiti!
loading...
