您好我已经查看过这些论坛一段时间但似乎无法找到答案。如果之前已经回答,我很感激链接。
无论如何,我正在尝试修改我的RSS提要阅读器以包含来自Wordpress网站的图像。借助其他地方的帮助,我能够弄清楚如何处理媒体:内容节点,但是当Wordpress坚持使用“Gravatar”图像时,我一直抓住错误的图像;我只需要能够通过一个存在的图像来提供。“
这是XML :(编辑后包含更多原始源文件) 编辑注释2:我意识到博客本身存在一些关于图像编码的不一致数据,因此我可能也必须解决这个问题。可能,如果我能识别一个图像xpath,它不是像这样的gravatar图像
<media:title type="html">fsclibrary</media:title>
无论如何,这里有更多的文件
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
>
<channel>
<title>Blog Title</title>
<atom:link href="http://fsclibrary.wordpress.com/feed/" rel="self" type="application/rss+xml" />
<link>http://fsclibrary.wordpress.com</link>
<item>
<title>Worst winter ever? Find out with reliable climate data!</title>
<link>http://fsclibrary.wordpress.com/2014/02/18/worst-winter-ever-find-out-with-reliable-climate-data/</link>
<pubDate>Tue, 18 Feb 2014 14:11:37 +0000</pubDate>
<dc:creator><![CDATA[fsulibrary]]></dc:creator>
<description><![CDATA[Descriptive text here]]>
</description>
<media:content url="http://0.gravatar.com/avatar/fa0ce8e825cafda53280958a7103a9b2?s=96&d=identicon&r=G" medium="image">
<media:title type="html">fsclibrary</media:title>
</media:content>
</item>
<item>
<title>Gold Medal Information about the Winter Olympics</title>
<link>http://fsclibrary.wordpress.com/2014/02/10/gold-medal-information-about-the-winter-olympics/</link>
<pubDate>Mon, 10 Feb 2014 17:05:28 +0000</pubDate>
<dc:creator><![CDATA[fsulibrary]]></dc:creator>
<description><![CDATA[Descriptive text for another item here]]>
</description>
<media:content url="http://0.gravatar.com/avatar/fa0ce8e825cafda53280958a7103a9b2?s=96&d=identicon&r=G" medium="image">
<media:title type="html">fsclibrary</media:title>
</media:content>
<media:content url="http://fsclibrary.files.wordpress.com/2014/02/sochi.jpg?w=152" medium="image">
<media:title type="html">Image</media:title>
</media:content>
</item>
<item>
<title>Treasures from our Special Collections – Robert Cormier Correspondence</title>
<link>http://fsclibrary.wordpress.com/2013/12/04/treasures-from-our-special-collections-robert-cormier-correspondence/</link>
<pubDate>Wed, 04 Dec 2013 21:12:54 +0000</pubDate>
<dc:creator><![CDATA[fsulibrary]]></dc:creator>
<description><![CDATA[Descriptive text here for third item]]>
</description>
<media:content url="http://0.gravatar.com/avatar/fa0ce8e825cafda53280958a7103a9b2?s=96&d=identicon&r=G" medium="image"
<media:title type="html">fsclibrary</media:title>
</media:content>
<media:content url="http://fsclibrary.files.wordpress.com/2013/12/cormier-letter-1.jpg?w=226" medium="image">
<media:title type="html">Cormier Letter 1</media:title>
</media:content>
</item>
</channel>
</rss>
</xml>
以下是我对此进行编码的尝试(无济于事)。问题是我希望能够跳过第一个,但只显示media:title的值为“Image”的图像
foreach ($rss->channel->item as $feedItem) {
$i++;
$myDate = ($feedItem->pubDate);
$dateForm = explode(" ", $myDate);
while ($feedItem->children('media',true)->content->title == "Image") {
$img = $feedItem->children('media',true)->content->attributes();
}
echo "<img src=\"" . $img['url']. "\">";
(等等。这只是一个片段)非常感谢任何帮助
编辑:
答案 0 :(得分:1)
这是一个新的答案,这是如何显示所有<item>
- 具有所需image-url(s)的节点。
方法#1: xpath()
:
$rss = simplexml_load_string($x);
$i = 1;
foreach ($rss->channel->item as $item) {
echo "#$i: $item->title" . PHP_EOL;
$i++;
// xpath
foreach ($item->xpath("//item[title = '$item->title']/media:content[media:title = 'Image']/@url") as $img)
echo "URL: $img" . PHP_EOL;
}
评论:当xpath()
返回整个XML中的值时,我将item/title
作为条件包含在内,只返回此项目的子项。
方法#2:迭代“经典”方式:
$rss = simplexml_load_string($x);
$i = 1;
foreach ($rss->channel->item as $item) {
echo "#$i: $item->title" . PHP_EOL;
$i++;
// classic
foreach ($item->children("media", TRUE) as $img) {
if ($img->title == 'Image') {
foreach ($img->attributes() as $name => $value)
if ($name == 'url') echo "URL: $value" . PHP_EOL;
}
}
}
看到它有效:https://eval.in/132862
XML源无效,我在>
的示例中添加了eval.in
答案 1 :(得分:0)
xpath
是你的朋友,就像SQL for XML:
$xml = simplexml_load_string($x); // assume XML in $x
$url = (string)$xml->xpath("//media:title[text() = 'Image']/../@url")[0];
修改强>:
如果您的XML包含要选择的多个节点,请执行以下操作:
$urls = $xml->xpath("//media:title[text() = 'Image']/../@url");
foreach ($urls as $url) {
echo $url;
}
评论:
//media:title
<title>
- 名称空间为media
的节点,无论它们出现在树中... [text() = 'Image']
'Image'
... /../@url
url
[0]
xpath()
(string)
使用PHP&lt; 5.4,你会这样做:
$url = $xml->xpath(...);
$url = (string)$url[0];
看到它有效:https://eval.in/131030