简体   繁体   中英

Can't figure out a for loop with an RSS feed

I'm attempting to pull in an RSS feed, but I only want to show one of the items - a random number - not all of them. I've set up a test using a for loop, but can't seem to get it to work. I come from a JS background. Any help or hints would be much appreciated!

<?php

$url = "http://abc.net.au/bestof/bestofabc.xml";

$rss = simplexml_load_file($url);

if ($rss) {

    $items = $rss->channel->item;

    for ($i = 0; $i < count($items); $i++){

        if ($i == 2) {

            echo($items[$i]); // doesn't show anything
        }
    }
}

?>

I really suggest you to use xpath to load all the items from all the channels and then randomly pick the item. Here is sample code, optimize as needed...

$url = "http://abc.net.au/bestof/bestofabc.xml";
$rss = @simplexml_load_file($url);
// get all the items in all channels
$items = $rss->xpath('//rss/channel/item');
// randomly dump one of the items from loaded list
$k = array_rand($items);
var_dump($items[$k]);

You have got basically two options here, for easy of use I assign the item to a variable of its own first:

$item = $items[$i];

And then the two options for debugging:

var_dump($item);
echo $item->asXML();

The first line will create a var_dump , which is PHP and in this case even SimpleXML specific:

class SimpleXMLElement#193 (5) {
  public $title =>
  string(29) "Asylum seeker system overload"
  public $link =>
  string(29) "http://www.abc.net.au/bestof/"
  public $description =>
  class SimpleXMLElement#287 (0) {
  }
  public $pubDate =>
  string(31) "Thu, 22 Nov 2012 00:00:00 +1100"
  public $guid =>
  string(8) "s3638457"
}

The second line will create something I bet is common to you, the XML itself:

<item>
            <title>Asylum seeker system overload</title>
            <link>http://www.abc.net.au/bestof/</link>
            <description><![CDATA[
            <img style="float:right;" src="http://www.abc.net.au/common/images/news_asylum125.jpg" alt="Asylum seeker detainees (ABC News)">
            <p>The Australian government is preparing to allow thousands of asylum seekers to love in the community.</p>
            <ul>

                <li><a href="http://mpegmedia.abc.net.au/news/lateline/video/201211/LATc_FedNauru_2111_512k.mp4">Watch (4:23)</a></li><li><a href="http://www.abc.net.au/lateline/content/2012/s3638174.htm">More - Lateline</a></li>
            </ul>
            ]]></description>

            <pubDate>Thu, 22 Nov 2012 00:00:00 +1100</pubDate>
            <guid isPermaLink="false">s3638457</guid>
        </item>

You did not see any output with:

echo $items[$i];

because that <item> element does not have a value, but just subelements. For example

echo $items[$i]->title;

Will output the string:

Asylum seeker system overload

I hope this is helpful and sheds some light. You find the demo here , it also shows that you can make use of foreach :

$i = 0;
foreach ($rss->channel->item as $item)
{
    if ($i++ == 2) {
        var_dump($item);
        echo $item->asXML(), "\n", $item->title;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM