【问题标题】:Warning: DOMDocument::load(): Extra content at the end of the document in http://widget.stagram.com/rss/n警告:DOMDocument::load():http://widget.stagram.com/rss/n 中文档末尾的额外内容
【发布时间】:2014-09-07 08:48:14
【问题描述】:

从上周开始,我收到了以下 php 警告消息

警告:DOMDocument::load():文档末尾的额外内容 在http://widget.stagram.com/rss/n/zee/,行:10 英寸 /home//public_html/wp-content/themes//inc/social-instagram.php 在第 22 行

我试图在警告消息中解析此链接的位置

$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

当我在网络浏览器http://widget.stagram.com/rss/tag/zee/ 中浏览链接时,xml 似乎没问题。

【问题讨论】:

    标签: php xml wordpress rss instagram


    【解决方案1】:

    您需要使用 curl 并添加选项CURLOPT_USERAGENT。这就是它在浏览器上运行的原因,而不是简单的file_get_contents->load。考虑这个例子:

    $url = ('http://widget.stagram.com/rss/tag/zee/');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    curl_setopt($ch, CURLOPT_FAILONERROR,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    $data = curl_exec($ch);
    curl_close($ch);
    $xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
    
    echo '<pre>';
    print_r($xml);
    

    Sample Output

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,发现以下解决方案不需要 CURL:

      libxml_set_streams_context(
          stream_context_create(
              array(
                  'http' => array(
                      'user_agent' => 'php'            
                  )
              )
          )
      );
      
      $dom = new DOMDocument;
      $dom->load($xml);
      

      多余的内容错误消失了,我推送的所有提要都运行良好。

      非常感谢 Gordon 用这个答案回答了另一个问题,这让我尝试解决这个问题。

      【讨论】:

        猜你喜欢
        • 2013-10-29
        • 1970-01-01
        • 2021-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-27
        相关资源
        最近更新 更多