如何修复 PHP 中的 "simplexml_load_file" 错误

How to fix "simplexml_load_file" error in PHP

我正在使用此代码在我的本地服务器中获取 Alexa 排名。但是当我上传到我的服务器并且出现错误时,这段代码不起作用。

我使用的代码:

$uri = 'http://data.alexa.com/data?cli=10&dat=snbamz&url=';
$uri .= $domain;
$xml = simplexml_load_file($uri);
if (isset($xml->SD[1]->COUNTRY))
$country_rate = (int) $xml->SD[1]->COUNTRY->attributes()->RANK;
return($country_rate);

错误:

Warning: simplexml_load_file(): http://data.alexa.com/data?cli=10&dat=snbamz&url=tooc.ir:1: parser error : Start tag expected, '<' not found in /home2/mydesign/domains/tooc.ir/public_html/panel/2.php on line 5
Warning: simplexml_load_file(): Okay in /home2/mydesign/domains/tooc.ir/public_html/panel/2.php on line 5
Warning: simplexml_load_file(): ^ in /home2/mydesign/domains/tooc.ir/public_html/panel/2.php on line 5
Notice: Undefined variable: country_rate in /home2/mydesign/domains/tooc.ir/public_html/panel/2.php on line 13

这里是示例页面的link

Alexa 将我的 IP 添加到黑名单,因此响应是 "Okay"。 我找到了 minisiteinfo URL 并编写了新代码以获得全球和国家/地区排名,此时它的工作正常。如果有人像我一样有问题,我会把代码放下。

代码:

function AlexaRank($domain, $country, $mode) {
    $url = "https://www.alexa.com/minisiteinfo/".$domain;
    $string = file_get_contents($url);
    if ($mode == "country") {
        $temp_s = substr($string, strpos($string, $country." Flag") + 9 + strlen($country));
        return(substr($temp_s, 0, strpos($temp_s, "</a></div>")));
    }
    else if ($mode == "global") {
        $temp_s = substr($string, strpos($string, "Global") + 38);
        return(substr($temp_s, 0, strpos($temp_s, "</a></div>")));
    }
    else {
        return('something wrong.');
    }
}


// Here how to use for country rank:
echo AlexaRank("tooc.ir", "Iran", "country");

// Here how to use for global rank:
echo AlexaRank("tooc.ir", "Iran", "global");