【问题标题】:simplexml_load_file return empty object for alexa ranksimplexml_load_file 为 Alexa 排名返回空对象
【发布时间】:2013-06-26 03:04:14
【问题描述】:

我正在尝试获取任何域的 alexa 排名,我发现一个功能可以使用,但如果网站未在 alexa 上注册,它将返回空对象,然后我的网站将停止加载。

Alexa 功能:

<?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=s&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 = (int) $nodeAttributes['TEXT'];

    return $text;
}

例如:http://data.alexa.com/data?cli=10&dat=snbamz&url=anythinghere123.com

结果:

<ALEXA VER="0.9" URL="anythinghere123.com/" HOME="0" AID="VruOh1YRg600iz" IDN="anythinghere123.com/">
<RLS PREFIX="http://" more="0"></RLS>
<SD TITLE="A" FLAGS="" HOST="anythinghere123.com"></SD>
</ALEXA>

我尝试提供前缀命名空间,但它始终返回 0(所有域)。

如何解决这个问题?我想为任何未注册的网站设置 Alexa 排名 0

【问题讨论】:

    标签: php


    【解决方案1】:

    这个

    if (!$xml) {
            return FALSE;
        }
    

    用这个替换

    if (!isset($xml->SD[1])) {
            return FALSE;
        }
    

    【讨论】:

    • 它为所有域返回 0 :(
    • 为什么适用于所有域?我试过echo alexa_get_rank('http://google.com'); 它返回2 但不是0
    • 如果我使用你的代码,它会返回空对象。我刚才试了很多次。
    • 好的,如果没有“www”的域,它将返回空。可以帮我吗?
    • 比如我写echo alexa_get_rank('http://google.com');这个输出2。怎么看,我用的网址没有www
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多