无法在PHP中从提要中提取标题?
我正试图从中提取数据。这是我的代码:无法在PHP中从提要中提取标题?,php,dom,rss,Php,Dom,Rss,我正试图从中提取数据。这是我的代码: $xml = file_get_contents_curl($feed_url); $rss = new DOMDocument(); $rss->load($xml); 函数file\u get\u contents\u curl从网页获取数据。在将其转储到var中时,如下所示: var_dump($xml); foreach ($rss->getElementsByTagName('item') as $node) { $title
$xml = file_get_contents_curl($feed_url);
$rss = new DOMDocument();
$rss->load($xml);
函数file\u get\u contents\u curl
从网页获取数据。在将其转储到var中时,如下所示:
var_dump($xml);
foreach ($rss->getElementsByTagName('item') as $node) {
$title = $node->getElementsByTagName('title')->item(0)->nodeValue;
它像预期的那样回应一切(我指的是所有的标题、链接等标签)。但是,如果我在$rss
上使用var\u dump
:
var_dump($rss);
我得到的答复是:
object(DOMDocument)#1 (34) { ["doctype"]=> NULL ["implementation"]=> string(22) "(object value omitted)" ["documentElement"]=> NULL ["actualEncoding"]=> NULL ["encoding"]=> NULL ["xmlEncoding"]=> NULL ["standalone"]=> bool(true) ["xmlStandalone"]=> bool(true) ["version"]=> string(3) "1.0"
["xmlVersion"]=> string(3) "1.0" ["strictErrorChecking"]=> bool(true) ["documentURI"]=> NULL ["config"]=> NULL ["formatOutput"]=> bool(false) ["validateOnParse"]=> bool(false) ["resolveExternals"]=> bool(false) ["preserveWhiteSpace"]=> bool(true)
["recover"]=> bool(false) ["substituteEntities"]=> bool(false) ["nodeName"]=> string(9) "#document" ["nodeValue"]=> NULL ["nodeType"]=> int(9) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL
["lastChild"]=> NULL ["previousSibling"]=> NULL ["attributes"]=> NULL ["ownerDocument"]=> NULL ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> NULL ["baseURI"]=> NULL ["textContent"]=> string(0) "" }
现在,我无法从提要中提取标题或其他内容。我的代码如下:
var_dump($xml);
foreach ($rss->getElementsByTagName('item') as $node) {
$title = $node->getElementsByTagName('title')->item(0)->nodeValue;
但是,如果您在第11列第273行的chromeerror中打开提要,则提要有一个错误:编码错误,但它是在Firefox中打开的。但我想我应该能够解析提要直到第一个错误点
以下是提要的示例:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>eBaum's World - Featured Media</title>
<link>http://www.ebaumsworld.com</link>
<atom:link href="http://www.ebaumsworld.com/rss/featured/" rel="self" type="application/rss+xml" />
<description>The latest featured media</description>
<language>en-us</language>
<copyright>eBaum's World (c) 1998-2015</copyright>
<lastBuildDate>Wed, 25 Nov 2015 03:31:12 -0500</lastBuildDate>
<pubDate>Wed, 25 Nov 2015 03:31:12 -0500</pubDate>
<item>
<title>24 People Being Complete A$$holes</title>
<link>http://www.ebaumsworld.com/pictures/view/84832600/</link>
<description>
<![CDATA[
<table cellspacing="0" cellpadding="2" width="100%" border="0">
<tr>
<td valign="top" width="120">
<a href="http://www.ebaumsworld.com/pictures/view/84832600/"><img width="320" height="220" src="http://cdn.ebaumsworld.com/thumbs/2015/11/24/070634/84832600/assholes.jpg" border="0" /></a>
</td>
<td valign="top">
People acting like such mega-jerks it might send you into a blind rage! </td>
</tr>
</table>
]]>
</description>
<pubDate>Tue, 24 Nov 2015 23:02:00 -0500</pubDate>
<enclosure type="image/jpg" url="http://cdn.ebaumsworld.com/thumbs/2015/11/24/070634/84832600/assholes.jpg" length="10000"/>
<guid isPermaLink="false">http://www.ebaumsworld.com/pictures/view/84832600/</guid>
</item>
只需使用simplexmlement
并访问xml
节点
$xml = file_get_contents_curl($feed_url);
$x = new SimpleXMLElement($xml);
foreach ($x as $node) {
print $node->title . PHP_EOL;
print $node->description . PHP_EOL;
}
将输出
eBaum's World - Featured Media
The latest featured media
使用指向实际XML提要的url,只需通过simplexml\u load\u file()加载它即可。
:
您可以编辑您的问题以包含xml样本吗?var\u dump($xml)
给我bool(false)
@SanJeetSingh:您看过我的评论了吗?您的XML已损坏,需要更正(即添加频道和rss)。这对我很有用(复制整个答案,如果你愿意,请检查)。谢谢。是的,我确实读过那个评论,但我只是发布了部分feed,而不是完整的feed,它太大了。这里是一个完整提要的链接http://feeds.feedburner.com/ebaumsworld/aUjW
。将指向rss的链接或完整xml字符串发布到pastebin或类似内容,您应该更新您的问题,以包含文件\u get\u contents\u curl的函数定义
单击此提要中的链接将带您进入提要。这是纯文本的链接http://feeds.feedburner.com/ebaumsworld/aUjW
。我会更新代码。谢谢:)