我正在尝试从this feed中提取数据。这是我的代码:
$xml = file_get_contents_curl($feed_url);
$rss = new DOMDocument();
$rss->load($xml);
函数file_get_contents_curl
从网页获取数据。将它转储到这样的var中:
var_dump($xml);
它按预期回应所有内容(我的意思是所有标题,链接等标签)。但是,如果我在var_dump
上使用$rss
:
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) "" }
现在,我无法从Feed中提取标题或其他内容。我的代码是这样的:
foreach ($rss->getElementsByTagName('item') as $node) {
$title = $node->getElementsByTagName('title')->item(0)->nodeValue;
但是,如果您在Chrome error on line 273 at column 11: Encoding error
中打开它,但它在Firefox中打开,则会出现错误。但我想我应该能够解析第一个错误点。
以下是Feed的示例:
<?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>
这是file_get_contents_curl
的函数定义:
function file_get_contents_curl($url) {
$agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
答案 0 :(得分:0)
只需使用SimpleXMLElement
并访问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
答案 1 :(得分:0)
使用您的实际XML Feed网址,您只需通过<?php
$str = '<?xml version="1.0" encoding="UTF-8"?>
$url = "http://feeds.feedburner.com/ebaumsworld/aUjW":
$xml = simplexml_load_file($url);
foreach ($xml->channel->item as $item)
echo $item->title;
// possible output:24 People Being Complete A$$holes
?>
{{1}}