2010-10-11 14 views
2

J'essaie de lire un simple flux Twitpic rss mais sans avoir beaucoup de chance. Je ne vois rien de mal avec mon code, mais son seul retour ce qui suit lors de l'utilisation print_r()Problèmes de lecture d'un flux RSS avec SimpleXML

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

Voici mon 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; 
} 

Répondre

3

Si vous souhaitez que les données comme un type spécifique , vous devez saisir explicitement:

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

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

Typecast ce qui suit explicitement (string):

$item -> title 
$item -> link 
$item -> description 
4
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; 
} 

Vous pouvez regarder votre boucle si vous êtes intéressé par plus que le dernier élément - à savoir

$article = array(); 
foreach($xml->channel->item as $item) { 
    $article[]['title'] = $item->description; 
} 
+0

trouver Nice, danlefree. – treeface