【问题标题】:How to fix "simplexml_load_file" error in PHP如何修复 PHP 中的“simplexml_load_file”错误
【发布时间】:2019-03-25 08:25:10
【问题描述】:

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

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");

【讨论】:

    猜你喜欢
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2017-12-18
    • 1970-01-01
    • 2011-05-16
    • 2017-11-10
    相关资源
    最近更新 更多