fimms
Junior Member
- Apr 30, 2009
- 179
- 59
For my private projects I've been using this PR check script with PHP, but this month Google changed they API entry point for toolbar. So I had to fix it. On same routines I also decided to add bulk checker that finds out both domain and page PR plus determines if given url exists. Given output should be more informative now.
Note: too many requests to Google server may get you temporarily banned by IP, but I haven't had any problems checking dozens of urls per one run.
Usage
Save two files to the same directory on your server and run test.php from command line or browser.
File 1: test.php
File 2: GooglePR.class.php
Note: too many requests to Google server may get you temporarily banned by IP, but I haven't had any problems checking dozens of urls per one run.
Usage
Save two files to the same directory on your server and run test.php from command line or browser.
File 1: test.php
PHP:
<?php
include 'GooglePR.class.php';
// some sample and test urls
$urls = array('http://'.'www.facebook.com', # site without trailing slash
'http://'.'www.google.com/ads/publisher/', # site with subpage
'http://'.'asdf.com/asdf.html', # site without www subdomain and with page that doesnt exist
'http://'.'ezinearticles.com/?cat=Business', # site with query only
'http://'.'staad.pro-online-tutorial.fyxm.net/', # new site with pr 0
'http://'.'bhwmarketplace.blogspot.com/', # new site without pr
'http://'.'n-o-t-a-v-a-i-l-a-b-l-e.net/'); # site that doesnt exist
// print out pr check results
echo '<pre>'.print_r(GooglePR::bulk($urls), 1).'</pre>';
?>
File 2: GooglePR.class.php
PHP:
<?php
/**
* File: GooglePR.class.php
*
* Google PageRank checker. Implements change on Google PageRank API around October 2011.
* Added bulk check which finds out domain and actual PageRank with extra site availability check.
* Bulk check takes longer to execute because of double requests, but gives more relevant data about sites being checked.
*
* Usage:
*
* include 'GooglePR.class.php';
* simple check: echo GooglePR::get($url);
* bulk check: print_r(GooglePR::bulk($urls));
*
* @source Original source and credits to this forge goes to: mmncs.com/2011/10/how-to-create-your-own-google-pagerank-checker-using-php-updated-pagerank-query-url-php-code/
*/
class GooglePR
{
// Google PageRank API check entry point
var $google_pr_api = 'http://'.'toolbarqueries.google.com/tbr?client=navclient-auto&ch=%s&features=Rank&q=info:%s&num=100&filter=0';
// GooglePR singleton instance
private static $instance;
// static handler for getting pr string for url
function get($url) {
if(!isset(self::$instance)) {
self::$instance = new GooglePR();
}
return trim(substr(self::$instance->get_pr($url), 9));
}
// static handler for getting prs sin bulk
function bulk($urls) {
if(!isset(self::$instance)) {
self::$instance = new GooglePR();
}
return self::$instance->bulk_pr_check($urls);
}
// give array or urls to check. returns url, host / domain pr plus page pr in associative array
function bulk_pr_check($urls) {
$prs = array();
foreach ($urls as $r => $url) {
$pagerank = $hostrank = 'na';
$url_info = parse_url($url);
$host = $url_info['host'];
# first check if site / host exists
if ($this->site_exists($host)) {
# strip pr from return string
$hostrank = trim(substr($this->get_pr($host), 9));
$has_path = (isset($url_info['path']) && $url_info['path'] != '' && $url_info['path'] != '/') || isset($url_info['query']);
# if path doesnt exist, then host and page prs are same
if (!$has_path) {
$pagerank = $hostrank;
} elseif ($this->site_exists($url)) {
$pagerank = trim(substr($this->get_pr($url), 9));
}
}
$prs[$r]['url'] = $url;
$prs[$r]['host_pr'] = $hostrank;
$prs[$r]['page_pr'] = $pagerank;
}
return $prs;
}
// init curl resourse
function init_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
return $ch;
}
// dummy callback method for site exists check
function curl_header_callback($ch, $header) {}
// check if site exists
function site_exists($url) {
$ch = $this->init_curl($url);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'curl_header_callback'));
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return !($code != 200 && $code != 302 && $code != 304);
}
// get pagerank
function get_pr($url) {
global $google_pr_api;
$checksum = $this->check_hash($this->create_hash($url));
$url = sprintf($this->google_pr_api, $checksum, urlencode($url));
$ch = $this->init_curl($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, @$_SERVER['HTTP_REFERER']);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; GoogleToolbar 2.0.114-big; Windows XP 5.1)');
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// convert string to a number
function strtonmbr($string, $check, $magic) {
$int32 = 4294967296;
$length = strlen($string);
for ($i = 0; $i < $length; $i++) {
$check *= $magic;
if ($check >= $int32) {
$check = ($check - $int32 * (int) ($check / $int32));
$check = ($check < -($int32 / 2)) ? ($check + $int32) : $check;
}
$check += ord($string{$i});
}
return $check;
}
// create a url hash
function create_hash($string) {
$check1 = $this->strtonmbr($string, 0x1505, 0x21);
$check2 = $this->strtonmbr($string, 0, 0x1003F);
$factor = 4;
$halfFactor = $factor/2;
$check1 >>= $halfFactor;
$check1 = (($check1 >> $factor) & 0x3FFFFC0 ) | ($check1 & 0x3F);
$check1 = (($check1 >> $factor) & 0x3FFC00 ) | ($check1 & 0x3FF);
$check1 = (($check1 >> $factor) & 0x3C000 ) | ($check1 & 0x3FFF);
$calc1 = (((($check1 & 0x3C0) << $factor) | ($check1 & 0x3C)) << $halfFactor ) | ($check2 & 0xF0F );
$calc2 = (((($check1 & 0xFFFFC000) << $factor) | ($check1 & 0x3C00)) << 0xA) | ($check2 & 0xF0F0000 );
return ($calc1 | $calc2);
}
// create checksum for hash
function check_hash($hashNumber) {
$check = $flag = 0;
$hashString = sprintf('%u', $hashNumber);
for ($i = strlen($hashString) - 1; $i >= 0; $i --) {
$r = $hashString{$i};
if (1 === ($flag % 2)) {
$r += $r;
$r = (int)($r / 10) + ($r % 10);
}
$check += $r;
$flag++;
}
$check %= 10;
if (0 !== $check) {
$check = 10 - $check;
if (1 === ($flag % 2) ) {
if (1 === ($check % 2)) {
$check += 9;
}
$check >>= 1;
}
}
return '7'.$check.$hashString;
}
}
?>
Last edited: