2014-07-16 21 views
1

從過去兩個星期,我在得到以下PHP警告信息警告:DOM文檔:: load()方法:在文檔的末尾附加內容http://widget.stagram.com/rss/n

警告:DOM文檔::負載():在文檔 在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似乎沒問題。

回答

2

您需要使用捲曲,並添加選項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

+0

謝謝,它的工作原理 – zzlalani

+0

@zzlalani沒有概率 – user1978142

0

我有同樣的問題,找到了以下解決方案,而無需工作了捲曲:

libxml_set_streams_context(
    stream_context_create(
     array(
      'http' => array(
       'user_agent' => 'php'    
      ) 
     ) 
    ) 
); 

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

額外的內容錯誤走了,所有的飼料我推動了良好的工作。

非常感謝Gordon回答另一個問題這個答案促使我嘗試了這個問題。

相關問題