search
HomeBackend DevelopmentPHP TutorialGoogle PR查询接口checksum新算法_php实例

前些日子一篇N久之前的老文忽然成了被阅读的热点,检查之后才发现自己使用那段代码来做pr查询的页面已经不能正常得到URL的Page Rank值了

取而代之的是一大段“In your email, please send us the entire code displayed below”之类的Google terms_of_service错误提示信息。看来是原先的接口已经失效了。

但我装在Firefox工具栏的扩展插件SearchStatus仍然能够正常解析出每个受访页的PR值,找到 SearchStatus 的插件包解开来看源码,果然是使用了不一样的验证码生成算法,在原先的 checksum 生成之后,还需要再进行一次计算,两次演算之后得到的才是正确的ch参数。

于是拿现成的js代码改造一番之后,新的PHP版本的 Google PageRank 查询接口方法就出来了。经过本地测试之后,谁想传到服务器之后又出现了该死的 terms_of_service 错误提示。把checksum的计算过程一步步打出来,发现经过了几次右位移之后本地和服务器上的数字就不一样了。这才想到服务器是64位机,32位系统下位移之后应该被cut掉的bit在那里就活得好好的。加了个 trunkbitForce32bit 方法,对所有算术运算之后的数值进行高位屏蔽,算是搞定了64位系统下的多余位问题。结果拿到32位Linux环境下跑又不兼容了,原因是PHP在进行算术处理出现溢出时,会自动尝试将int转为float。当发生的是负数溢出时,这一操作在Windows下能正确保留精度,但在Linux下就有问题了。

下面这段代码:

$a = -4294967295;
echo dechex($a)."df250b2156c434f3390392d09b1c9563\n";
if ( $a < 0 ) $a += 4294967296;
echo dechex($a)."df250b2156c434f3390392d09b1c9563\n";

第一个echo在Windows下能够正确输出该负数低32位的补码,而在32位Linux机上输出的则是int类型所能表示的最大负数0x80000000了。只有通过取巧的方式给这个溢出的大负数加上一个超出整数范围的大整数来抵消掉溢出的部分,才能复原低32位应该有的样子。

使用这些非常规手段,终于炮制出这个更新版的兼容Linux32/Linux64/Windows的Google PR值查询接口的PHP脚本实现(含完整代码)。


Google本身提供了查询指定的url的PageRank值的接口,知道了这个接口,就可以很容易编写脚本在页面上实现这一功能,而无需再依赖google toolbar才能进行查询。本文提供了一个用PHP实现的pr查询接口。同时修正了大部分版本中存在的 Linux 操作系统及64位操作系统下无法正常生成checksum的问题。


使用很简单,只要在需要的地方

fa7dc1d850e835711a146b717700644c

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

演示页面请参见 : Google PageRank Query

本文代码素材来源: http://www.php.cn/ ;NewGCH方法实现参考于Firefox的工具栏扩展插件SearchStatus的相关代码实现。

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

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

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

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

pr.inc.php源文件如下(Updated 2008-05-04 14:29 -- Google修改了checksum的计算算法,需要在原有的GCH方法之后再套一层NewGCH方法来得到正确的checksum,同时引发的php int overflow及64位机器兼容性问题请参照以下源代码的变化部分):

<?php 
// url get method macro. 
define(&#39;G_PR_GET_TYPE_FILE&#39;, 1); // use fopen() function 
define(&#39;G_PR_GET_TYPE_SOCKET&#39;, 2); // use standard fsocketopen function 
// main function to be called 
function getPR($_url,$gettype=G_PR_GET_TYPE_SOCKET){ 
$url = &#39;info:&#39;.$_url; 
$ch = GCH(strord($url)); 
$ch = NewGCH($ch); 
$url=str_replace("_","%5F",&#39;info:&#39;.urlencode($_url)); 
$googlePRUrl = 
"http://toolbarqueries.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(&#39;8&#39;.implode(&#39;&#39;,array_fill(0,PHP_INT_SIZE*2-1,&#39;0&#39;))); 
if ($z & $a){ 
$a = ($a>>1); 
$a &= (~$z); 
$a |= hexdec(&#39;4&#39;.implode(&#39;&#39;,array_fill(0,PHP_INT_SIZE*2-1,&#39;0&#39;))); 
$a = ($a>>($b-1)); 
} 
else{ 
$a = ($a>>$b); 
} 
return $a; 
} 
// discard bits beyonds 32 bit. 
function trunkbitForce32bit($n){ 
if(PHP_INT_SIZE <= 4){ 
settype($n,&#39;float&#39;); 
if ( $n < 0 ) $n += 4294967296; 
return $n; 
} 
else{ 
$clearbit = &#39;&#39;; 
for($i=0;$i<PHP_INT_SIZE-4;$i++){ 
$clearbit .= &#39;00&#39;; 
} 
for($i=0;$i<4;$i++){ 
$clearbit .= &#39;ff&#39;; 
} 
return ($n & hexdec($clearbit)); 
} 
} 
function bigxor($m,$n){ 
//if(function_exists(&#39;gmp_init&#39;)){ 
// return floatval(gmp_strval(gmp_xor($m,$n))); 
//} 
//else{ 
return $m ^ $n; 
//} 
} 
function mix($a,$b,$c){ 
$a = trunkbitForce32bit($a); 
$b = trunkbitForce32bit($b); 
$c = trunkbitForce32bit($c); 
$a -= $b; $a = trunkbitForce32bit($a); 
$a -= $c; $a = trunkbitForce32bit($a); 
$a = bigxor($a,(zeroFill($c,13))); $a = trunkbitForce32bit($a); 
$b -= $c; $b = trunkbitForce32bit($b); 
$b -= $a; $b = trunkbitForce32bit($b); 
$b = bigxor($b,trunkbitForce32bit($a<<8)); $b = trunkbitForce32bit($b); 
$c -= $a; $c = trunkbitForce32bit($c); 
$c -= $b; $c = trunkbitForce32bit($c); 
$c = bigxor($c,(zeroFill($b,13))); $c = trunkbitForce32bit($c); 
$a -= $b;$a = trunkbitForce32bit($a); 
$a -= $c;$a = trunkbitForce32bit($a); 
$a = bigxor($a,(zeroFill($c,12)));$a = trunkbitForce32bit($a); 
$b -= $c;$b = trunkbitForce32bit($b); 
$b -= $a;$b = trunkbitForce32bit($b); 
$b = bigxor($b,trunkbitForce32bit($a<<16)); 
$c -= $a; $c = trunkbitForce32bit($c); 
$c -= $b; $c = trunkbitForce32bit($c); 
$c = bigxor($c,(zeroFill($b,5))); $c = trunkbitForce32bit($c); 
$a -= $b;$a = trunkbitForce32bit($a); 
$a -= $c;$a = trunkbitForce32bit($a); 
$a = bigxor($a,(zeroFill($c,3)));$a = trunkbitForce32bit($a); 
$b -= $c;$b = trunkbitForce32bit($b); 
$b -= $a;$b = trunkbitForce32bit($b); 
$b = bigxor($b,trunkbitForce32bit($a<<10)); 
$c -= $a; $c = trunkbitForce32bit($c); 
$c -= $b; $c = trunkbitForce32bit($c); 
$c = bigxor($c,(zeroFill($b,15))); $c = trunkbitForce32bit($c); 
return array($a,$b,$c); 
} 
function NewGCH($ch){ 
$ch = ( trunkbitForce32bit( ( $ch / 7 ) << 2 ) | 
( ( myfmod( $ch,13 ) ) & 7 ) ); 
$prbuf = array(); 
$prbuf[0] = $ch; 
for( $i = 1; $i < 20; $i++ ) 
{ 
$prbuf[$i] = $prbuf[$i-1] - 9; 
} 
$ch = GCH( c32to8bit( $prbuf ) ); 
return $ch; 
} 
function myfmod($x,$y){ 
$i = floor( $x / $y ); 
return ( $x - $i * $y ); 
} 
function c32to8bit($arr32){ 
$arr8 = array(); 
for( $i = 0; $i < count($arr32); $i++ ) { 
for( $bitOrder = $i * 4; 
$bitOrder <= $i * 4 + 3; $bitOrder++ ) { 
$arr8[$bitOrder] = $arr32[$i] & 255; 
$arr32[$i] = zeroFill( $arr32[$i], 8 ); 
} 
} 
return $arr8; 
} 
function GCH($url, $length=null){ 
if(is_null($length)) { 
$length = sizeof($url); 
} 
$init = 0xE6359A60; 
$a = 0x9E3779B9; 
$b = 0x9E3779B9; 
$c = 0xE6359A60; 
$k = 0; 
$len = $length; 
$mixo = array(); 
while( $len >= 12 ){ 
$a += ($url[$k+0] +trunkbitForce32bit($url[$k+1]<<8) 
+trunkbitForce32bit($url[$k+2]<<16) 
+trunkbitForce32bit($url[$k+3]<<24)); 
$b += ($url[$k+4] +trunkbitForce32bit($url[$k+5]<<8) 
+trunkbitForce32bit($url[$k+6]<<16) 
+trunkbitForce32bit($url[$k+7]<<24)); 
$c += ($url[$k+8] +trunkbitForce32bit($url[$k+9]<<8) 
+trunkbitForce32bit($url[$k+10]<<16) 
+trunkbitForce32bit($url[$k+11]<<24)); 
$mixo = mix($a,$b,$c); 
$a = $mixo[0]; $b = $mixo[1]; $c = $mixo[2]; 
$k += 12; 
$len -= 12; 
} 
$c += $length; 
switch( $len ) { 
case 11: 
$c += trunkbitForce32bit($url[$k+10]<<24); 
case 10: 
$c+=trunkbitForce32bit($url[$k+9]<<16); 
case 9 : 
$c+=trunkbitForce32bit($url[$k+8]<<8); 
case 8 : 
$b+=trunkbitForce32bit($url[$k+7]<<24); 
case 7 : 
$b+=trunkbitForce32bit($url[$k+6]<<16); 
case 6 : 
$b+=trunkbitForce32bit($url[$k+5]<<8); 
case 5 : 
$b+=trunkbitForce32bit($url[$k+4]); 
case 4 : 
$a+=trunkbitForce32bit($url[$k+3]<<24); 
case 3 : 
$a+=trunkbitForce32bit($url[$k+2]<<16); 
case 2 : 
$a+=trunkbitForce32bit($url[$k+1]<<8); 
case 1 : 
$a+=trunkbitForce32bit($url[$k+0]); 
} 
$mixo = mix( $a, $b, $c ); 
$mixo[2] = trunkbitForce32bit($mixo[2]); 
if( $mixo[2] < 0 ){ 
return ( 
hexdec(&#39;1&#39;. 
implode(&#39;&#39;, 
array_fill(0,PHP_INT_SIZE*2,&#39;0&#39;))) 
+ $mixo[2] ); 
} 
else{ 
return $mixo[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,&#39;://&#39;))){ 
return false; 
} 
$host = substr( $url, 
$pos+3, 
strpos($url,&#39;/&#39;,$pos+3) - $pos - 3); 
$uri = substr($url,strpos($url,&#39;/&#39;,$pos+3)); 
} 
else{ 
$uri = $url; 
} 
$request = "GET ".$uri." HTTP/1.0\r\n" 
."Host: ".$host."\r\n" 
."Accept: */*\r\n" 
."User-Agent: ZealGet\r\n" 
."\r\n"; 
$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,"\r\n\r\n"); 
if (is_bool($headerend)) 
{ 
return $result; 
} 
else{ 
return substr($result,$headerend+4); 
} 
}
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment