victorjonsson.se

En klass skriven i PHP som hämtar google pagerank

Var ute efter en klass i php för att hämta en hemsidas page rank hos google. Det fanns en hel del kod att tillgå men det enda jag hittade i php var proceduralt så jag skrev min egen klass. Förslagsvis använder man sig av curl istället för file_get_contents och skriver funktionalitet för bättre felhantering.

Klass som hämtar google page-rank för angiven hemsida

/**
 * @desc Class for checking google page rank for websites.
 * Credit to moneypa.com/post/How-to-get-Google-page-rank-programming-in-code-%28C-PHP%29.aspx
 */
class PageRankFetcher {

    /**
     * @param string $url
     * @return numeric string
     * @throws Exception
     */
    public function getPageRank($url) {
        $hash = self::checkHash(self::hashURL($url));
        $query_url = self::buildQueryURL($hash, $url);
        $google_response = @file_get_contents($query_url);
        if (!$google_response) {
            throw new Exception($url . ' not responding');
        }
        $pos = strpos($google_response, 'Rank_');
        if ($pos !== false) {
            return substr($response, $pos + 9);
        }
        else {
            throw new Exception($url . ' could not be checked');
        }
    }

    /**
     * @param string $hash
     * @param string $url
     * @return string
     */
    private static function buildQueryURL($hash, $url) {
        return 'http://toolbarqueries.google.com/' .
               'search?client=navclient-auto&ch=' .
               $hash . '&features=Rank&q=info:' .
               $url . '&num=100&filter=0';
    }

    /**
     * @param string $str
     * @param int $check
     * @param int $magic
     * @return int
     */
    private static function strToNum($str, $check, $magic) {
        $Int32_unit = 4294967296;
        $length = strlen($str);
        for ($i = 0; $i < $length; $i++) {
            $check *= $magic;
            if ($check >= $Int32_unit) {
                $check -= $Int32_unit * (int)($check / $Int32_unit);

                if ($check < -2147483648) {
                    $check = $check + $Int32_unit;
                 }
             }  

            $check += ord($str{$i});
         }
         return $check;
    }     

    /**
    * @param string $string
    */
    private static 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);
    }     

    /**
    * @param unknown_type $hashnum
    */
    private static function checkHash($hashnum) {
        $check_byte = 0;
        $flag = 0;
        $hash_str = sprintf('%u', $hashnum);
        $length = strlen($hash_str);
        for ($i = $length - 1; $i >= 0; $i--) {
            $Re = $hash_str{$i};
            if (1 === ($flag % 2)) {
                $Re += $Re;
                $Re = (int)($Re / 10) + ($Re % 10);
            }
            $check_byte += $Re;
            $flag++;
        }
        $check_byte %= 10;
        if (0 !== $check_byte) {
            $check_byte = 10 - $check_byte;
            if (1 === ($flag % 2)) {
                if (1 === ($check_byte % 2)) {
                    $check_byte += 9;
                }
                $check_byte >>= 1;
            }
        }
        return '7' . $check_byte . $hash_str;
    }
}