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
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
Whoops, please change $data = $this->getPage($url); PHP: to $data = getPage($url); PHP: Sorry for the trouble
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
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(" ", $var); $alexa=$pieces[1]; $alexa=str_replace(',','',$alexa); $alexa=$alexa+1-1; PHP: $alexa is the alexa rank of $sitename... that was easy huh
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
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..