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

时间:2019-03-25 08:25:11

标签: php xml simplexml

我正在使用此代码来获取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

1 个答案:

答案 0 :(得分: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");