0

我有一个示例代码:

<?php 
header('Content-Type: text/html; charset=utf-8');
$doc = new DOMDocument();
$doc->load('http://www.haivl.com/rss');

$items = $doc->getElementsByTagName('item');
foreach ($items as $key => $item) {
    $titles = $item->getElementsByTagName( "title" );
    $title = $titles->item(0)->nodeValue;
    echo $title;
}
?>

我无法从此网址获取标题,为什么会出错,可以帮我解决这个错误!

4

1 回答 1

2

问题是这两行

$title = $titles->item(0)->nodeValue;
echo $titles;

您将 title 值分配给$title但 echoing $titles$titles是 aDOMNodeList并且不能隐式转换为字符串。你想做

$title = $titles->item(0)->nodeValue;
echo $title;
于 2013-05-01T14:23:21.327 回答