PHP PHP
Get Google and Alexa rank of a domain using PHP
On 26, Jun 2009 | 26 Comments | In PHP | By abcoder
There are already a lot of ways to find gpr/alexa rank of a domain using php. But not all of them work on 64bit servers. Here is the code that I always use for finding the google page rank and alexa rank of a website.
Get Google Page rank
function google_page_rank($url) { // URL or domain name
if (strlen(trim($url))>0) {
$_url = eregi("http://",$url)? $url:"http://".$url;
$pagerank = trim(GooglePageRank($_url));
if (empty($pagerank)) $pagerank = 0;
return (int)($pagerank);
}
return 0;
}
function GooglePageRank($url) {
$fp = fsockopen("toolbarqueries.google.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET /search?client=navclient-auto&ch=".CheckHash(HashURL($url))."&features=Rank&q=info:".$url."&num=100&filter=0 HTTP/1.1\r\n";
$out .= "Host: toolbarqueries.google.com\r\n";
$out .= "User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114-big; Windows XP 5.1)\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
$data = fgets($fp, 128);
$pos = strpos($data, "Rank_");
if($pos === false){} else{
$pagerank = substr($data, $pos + 9);
}
}
fclose($fp);
return $pagerank;
}
}
function StrToNum($Str, $Check, $Magic) {
$Int32Unit = 4294967296; // 2^32
$length = strlen($Str);
for ($i = 0; $i < $length; $i++) {
$Check *= $Magic;
if ($Check >= $Int32Unit) {
$Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
$Check = ($Check < -2147483648)? ($Check + $Int32Unit) : $Check;
}
$Check += ord($Str{$i});
}
return $Check;
}
function HashURL($String) {
$Check1 = StrToNum($String, 0x1505, 0x21);
$Check2 = 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);
}
function CheckHash($Hashnum) {
$CheckByte = 0;
$Flag = 0;
$HashStr = sprintf('%u', $Hashnum) ;
$length = strlen($HashStr);
for ($i = $length - 1; $i >= 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 >>= 1;
}
}
return '7'.$CheckByte.$HashStr;
}
Find Alexa Rank
function alexaRank($domain){
$remote_url = 'http://data.alexa.com/data?cli=10&dat=snbamz&url='.trim($domain);
$search_for = '<POPULARITY URL';
if ($handle = @fopen($remote_url, "r")) {
while (!feof($handle)) {
$part .= fread($handle, 100);
$pos = strpos($part, $search_for);
if ($pos === false)
continue;
else
break;
}
$part .= fread($handle, 100);
fclose($handle);
}
$str = explode($search_for, $part);
$str = array_shift(explode('"/>', $str[1]));
$str = explode('TEXT="', $str);
return $str[1];
}
Hope this will help you. If you know any better(working) solution please let me know via comments.
Become expert web developer with testking JN0-522 web development course and learn about the latest PHP applications using testking 642-611 tutorials and self paced testking 70-431 study guides.
-
this is really amazing. the google page rank looks fine. but, i have a question about alexa ranking. wont it break when alexa changes its page (UI) layout? also, is it legal to get it this way?
let me know if i can use it in my projects. thanks again for the great code. you rock!
-
adnan,
right you are! the above url is returning correct alexa rank for subdomains too. please modify your php and post it in comments to reflect the changes. that would be really helpful for me and others too.
thanks again for your effort.
-
oppss.. sorry. i did not notice that your previous code (in comment) is appropriate for this purpose.
-
usually developers dont use fopen for remote url and hosting servers also may not enable them due to security reason. Why dont you use fsockopen or cURL ?
-
Thanks for this script
-
The Alexa script works perfectly, thanks a lot
-
@Zyva, glad to know that
-
Hello how can i get popularity number from this code?
http://data.alexa.com/data?cli=10&dat=snbamz&url=abcoder.com
Could you post all code what I should insert in templates to display only this number?
Thanks Daniel -
@adnan – Thanks for your respond , I knew where is the number in XML , but i wanted the code which retrieves only this number from this XML to display it in my template – just did it thanks to one good programmer, http://worldwidedancers.com/topsites/ , in first column I have Alexa list ,
About Page rank , can you tell me pls. what code I need to add in templates to insert it in the column on the left next to the Alexa ?
Thanks for your respond, Daniel -
Thanks, it works great!
It’s very useful!Thanks a lot for sharing
-
Thank you for the genius note. It’s work.
-
Hi,
Thanks for the script. But I have a little question about running it in a wordpress blog.
My Blog is a listing of popular websites ordered by their Alexa Rank. Every post of my wordpress blog treats about a website.
How Do I pick up a rank of a website and associate it with an article ID? May I use custom fields for doing that?Thanks for your help!
-
@JkrzSeven, You may do this:
1. In your post add a custom field, say “gpr” with the domain name of the site as value.
2. In your template put an image with src=”gpr.php?domain=domain_name_of_the_site
3. Make 10 different images like gpr_1.jpg to gpr_10.jpg and keep them in your images folder.
4. In gpr.php calculate the gpr using my script and output an image like gpr_(page rank value).jpg
-
Thanks for sharing. I visited few of your post, and its very informative. I will apply the points to my blog for increase my Alexa Page rank.
-
A more powerful php class can be found here:
http://code.google.com/p/seostats/ -
Hi,
I wonder if you could let me know. How to find similar domain according to alexa from my database.??like if I have http://www.freewebstat.info/abcoder.com rank 256,481 now my similar domains mean nearest alexa rank to abcoder.
like
abcoder0.com – 256478
abcoder1.com – 256479
abcoder2.com – 256480
abcoder3.com – 256482
abcoder4.com – 256483
abcoder5.com – 256484I wanna print only domain name. if you could help me please.. with phpcode. thank you
-
Thank you so much
This really helpfull.. -
A few people recommended alexage.com .They brought my site to Alexa’s top 50K in less than 90 days
-
Adnan, this tutorial is really helpful to my exercise project!! Thank you so much for posting such script like above!!
Submit a Comment
© Copyright 2012 ABCoder |













Comments