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