Jnerocorp Posted October 31, 2009 Share Posted October 31, 2009 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']; } Quote Link to comment https://forums.phpfreaks.com/topic/179740-getalexarankurl-function-not-working/ Share on other sites More sharing options...
Daniel0 Posted October 31, 2009 Share Posted October 31, 2009 Where did you get that API URL? Quote Link to comment https://forums.phpfreaks.com/topic/179740-getalexarankurl-function-not-working/#findComment-948320 Share on other sites More sharing options...
Jnerocorp Posted October 31, 2009 Author Share Posted October 31, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/179740-getalexarankurl-function-not-working/#findComment-948323 Share on other sites More sharing options...
Daniel0 Posted October 31, 2009 Share Posted October 31, 2009 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']; } Quote Link to comment https://forums.phpfreaks.com/topic/179740-getalexarankurl-function-not-working/#findComment-948326 Share on other sites More sharing options...
Jnerocorp Posted October 31, 2009 Author Share Posted October 31, 2009 ok that code got it to work but now it is always displaying "0" instead of the number that is actually there Quote Link to comment https://forums.phpfreaks.com/topic/179740-getalexarankurl-function-not-working/#findComment-948330 Share on other sites More sharing options...
salathe Posted October 31, 2009 Share Posted October 31, 2009 What is the actual XML that you're retrieving (use $xml->saveXML() to see it)? Quote Link to comment https://forums.phpfreaks.com/topic/179740-getalexarankurl-function-not-working/#findComment-948332 Share on other sites More sharing options...
Daniel0 Posted October 31, 2009 Share Posted October 31, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/179740-getalexarankurl-function-not-working/#findComment-948333 Share on other sites More sharing options...
Jnerocorp Posted October 31, 2009 Author Share Posted October 31, 2009 ok found the problem its my hosting im on a free hosting so it doesnt allow certain things -John thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/179740-getalexarankurl-function-not-working/#findComment-948337 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.