【问题标题】:Error with simplexml_load_file via HTTP通过 HTTP 的 simplexml_load_file 错误
【发布时间】:2014-10-29 08:52:59
【问题描述】:

我在 php 中使用 simplexml_load_file 通过 HTTP 检索 XML 内容。

这是我的代码:

if ($xml = simplexml_load_file($url)) {
   $item_list = $xml->xpath("/probabile_formazione/titolari/calciatore");
   foreach ($item_list as $item) {
       echo $item . ' ';
   }
}

如果我使用 $url = "http://www.gazzetta.it/ssi/2011/boxes/calcio/squadre/cagliari/formazione/formazione.xml"; 它有效,但如果我使用 $url = "http://www.gazzetta.it/ssi/2011/boxes/calcio/squadre/atalanta/formazione/formazione.xml"; 我遇到了这个错误:

[Thu Sep 04 16:37:01 2014] [error] [client 127.0.0.1] PHP Warning: simplexml_load_file(): http://www.gazzetta.it/ssi/2011/boxes/calcio/squadre/atalanta/formazione/formazione.xml:1: parser error : start tag expected, '

你能帮帮我吗? 谢谢 pasquy73

【问题讨论】:

  • 第 42 行是:if ($xml = simplexml_load_file($url)) ...

标签: php xml curl


【解决方案1】:

我认为响应 xml 格式不正确。您应该处理获取正确 xml 的可能错误,例如:

<?
    $url = "http://www.gazzetta.it/ssi/2011/boxes/calcio/squadre/atalanta/formazione/formazione.xml";

    libxml_use_internal_errors(true);

    if ($xml = simplexml_load_file($url)) {
       $item_list = $xml->xpath("/probabile_formazione/titolari/calciatore");
       foreach ($item_list as $item) {
           echo $item . ' ';
       }
    }
    else {

        foreach (libxml_get_errors() as $error) {
            print_r($error);
        }

        libxml_clear_errors();
    }

?>

【讨论】:

    猜你喜欢
    • 2011-05-16
    • 2017-11-10
    • 2015-03-26
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多