0

I do this and it works.

<?php
    function load_file($url) 
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $xml = simplexml_load_string(curl_exec($ch));
        return $xml;
    }

    $feedurl = 'http://www.astrology.com/horoscopes/daily-extended.rss';
    $rss = load_file($feedurl);

    $items = array();
    $count = 0;
    foreach ($rss->channel->item->description as $i => $description) 
    {
        $items[$count++] = $description;
    }
    echo $items[0];
?>

When I echo $items[1]; it doesn't show the next one in line. Not sure what I did wrong.

5
  • how many items are there in $rss->channel->item->description (associative) array? Commented Sep 7, 2012 at 8:30
  • var_dump($items) see what it returns. And what is the xml format ? Commented Sep 7, 2012 at 8:31
  • there are 12 <items>s and inside each <item> there is one <description> Commented Sep 7, 2012 at 8:32
  • Seems like foreach just loops one time. My guess is to iterate $rss->channel->item instead Commented Sep 7, 2012 at 8:34
  • it's an rss feed. sorry taking so long to respond. Commented Sep 7, 2012 at 8:36

1 Answer 1

4

Here is an example of your xml:

<channel>
    <item>
        <description>blah</description>
    </item>
    <item>
        <description>blah1</description>
    </item>
    <item>
        <description>blah2</description>
    </item>
    <item>
        <description>blah3</description>
    </item>
</channel>

When you do $rss->channel->item->description you're getting the first item's description.

You need to first loop through the items and then get each description.

e.g.:

$descriptions = array();
foreach($rss->channel->item as $item){
    $descriptions[] = $item->description;
    // note I don't need the $count variable... if you just use
    // [] then it auto increments the array count for you.
}

Hope that helps. Its untested, but should work.

Sign up to request clarification or add additional context in comments.

2 Comments

this makes sense, trying this now.
you're the man, this worked, it won't let me accept your answer for 4 more minutes, I appreciate it your time, I'll accept your answer as soon as it lets me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.