0

我正在尝试获得任何链接的 Alexa 排名。我找到了一个简单的类,它是

Class Alexa {

    function getAlexaRank($url)
    {
        $request_url = "http://data.alexa.com/data?cli=10&dat=snbamz&url=".$url;
        $xml = simplexml_load_file($request_url) or die("feed not loading");
        return $xml->SD->POPULARITY['TEXT'];
    }

}

当我去这个链接

http://data.alexa.com/data?cli=10&dat=snbamz&url=google.com

我明白了

<SD>
<POPULARITY URL="google.com/" TEXT="2" SOURCE="panel"/>
<REACH RANK="1"/>
<RANK DELTA="+1"/>
<COUNTRY CODE="US" NAME="United States" RANK="1"/>
</SD>

但是为什么这个类不起作用?它不返回任何东西

谢谢你 :)

4

1 回答 1

4
<?php
/**
 * Get Popularity Text of a Domain via Alexa XML Data
 *
 * @return string|FALSE text or FALSE on error
 */
function alexa_get_rank($domain)
{
    $alexa = "http://data.alexa.com/data?cli=10&dat=snbamz&url=%s";
    $request_url =  sprintf($alexa, urlencode($domain));

    $xml = simplexml_load_file($request_url);
    if (!$xml) {
        return FALSE;
    }

    $nodeAttributes = $xml->SD[1]->POPULARITY->attributes();
    $text = (string) $nodeAttributes['TEXT'];

    return $text;
}

var_dump(alexa_get_rank('google.com'));
// string(1) "2"
于 2012-11-09T09:07:11.157 回答