繁体   English   中英

如何修复 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

这是示例页面的链接

Alexa 将我的 IP 添加到黑名单,因此响应为“好的”。 我找到了 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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM