Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.