收藏本页 | 网站地图 | 投稿指南
 
 
当前位置:首页 >> 学院首页 >> 程序开发 >> PHP >>

用PHP实现Google PR查询

放大字体  缩小字体  At: 2005-12-08 22:58  By: master8 转载 来源: http://www.zeali.net/blog/entry.php?id=78


转载请保留本文原始链接:http://www.zeali.net/blog/entry.php?id=78
关键字: google PageRank, PHP, 源代码
Google本身提供了查询指定的url的PageRank值的接口,知道了这个接口,就可以很容易编写脚本在页面上实现这一功能,而无需再依赖google toolbar才能进行查询。本文提供了一个用PHP实现的pr查询接口。
使用很简单,只要在需要的地方

<?php
include_once("./pr.inc.php");
echo getPR($urlToQuery);
?>

即可显示出指定url的PageRank的数值。知道了这个数值再在其基础上模拟出Google Toolbar上面的图形化的pr显示也就不是难事了。实际上实现原理说白了也很简单,就是传递特定的查询参数到Google的搜索引擎,然后抓取返回的页面内容。

演示页面请参见 : Google PageRank Query

本文代码素材来源: http://forums.seochat.com/archive/t-17286/Php-Pagerank-checker

网上还有一个开源的pr状态查询的项目: http://pagerankstatus.mozdev.org/source.html , 可以从cvs上直接抓取完整的源代码(cvs用户密码 guest):

cvs -d :pserver:guest@mozdev.org:/cvs login
cvs -d :pserver:guest@mozdev.org:/cvs co pagerankstatus

一个专门提供pr显示接口的网站: http://www.prchecker.info/

------------------------------------------------------------------

pr.inc.php源文件如下:

<?php

define('GMAG', 0xE6359A60);

// url get method macro.
define('G_PR_GET_TYPE_FILE', 1); // use fopen() function
define('G_PR_GET_TYPE_SOCKET', 2); // use standard fsocketopen function

// main function to be called
function getPR($_url,$gettype=G_PR_GET_TYPE_SOCKET){
    $url = 'info:'.$_url;
    $ch = GCH(strord($url));
    $url='info:'.urlencode($_url);
    $googlePRUrl = "http://www.google.com/search?client=navclient-auto&ch=6"
                              .$ch."&ie=UTF-8&oe=UTF-8&features=Rank&q=".$url;
    $pr_str = retrieveURLContent($googlePRUrl,$gettype);
    return substr($pr_str,strrpos($pr_str, ":")+1);
}

//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 GCH($url, $length=null, $init=GMAG){
    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;
    /* all the case statements fall through */
    switch($len){
        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;
}

// return url page content or false if failed.
function retrieveURLContent($url,$gettype){
    switch($gettype){
        case G_PR_GET_TYPE_FILE:
            return retrieveURLContentByFile($url);
            break;
        default:
            return retrieveURLContentBySocket($url);
            break;
    }
}

function retrieveURLContentByFile($url){
    $fd = @fopen($url,"r");
    if(!$fd){
        return false;
    }
    $result = "";
    while($buffer = fgets($fd, 4096)) {
      $result .= $buffer;
    }
    fclose($fd);
    return $result;
}

function retrieveURLContentBySocket($url,$host="",$port=80,$timeout=30){
    if($host == ""){
        if(!($pos = strpos($url,'://'))){
            return false;
        }
        $host = substr($url,$pos+3,strpos($url,'/',$pos+3) - $pos - 3);
        $uri = substr($url,strpos($url,'/',$pos+3));
    }
    else{
        $uri = $url;
    }

    $request = "GET ".$uri." HTTP/1.0rn"
               ."Host: ".$host."rn"
               ."Accept: */*rn"
               ."User-Agent: ZealGetrn"
               ."rn";
    $sHnd = @fsockopen ($host, $port, $errno, $errstr, $timeout);
    if(!$sHnd){
        return false;
    }

    @fputs ($sHnd, $request);
   
    // Get source
    $result = "";
    while (!feof($sHnd)){
        $result .= fgets($sHnd,4096);
    }
    fclose($sHnd);
   
    $headerend = strpos($result,"rnrn");
    if (is_bool($headerend))
    {
        return $result;
    }
    else{
        return substr($result,$headerend+4);
    }
}
?>

 


 






         









 
Google
论坛精华  
 
 
  ©2005-2008 站长吧 Master8.NET All Rights Reserved 陕ICP备05010609号