1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Simple Alexa Rank Retrieval

Discussion in 'PHP' started by -NB-, Feb 10, 2007.

  1. #1
    Many web developers are stumped with the method of retrieving Alexa Ranks. With this simple code, you can get yours within seconds!
    <?php
    	function getPage ($url) {
    		if (function_exists('curl_init')) {
    			$ch = curl_init($url);
    			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    			curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    			return curl_exec($ch);
    		} else {
    			return file_get_contents($url);
    		}
    	}
    	function getAlexaRank($url) {
    		$url = @parse_url($url);
    		$url = $url['host'];
    		$url = "http://data.alexa.com/data?cli=10&dat=s&url=$url";
    		$data = $this->getPage($url);
    		preg_match('#<POPULARITY URL="(.*?)" TEXT="([0-9]+){1,}"/>#si', $data, $p);
    		$value = ($p[2]) ? number_format($p[2]) : 0;
    		return $value;
    	}
    ?>
    PHP:
    To call the function, simply enter this code, but with your desired website:
    <?php
    	echo getAlexaRank ('http://www.google.com');
    ?>
    PHP:
    Comments and rep appreciated. Have a nice day :D
     
    -NB-, Feb 10, 2007 IP
    Subikar, Brandon Sheley and SoftCloud like this.
  2. Red_Virus

    Red_Virus Well-Known Member

    Messages:
    3,756
    Likes Received:
    249
    Best Answers:
    0
    Trophy Points:
    135
    #2
    nice one..
     
    Red_Virus, Feb 11, 2007 IP
  3. Alistair

    Alistair Active Member

    Messages:
    563
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Five star post, thanks
     
    Alistair, Feb 11, 2007 IP
  4. SNaRe

    SNaRe Well-Known Member

    Messages:
    1,132
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    165
    #4
    very nice i was thinking how to do it . Thanks
     
    SNaRe, Feb 11, 2007 IP
  5. protocol96

    protocol96 Peon

    Messages:
    413
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Awesome!!

    Gr8 post!! Lucky to be here, between u guyz :D
     
    protocol96, Feb 11, 2007 IP
  6. SoftCloud

    SoftCloud Well-Known Member

    Messages:
    1,060
    Likes Received:
    28
    Best Answers:
    2
    Trophy Points:
    120
    #6
    Oh my goodness... deffo decent!! Rated you ! :)
     
    SoftCloud, Feb 11, 2007 IP
  7. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks everyone, appreciate the comments :)

    Took me a while to figure it out as well, figured I'd share :D
     
    -NB-, Feb 11, 2007 IP
  8. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I get:
    Fatal error: Using $this when not in object context in C:\Program Files\xampp\htdocs\alexa.php on line 17

    lemme check to see if cURL is enabled
     
    bobby9101, Feb 11, 2007 IP
  9. SoftCloud

    SoftCloud Well-Known Member

    Messages:
    1,060
    Likes Received:
    28
    Best Answers:
    2
    Trophy Points:
    120
    #9
    oooo I got that too, when I tried it on my host and also on localhost ... :/
     
    SoftCloud, Feb 11, 2007 IP
  10. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Whoops, please change
    $data = $this->getPage($url);
    PHP:
    to
    $data = getPage($url);
    PHP:
    Sorry for the trouble :eek:
     
    -NB-, Feb 11, 2007 IP
  11. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #11
    with thanks to nico_swd (i think that's his username)
    I am using:
    
    <?php
    $url = google.com;
    $xml = file_get_contents('http://data.alexa.com/data?cli=10&dat=s&url=$url');
    preg_match('/TEXT="([\d]+)"\/>/i', $xml, $match);
    echo $match[1];
    ?>
    
    Code (markup):
    does not require cURL and it is fast
     
    bobby9101, Feb 11, 2007 IP
  12. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I wonder what:
    <REACH RANK="2"/>
    Code (markup):
    is for?
     
    bobby9101, Feb 11, 2007 IP
  13. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #13
    I ran a simple benchmark test, and mine returned on average about .01 seconds faster :D
     
    bobby9101, Feb 11, 2007 IP
  14. aditya_sfs

    aditya_sfs Peon

    Messages:
    2,271
    Likes Received:
    389
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Another simple method which i used on a few projects of mine was like this -

    
    $sitename="www.xxxx.com";
    $content = strip_tags (file_get_contents ('http://www.alexa.com/data/details/traffic_details?url='.$sitename));
    $content = str_replace(' Traffic Rank','xxx',$content);
    $content = str_replace('Explore this site','xxx',$content);
    $content = explode('xxx', $content);
    $var=trim($content[1]);
    $pieces = explode("&nbsp;", $var);
    $alexa=$pieces[1];
    $alexa=str_replace(',','',$alexa);
    $alexa=$alexa+1-1;
    
    PHP:
    $alexa is the alexa rank of $sitename...

    that was easy huh :p :p
     
    aditya_sfs, Feb 12, 2007 IP
    SoftCloud likes this.
  15. SoftCloud

    SoftCloud Well-Known Member

    Messages:
    1,060
    Likes Received:
    28
    Best Answers:
    2
    Trophy Points:
    120
    #15
    Yay, this one works. +1
     
    SoftCloud, Feb 12, 2007 IP
  16. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #16
    @softcloud? did mine not work?
    hmm works on localhost for me, I am on PHP5
     
    bobby9101, Feb 12, 2007 IP
  17. aditya_sfs

    aditya_sfs Peon

    Messages:
    2,271
    Likes Received:
    389
    Best Answers:
    0
    Trophy Points:
    0
    #17
    The only problem might be that file_get_contents is not allowed by some hosting companies like dreamhost.

    Also such things make a site very slow especially if you have them on all pages. The similar method can be used to calculate backlinks pointing to a site as in MSN And Yahoo

    To count the number of backlinks in MSN we type "link:www.xxxx.com" .In PHP we can do it same way like this -

    
    $sitename="www.xxxx.com";
    $content = strip_tags (file_get_contents ('http://search.live.com/results.aspx?q=link%3A'.$sitename));
    $content = str_replace('Page 1 of ','xxx',$content);
    $content = str_replace(' results'.$sitename.'','xxx',$content);
    $content = explode('xxx', $content);
    $var=trim($content[1]);
    $pieces = explode(" ", $var);
    $msnbl = $pieces[0];
    $msnbl=str_replace(',','',$msnbl);
    $msnbl=$msnbl+1-1;
    
    PHP:
    And similarly for yahoo

    
    $sitename="www.xxxx.com";
    $content = strip_tags (file_get_contents ('http://siteexplorer.search.yahoo.com/advsearch?p=http%3A%2F%2F'.$sitename.'&bwm=i&bwmf=a&bwms=p'));
    $content = str_replace('Inlinks (','xxx',$content);
    $content = str_replace(') Show Inlinks','xxx',$content);
    $content = explode('xxx', $content);
    $var=trim($content[1]);
    $pieces = explode(")", $var);
    $yahoobl = $pieces[0];
    $yahoobl=str_replace(',','',$yahoobl);
    $yahoobl=$yahoobl+1-1;
    
    PHP:
    Yes, your method is nice, small and quick. I tried to this using this method earlier but failed since i am not a expert at regular expressions.

    regards
    aditya
     
    aditya_sfs, Feb 12, 2007 IP
  18. ThomasNederman

    ThomasNederman Peon

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Is it not a risk that alexa block a IP when using file_get_contents and it is better to use curl since you can set the browser version ? Just a thought..
     
    ThomasNederman, Feb 12, 2007 IP
  19. SoftCloud

    SoftCloud Well-Known Member

    Messages:
    1,060
    Likes Received:
    28
    Best Answers:
    2
    Trophy Points:
    120
    #19
    Nah the file_get_contents thing didn't work mate. My localhost is PHP4 and my host is PHP5.
     
    SoftCloud, Feb 13, 2007 IP
  20. ruby

    ruby Well-Known Member

    Messages:
    1,854
    Likes Received:
    40
    Best Answers:
    1
    Trophy Points:
    125
    #20
    Nice! Works a charm!
     
    ruby, Feb 14, 2007 IP