<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>maduixa &#187; seo</title>
	<atom:link href="http://blog.jau.cat/tag/seo/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jau.cat</link>
	<description>Blog personal d&#039;en minterior amb gust a maduixa</description>
	<lastBuildDate>Sun, 16 Oct 2011 22:02:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Com obtenir el Pagerank (PR) d&#8217;una uri des de PHP</title>
		<link>http://blog.jau.cat/2010/02/12/com-obtenir-el-pagerank-pr-duna-uri-des-de-php/</link>
		<comments>http://blog.jau.cat/2010/02/12/com-obtenir-el-pagerank-pr-duna-uri-des-de-php/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 17:11:24 +0000</pubDate>
		<dc:creator>minterior</dc:creator>
				<category><![CDATA[Programari]]></category>
		<category><![CDATA[com es fa]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[seo]]></category>
		<category><![CDATA[utilitats]]></category>

		<guid isPermaLink="false">http://blog.jau.cat/?p=206</guid>
		<description><![CDATA[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...]]></description>
			<content:encoded><![CDATA[<p>Primer de tot, per aquells qui no sapigueu de què parlo, us poso la <a href="http://ca.wikipedia.org/wiki/PageRank">definició de Pagerank de la Viquipèdia</a>:</p>
<p>﻿﻿<strong>PageRank</strong> és l&#8217;<a title="Algorisme" href="http://ca.wikipedia.org/wiki/Algorisme">algorisme</a> que utilitza Google per determinar la posició d&#8217;una pàgina web a l&#8217;hora de fer una consulta mitjançant el seu motor de cerca. Aquest mètode mesura el seu grau d&#8217;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&#8217;enllaços de forma aleatòria arribi a una pàgina web concreta.</p>
<p>Doncs bé, he trobat un script en el llenguatge de programació PHP que permet obtenir el Pagerank . L&#8217;he transformat en una classe i aquí el teniu:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?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 &lt; $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 &gt;= $Int32Unit) {
                $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
                //if the check less than -2^31
                $Check = ($Check &lt; -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 &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);
    }

    /**
     * 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 &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;
    }

    /**
     * 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 = &quot;GET /search?client=navclient-auto&amp;ch=$ch&amp;features=Rank&amp;q=info:$url HTTP/1.1\r\n&quot;;
            //echo &quot;&lt;pre&gt;$out&lt;/pre&gt;\n&quot;; //debug only
            $out .= &quot;User-Agent: $googleua\r\n&quot;;
            $out .= &quot;Host: $googlehost\r\n&quot;;
            $out .= &quot;Connection: Close\r\n\r\n&quot;;

            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, &quot;Rank_&quot;);
                if($pos === false){} else{
                    $pr=substr($data, $pos + 9);
                    $pr=trim($pr);
                    $pr=str_replace(&quot;\n&quot;,'',$pr);
                    return $pr;
                }
            }
            //else { echo &quot;$errstr ($errno)&lt;br /&gt;\n&quot;; } //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=&quot;PageRank: $pr/10&quot;;

        //The (old) image method
        if ($method == 'image') {
            $prpos=$width*$pr/10;
            $prneg=$width-$prpos;
            $html='&lt;img src=&quot;http://www.google.com/images/pos.gif&quot; width='.$prpos.' height=4 border=0 alt=&quot;'.$pagerank.'&quot;&gt;&lt;img src=&quot;http://www.google.com/images/neg.gif&quot; width='.$prneg.' height=4 border=0 alt=&quot;'.$pagerank.'&quot;&gt;';
        }
        //The pre-styled method
        if ($method == 'style') {
            $prpercent=100*$pr/10;
            $html='&lt;div style=&quot;position: relative; width: '.$width.'px; padding: 0; background: #D9D9D9;&quot;&gt;&lt;strong style=&quot;width: '.$prpercent.'%; display: block; position: relative; background: #5EAA5E; text-align: center; color: #333; height: 4px; line-height: 4px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;';
        }

        $out='&lt;a href=&quot;'.$url.'&quot; title=&quot;'.$pagerank.'&quot;&gt;'.$html.'&lt;/a&gt;';
        return $out;
    }

}
</pre>
<p>Per usar-la només cal el següent:</p>
<pre class="brush: php; title: ; notranslate">
require 'Pagerank.php';

echo Pagerank::getPagerank('google.com');
</pre>
<p>L&#8217;exemple mostrarà: 10</p>
<p>Podeu baixar-vos el fitxer amb el codi font fent clic <a href="http://blog.jau.cat/wp-content/uploads/2010/02/Pagerank.phps">aquí</a>.</p>
<p>Que aprofiti!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jau.cat/2010/02/12/com-obtenir-el-pagerank-pr-duna-uri-des-de-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

