Image Image Image Image Image

© Copyright 2012 ABCoder | Email | RSS

Scroll to Top

To Top

PHP PHP

26

Jun
2009

26 Comments

In PHP

By abcoder

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.

Tags | , , , , , , , ,

Comments

  1. zp bappi

    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!

  2. Bappi,

    Yes you are right. This is not the proper way for finding the alexa rank and it won’t work if alexa.com changes their layout. You can get it in formal way from this XML:

    http://data.alexa.com/data?cli=10&dat=snbamz&url=abcoder.com

    and the php code for this is:

    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];
    }

    But the big problem with this is you won’t get correct alexa rank of subdomains from this. Say blogspot.com and abcoder.blogsopt.com will show same alexa rank, which is wrong.
    For this problem I’m using the method of parsing the alexa website and for now it’s getting accurate alexa rank for subdomains :)

    Lemme know if you get any better thing.

    Thanks

  3. Bappi,

    I guess http://data.alexa.com/data?cli=10&dat=snbamz&url=http://girlwithaonetrackmind.blogspot.com/ is providing the correct alexa rank of subdomain now!

    It seems they have updated their service. :)

  4. zp bappi

    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. :)

  5. zp bappi

    oppss.. sorry. i did not notice that your previous code (in comment) is appropriate for this purpose. :P

  6. Bappi,

    Thank you. I’ve already posted the modified code in the comment, I’ll modify my post also..

    The interesting thing is last time I checked this code on 6 June here http://www.abcoder.com/alexa.php to find out the alexa rank of http://sethgodin.typepad.com and it returned 215 (the alexa rank of typepad.com). Now the same code is providing the correct alexa rank 8935 :)

  7. The alexa rank finding code in the main post is updated.

  8. 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 ?

    • @Adnan, good point, CURL is not enabled/supported by all hosting providers. I wrote the code for a module which is publicly used on various hosting providers.

      But thanks for your suggesting and I’ll update my code soon to use CURL if available otherwise fopen/fsockopen.

      Thanks

  9. Testmuku

    Thanks for this script

  10. The Alexa script works perfectly, thanks a lot ;)

  11. @Zyva, glad to know that :)

  12. 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

    • @Daniel, If you open that url in browser you’ll see

      <POPULARITY URL="abcoder.com/" TEXT="338168"/>

      at bottom section. the value of TEXT is the alexa rank.

  13. @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

  14. Thanks, it works great!
    It’s very useful!

    Thanks a lot for sharing

  15. Thank you for the genius note. It’s work.

  16. JkrzSeven

    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!

  17. @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

  18. 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.

  19. A more powerful php class can be found here:
    http://code.google.com/p/seostats/

  20. 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 – 256484

    I wanna print only domain name. if you could help me please.. with phpcode. thank you

  21. Thank you so much
    This really helpfull..

  22. Loura

    A few people recommended alexage.com .They brought my site to Alexa’s top 50K in less than 90 days

  23. Adnan, this tutorial is really helpful to my exercise project!! Thank you so much for posting such script like above!!

Submit a Comment