我有以下rss格式,我无法弹出'content:encoded'值。
<item>
<title>some title</title>
<link>some link</link>
<pubDate>Sat, 07 Apr 2012 5:07:00 -0700</pubDate>
<content:encoded><![CDATA[this value]]></content:encoded>
</item>
我写了这个函数,除了"content:encoded"字段之外,一切都运行良好,该字段给了我此错误:"注意:尝试获取非对象的属性"
function rssReader($url) {
$doc = new DOMDocument();
$doc->load($url);
$fields = array('title', 'description', 'link', 'pubDate', 'content:encoded');
$nodes = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$item = array();
var_export($node, true);
foreach ($fields as $field)
$item[$field] = $node->getElementsByTagName($field)->item(0)->nodeValue;
$nodes[] = $item;
}
return $nodes;
}
你需要
使用getElementsByTagNameNS而不是getElementsByTagName
'content:encoded'
标签:
foreach ($fields as $field){
if( $field == 'content:encoded' ){
$item[$field] = $node->getElementsByTagNameNS('contentNamespaceURI','encoded')->item(0)->nodeValue;
}else{
$item[$field] = $node->getElementsByTagName($field)->item(0)->nodeValue;
}
}
你可以在rss
找到'contentNamespaceURI'
。必须有类似的东西:
xmlns:content="contentNamespaceURI"
这里的标签名称是"编码的"。
只需使用
$content => $node->getElementsByTagName('encoded')->item(0)->nodeValue