Hi I've looked over these forums for a while but can't seem to find an answer. If it has been answered before, I appreciate a link.

anyway, I'm trying to modify my RSS feed reader to include images from a Wordpress site. With help other places here I was able to figure out how to deal with media:content nodes, however as Wordpress insists on using "Gravatar" images, I keep grabbing the wrong image; I just need to be able to feed through an image where one exists."

Here's the XML: (edited to include more of the original source file).
Edit note 2: I'm realizing that there's some inconsistent data coming from the blog itself regarding encoding of images, so I may have to get around that as well; possibly if I could identify an image xpath where it is NOT the gravatar image such as this

<media:title type="html">fsclibrary</media:title>

Anyway, here's more of the file

<?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&#38;d=identicon&#38;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&#38;d=identicon&#38;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 &#8211; 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&#38;d=identicon&#38;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>

Here are my attempts at coding this (to no avail). The problem is that I want to be able to skip the first one, but only display the image shown where media:title has a value of "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']. "\">";

(etc. this is just a snippet) Any help will be greatly appreciated

Edit:

The XML source is coming from here: http://fsclibrary.wordpress.com/feed/

有帮助吗?

解决方案

Here's a fresh answer, this is how to display all <item>-nodes with the desired image-url(s).

Method #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;
}

Comment: As xpath() returns the values from the whole XML, I included the item/titleas condition to return only this item's children.

Method #2: iterating the "classic" way:

$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;
       }
   }  
}

see it working: https://eval.in/132862

The XML source is not valid, I added a > in my example at eval.in

其他提示

xpathis your friend, it is like SQL for XML:

$xml = simplexml_load_string($x); // assume XML in $x

$url = (string)$xml->xpath("//media:title[text() = 'Image']/../@url")[0];

EDIT:
if your XML contains several nodes you want to select, do it like this:

$urls = $xml->xpath("//media:title[text() = 'Image']/../@url");

foreach ($urls as $url) {
    echo $url;
}

Comments:

  • //media:title
    select all <title>-nodes with the namespace media wherever they occur in the tree...
  • [text() = 'Image']
    where the content = 'Image'...
  • /../@url
    then go one step up and select the parent's attribute url
  • [0]
    works with PHP >= 5.4 and selects the first element of the array returned by xpath()
  • (string)
    force the result to string.

With PHP < 5.4, you'd do:

$url = $xml->xpath(...);
$url = (string)$url[0];

see it working: https://eval.in/131030

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top