Quantcast
Channel: Why does Simplepie return feed items in a wrong order? - WordPress Development Stack Exchange
Viewing all articles
Browse latest Browse all 5

Answer by jimihenrik for Why does Simplepie return feed items in a wrong order?

$
0
0

I have no idea what's happening with this feed but with this code

include_once( ABSPATH . WPINC . '/feed.php' );
$rss = fetch_feed( 'https://picasaweb.google.com/data/feed/base/user/110981376645512804522/albumid/5638371435917293985' );

foreach ($rss->get_items() as $item) {
    if ($enclosure = $item->get_enclosure()) {
        echo '<img src="'.$enclosure->get_link().'">';
    }
}

I get the same order that I get when opening that feed with my browser. If I leave the ?alt=rss&kind=photo&hl=en_US in the url, it gives the problems you described and the items are in the wrong order.

No idea why, but maybe you can work forward from here.

Edit: So in short, maybe the problem was the url of the feed?


Here's a hacky way to get the right feed:

$url = 'https://picasaweb.google.com/data/feed/base/user/110981376645512804522/albumid/5638371435917293985?alt=json';
// or https://picasaweb.google.com/data/feed/base/user/117541338986679044195/albumid/6129832223894216561?alt=json&kind=photo&hl=en_US&imgmax=800
$json = json_decode(file_get_contents($url));
$feed = (array)$json->feed;
foreach ($feed['entry'] as $entry) {
    $i = 0;
    foreach ($entry->content as $image) {
        if($i % 2 != 0) { echo '<img src="'.$image.'" />'; }
        $i++;
    }
}

So I switched to json feed and parse it kinda uglily. But it gets the job done :D the $entry->content gives both image type and source which is why there's that extra if($i % 2 != 0) to get only every other node.

Still, I have no clue why the RSS does that, but here's alternative way anyway.

edit: to add again you said you really couldn't parse the rss manually so I'll just say that you can add &prettyprint=true to the url to get a prettier feed if you'd like to read it.


edit number thousand: Alternative way!

include_once( ABSPATH . WPINC . '/feed.php' );
$rss = fetch_feed( 'https://picasaweb.google.com/data/feed/base/user/117541338986679044195/albumid/6129832223894216561?alt=rss&fields=item(media:group(media:content(@url)))' );

foreach ($rss->get_items() as $item) {
    if ($enclosure = $item->get_enclosure()) {
        //echo $enclosure->get_link().'<br>';
      echo '<img src="'.$enclosure->get_link().'">';
    }
}

by adding ?alt=rss&fields=item(media:group(media:content(@url))) I got the two feeds you have provided thus far to work correctly! I got all the funny information to try out from here.


Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>