我有一个示例代码:
<?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;
}
?>
我无法从此网址获取标题,为什么错误,可以帮我修复此错误!
答案 0 :(得分:2)
问题是这两行
$title = $titles->item(0)->nodeValue;
echo $titles;
您将标题值分配给$title
,但回显$titles
。 $titles
是DOMNodeList
,无法隐式转换为字符串。你想做什么
$title = $titles->item(0)->nodeValue;
echo $title;