views:

55

answers:

3

I am trying to read a simple Twitpic rss feed but not having much luck. I can't see anything wrong with my code, but its only returning the following when using print_r()

Array ( [title] => SimpleXMLElement Object ( ) )

Here is my code:

function get_twitpics() {

    /* get raw feed */

    $url = 'http://www.twitpic.com/photos/Shealan/feed.rss'; 
    $raw = file_get_contents($url); 
    $xml = new SimpleXmlElement($raw);

    /* create array from feed items */

    foreach($xml->channel->item as $item) {

        $article = array();
        $article['title'] = $item->description;
    }

    return $article;
}
+3  A: 

If you want the data as a specific type, you'll need to explicitly type it:

foreach($xml->channel->item as $item) {

    $article = array();
    $article['title'] = (string) $item->description;
}
Tim Lytle
+1  A: 

Typecast the following explicitly to (string):

$item -> title
$item -> link
$item -> description
stillstanding
+4  A: 
foreach($xml->channel->item as $item) {
    $article = array(); // so you want to erase the contents of $article with each iteration, eh?
    $article['title'] = $item->description;
}

You might want to look at your for loop if you're interested in more than just the last element - i.e.

$article = array();
foreach($xml->channel->item as $item) {
    $article[]['title'] = $item->description;
}
danlefree
Nice find, danlefree.
treeface