如何在PHP中修复“simplexml_load_file”错误

问题描述 投票:-3回答:1

我正在使用此代码在我的本地服务器中获得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

php xml simplexml
1个回答
0
投票

Alexa将我的IP添加到黑名单中,因此响应是“好的”。我找到minisiteinfo网址并编写新代码以获得全球和国家排名,并且此时工作正常。如果有人像我一样有问题,我会放下代码。

码:

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");
© www.soinside.com 2019 - 2024. All rights reserved.