Flux rss : comment récupérer uniquement le 2eme item ?

aurelien59

WRInaute impliqué
Bonjour à tous,

Je récupère un flux rss comme ceci :

Code:
<?php
require_once("feedparser.php");
echo
 FeedParser("*****", 1);
?>

Je souhaite n'affiche que le 2éme item. Comment faire sachant que la page xml est du style ? :

Code:
<item>
		<title>***</title>
		<link>http://www.***.net/</link>
		<description></description>
</item>
<item>
		<title>***</title>
		<link>http://www.***.net/</link>
		<description></description>
</item>
<item>
		<title>***</title>
		<link>http://www.***.net/</link>
		<description></description>
</item>


Merci à vous
 

spout

WRInaute accro
On ne sait pas ce que FeedParser te renvois, allez au pif: si c'est un array:
PHP:
<span class="syntaxdefault"></span><span class="syntaxkeyword"><?</span><span class="syntaxdefault">php<br />$feed&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">FeedParser</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"*****"</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">$feed</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">]&nbsp;</span><span class="syntaxdefault"></span>
 

aurelien59

WRInaute impliqué
Non ca ne marche pas.

Le feedparser envoi


Code:
<?php
// inclusion de la classe magpierss
require_once("magpierss/rss_fetch.inc");

function FeedParser($url_feed, $nb_items_affiches=10)
{
  // lecture du fichier distant (flux XML)
  $rss = fetch_rss($url_feed);

  // si la lecture s'est bien passee,
  // on lit les elements
  if (is_array($rss->items))
  {
   // on ne recupere que les elements les + recents
   $items = array_slice($rss->items,
    0, $nb_items_affiches);

   // debut de la liste
   // (vous pouvez indiquer un style CSS
   // pour la formater)
   $html = "\n";

   // boucle sur tous les elements
   foreach ($items as $item)
   {
    $html .= "";
    $html .= $item['title']."";
   }
   $html .= "\n";
 }

 // retourne le code HTML a inclure dans la page
 return $html;
}
?>
 

spout

WRInaute accro
Remplacer:
PHP:
<span class="syntaxdefault"></span><span class="syntaxkeyword">foreach&nbsp;(</span><span class="syntaxdefault">$items&nbsp;</span><span class="syntaxkeyword">as&nbsp;</span><span class="syntaxdefault">$item</span><span class="syntaxkeyword">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">$html&nbsp;</span><span class="syntaxkeyword">.=&nbsp;</span><span class="syntaxstring">""</span><span class="syntaxkeyword">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">$html&nbsp;</span><span class="syntaxkeyword">.=&nbsp;</span><span class="syntaxdefault">$item</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'title'</span><span class="syntaxkeyword">].</span><span class="syntaxstring">""</span><span class="syntaxkeyword">;<br />}<br />&nbsp;</span><span class="syntaxdefault"></span>
Par:
PHP:
<span class="syntaxdefault">$item&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">$items</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">];<br /></span><span class="syntaxdefault">$html&nbsp;</span><span class="syntaxkeyword">.=&nbsp;</span><span class="syntaxdefault">$item</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'title'</span><span class="syntaxkeyword">];&nbsp;</span><span class="syntaxdefault"></span>
 

spout

WRInaute accro
Ah ben oui vu qu'il y a un array_slice, sorry:
PHP:
<span class="syntaxdefault">$items </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> array_slice</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$rss</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">items</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 1</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 1</span><span class="syntaxkeyword">);</span><span class="syntaxdefault"></span>

Bon plutôt que d'utiliser Magpie RSS qui date de l'an 40:
PHP:
<span class="syntaxdefault"></span><span class="syntaxkeyword"><?</span><span class="syntaxdefault">php<br />$url </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">'https://www.webrankinfo.com/forum.rss'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">$xml </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> simplexml_load_file</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$url</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'SimpleXMLElement'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> LIBXML_NOCDATA</span><span class="syntaxkeyword">);<br /><br /></span><span class="syntaxdefault">if </span><span class="syntaxkeyword">(!empty(</span><span class="syntaxdefault">$xml</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">channel</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">item</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">]))</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">    echo sprintf</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'<a href="%s">%s</a>'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $xml</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">channel</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">item</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">]-></span><span class="syntaxdefault">link</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $xml</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">channel</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">item</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">]-></span><span class="syntaxdefault">title</span><span class="syntaxkeyword">);<br />}</span><span class="syntaxdefault"></span>
 

Discussions similaires

Haut