2015-11-07 120 views
0

我想用PHP解析this feed。这是饲料结构:RSS源不会返回任何与PHP?

<item> 
    <title> ... TITLE ... </title> 
    <link> ... LINK .... </link> 
    <comments> .. COMMENTS .. </comments> 
    .... More tags here .... 
    <description><![CDATA[.. HTML ...]]></description> 
</item> 

这是我的PHP代码:

$rss = new DOMDocument(); 
$rss->loadHTML($feed_url); 
foreach ($rss->getElementsByTagName('item') as $node) { 
    $description = $node->getElementsByTagName('description')->item(0)->nodeValue; 
    echo $description; 
} 

但它呼应什么。我试过使用cURL,但即使如此,我也无法回应description标签。

我需要在此代码中更改它以使其工作?请让我知道如果我需要发布备用cURL方法的代码。

回答

0

loadHTML用于加载HTML内容,阅读下面的解决方案RSS使用

方法1

$feed_url = 'http://thechive.com/feed/'; 
$rss = new DOMDocument(); 
$rss->load($feed_url); 
foreach ($rss->getElementsByTagName('item') as $node) { 
    $description = $node->getElementsByTagName('description')->item(0)->nodeValue; 
    echo $description; 
} 

方法2

$feed_url = 'http://thechive.com/feed/'; 
$content = file_get_contents($feed_url); 
$x = new SimpleXmlElement($content); 

foreach($x->channel->item as $entry) { 
    echo $entry->description; 
} 

希望它会帮助你。 ..