How to add alexa traffic rate in my php code?

Discussion in 'PHP' started by hotpop, Aug 18, 2010.

  1. #1
    Hi ,I want to add the alexa traffic rate in my php code, please see the attached image.
    Could you tell how to do?
    Thanks in advance.
     

    Attached Files:

    • s.gif
      s.gif
      File size:
      2.7 KB
      Views:
      126
    hotpop, Aug 18, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Hmm I'm sure I did that for a script I wrote a while back (serversiders.com) I'll have to search through the archive an get back to you. I'm sure its only a few lines of code :)
     
    danx10, Aug 18, 2010 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    <?php
    function alexa_badge($url) {
        $url = trim($url);
        if (!preg_match('~^http://~i', $url))
            $url = "http://{$url}";
        $host = parse_url($url, PHP_URL_HOST);
        $host = preg_replace('~^www\.~', NULL, strtolower($host));
        return '<img src="http://xsltcache.alexa.com/site_stats/gif/t/a/' . base64_encode($host) . '/s.gif" />';
    }
    
    //replace the http://google.com with your site url...
    echo alexa_badge('http://google.com');
    ?>
    PHP:
     
    danx10, Aug 18, 2010 IP
    hotpop likes this.
  4. hotpop

    hotpop Peon

    Messages:
    2,059
    Likes Received:
    96
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for your code.I will try right now . :)
     
    hotpop, Aug 18, 2010 IP
  5. jody_w

    jody_w Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I stumbled on this thread and this worked great for something I was doing, so I wanted to stop by and thank danx10 for the code. Now I have a question: suppose I just wanted the Alexa traffic rank, without the gif. like for it to say "Alexa traffic rank" and then whatever the rank was. How would I do that?

    TIA
     
    jody_w, Aug 21, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    <?php
    function alexa_rank($url) {
        $url = trim($url);
        if (!preg_match('~^http://~i', $url))
            $url = "http://{$url}";
        $host = parse_url($url, PHP_URL_HOST);
        $host = preg_replace('~^www\.~', NULL, strtolower($host));
        preg_match('~\<POPULARITY.+?TEXT="(\d+)"/\>~', file_get_contents('http://data.alexa.com/data?cli=10&dat=s&url=' . $host), $a);
        return $a[1];
    }
    
    //replace the http://google.com with your site url...
    echo 'Alexa traffic rank: '.alexa_rank('http://google.com');
    ?>
    PHP:
     
    danx10, Aug 21, 2010 IP
  7. jody_w

    jody_w Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thank you so much! That does exactly what I need it to do.
     
    jody_w, Aug 21, 2010 IP
  8. GeekOnline

    GeekOnline Peon

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks! from here too!
     
    GeekOnline, Aug 22, 2010 IP