Jump to content

getAlexaRank($url) function not working


Jnerocorp

Recommended Posts

I have made a function to get alexa rank

the site is here: http://mytestsite.rack111.com/1

 

but the Alexa rank is showing up blank and I dont know why can someone please tell me if there is something wrong with my code

 

 

 


        function getAlexaRank ($url) {
        $xml = simplexml_load_file("http://data.alexa.com/data?cli=10&dat=s&url=$url");
        return $xml->SD->POPULARITY['TEXT'];

        }

Link to comment
https://forums.phpfreaks.com/topic/179740-getalexarankurl-function-not-working/
Share on other sites

do you mean this?: http://data.alexa.com/data?cli=10&dat=s&url=$url

 

http://data.alexa.com/data?cli=10&dat=s&url=www.google.com <- example

 

its just the xml file that has the data for alexa rankings

 

but my script that i am working on is supposed to open and read the xml for for the popularity where text="a number here"

 

-John

Oh sorry. I was viewing it in Chrome and I forgot that it doesn't work very well with XML files; it showed up blank to me.

 

Anyways, the problem is that it's not returning the string in the attribute, but it's returning a SimpleXMLElement object. Simply casting it explicitly to an int will fix it:

 

function getAlexaRank ($url) {
$xml = simplexml_load_file("http://data.alexa.com/data?cli=10&dat=s&url=$url");
return (int) $xml->SD->POPULARITY['TEXT'];
}

Really? It works fine for me.

 

<?php
function getAlexaRank ($url) {
$xml = simplexml_load_file("http://data.alexa.com/data?cli=10&dat=s&url=$url");
return (int) $xml->SD->POPULARITY['TEXT'];
}

echo getAlexaRank('google.com');    // 1
echo getAlexaRank('phpfreaks.com'); // 11283

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.