We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 35824
    • 6 Posts
    Hello there!

    I’m having a little problem with PHP and xml.

    Here is the deal, I’ve created multiple rss feeds for my website (5 in total, 1 for the news and 4 for the galeries) and i’m trying to assemble them in one.
    I’m using a friend of mine’s PHP program that retrieves multiple feeds from urls using spiefeed but the problem is that he did this one to put everything on a html page, and it was on modx EVO, i’m on REVO.
    I want to do the same thing with my 5 feeds from my website, i’ve succeeded in setting the base xml page in place (http://vgd-pinceaux.eu/test-rss.xml) but I really don’t know how to retrieve an ENTIRE item from an xml page to put it in another..

    The idea is to tell the program "for each feed, when you see <item> you copy from there to </item> and then you paste it in my new page" (the paste part is already done i think)
    I only have a few basics in PHP so i’ve been able to more or less understand my friend’s program but to do what I’m talking about I can’t find anything on the internet...

    I hope I made myself understandable, if you have any other questions don’t hesitate!

    P.S: here is the base code of my friend:

    MULTIFEED My Friend's Version
    <?php
    if (!isset ($url) || $url == "") {  
      return ''; 
    }
    
    // How many items per feed should we try to grab?
    if (!isset ($items_per_feed ) || $items_per_feed == "") {  
      $items_per_feed = 0 ;
    }  
    
    // Include the SimplePie library
    require_once($modx->config["base_path"]."assets/snippets/simplepie/simplepie.inc");
    
    $url = explode(",", $url);
    
    $first_items = array();
    
    foreach ($url as $feed_url){
    	$feed = new SimplePie();
    	$feed ->set_cache_location($modx->config['base_path'] . "assets/cache/");
    	$feed->set_feed_url($feed_url);
    	$feed->init();
    
    	// As long as we're not trying to grab more items than the feed has, go through them one by one and add them to the array.
    	for ($x = 0; $x < $feed->get_item_quantity($items_per_feed); $x++)
    	{
    		$first_items[] = $feed->get_item($x);
    	}
     
            // We're done with this feed, so let's release some memory.
            unset($feed);
    }
    
    // We need to sort the items by date with a user-defined sorting function.  Since usort() won't accept "SimplePie::sort_items", we need to wrap it in a new function.
    function sort_items($a, $b)
    {
    	return SimplePie::sort_items($a, $b);
    }
     
    // Now we can sort $first_items with our custom sorting function.
    usort($first_items, 'sort_items');
     
    // Since we're using different feeds with different settings, let's set the header manually.
    header('Content-type:text/html; charset=utf-8');
     
    // Begin our XHTML markup
    $html = '<style type="text/css"> h4.title { background-color:#DDDDDD; background-repeat:no-repeat; background-position:0 1px; padding-left:20px; font-size:larger; } </style>';
    
    if ($feed->error){
     $html .= '<p>error = '.$feed->error.'</p>';
     }
     
    $html .= '<h1>Awesome feeds : les nouvelles de nos membres.</h1>';
     
    foreach ($first_items as $item){
     
    		$html .= '<div class="chunk">';
     
    			/* Here, we'll use the $item->get_feed() method to gain access to the parent feed-level data for the specified item. */
    			$feed = $item->get_feed();
    			$html .= '<img src="'.$feed->get_favicon().'" width=16 style="float:left;" /><h4 class="title"><a href="'.$item->get_permalink().'">'.$item->get_title().'</a></h4>';
     
    //			$html .= $item->get_content();
     
    			$html .=  '<p class="footnote" style="border-top:black 1px dotted;border-bottom:black 1px dotted;">Source: <a href="'.$feed->get_permalink().'">'.$feed->get_title().'</a> | '.$item->get_date('j M Y | g:i a T').'</p>';
     
    		$html .= '</div>';
    }
    echo $html;
    ?>


    And here is the code i modified:

    MULTIFEED My Version
    <?php
    if (!isset ($url) || $url == "") {  
      return ''; 
    }
    
    // How many items per feed should we try to grab?
    if (!isset ($items_per_feed ) || $items_per_feed == "") {  
      $items_per_feed = 0 ;
    }  
    
    // Include the SimplePie library
    require_once($modx->config["base_path"]."core/components/spiefeed/includes/simplepie/simplepie.inc");
    
    $url = explode(",", $url);
    
    $first_items = array();
    
    foreach ($url as $feed_url){
    	$feed = new SimplePie();
    	$feed ->set_cache_location($modx->config['base_path'] . "core/cache/");
    	$feed->set_feed_url($feed_url);
    	$feed->init();
    
    	// As long as we're not trying to grab more items than the feed has, go through them one by one and add them to the array.
    	for ($x = 0; $x < $feed->get_item_quantity($items_per_feed); $x++)
    	{
    		$first_items[] = $feed->get_item($x);
    	}
     
            // We're done with this feed, so let's release some memory.
            unset($feed);
    }
    
    // We need to sort the items by date with a user-defined sorting function.  Since usort() won't accept "SimplePie::sort_items", we need to wrap it in a new function.
    function sort_items($a, $b)
    {
    	return SimplePie::sort_items($a, $b);
    }
     
    // Now we can sort $first_items with our custom sorting function.
    usort($first_items, 'sort_items');
     
    // Since we're using different feeds with different settings, let's set the header manually.
    header('Content-type:text/html; charset=utf-8');
     
    // Begin our XHTML markup
    
     
    $xml .= ' <?xml version="1.0" encoding="UTF-8"?>
    
    
    
    <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
    <atom:link href="[[++site_url]][[~[[*id]]]]" rel="self" type="application/rss+xml" />
    <title>[[*longtitle]]</title>
    <link>[[++site_url]]</link>
    <description>[[*description]]</description>
    <lastBuildDate>[[!getTime?format=`%a, %d %b %Y %H:%M:%S %z`]]</lastBuildDate>
    <language>fr-fr</language> ' ;
     
    foreach ($first_items as $item){
     
    	$xml .=$item->get_feed(); //this is completely wrong i know it i've tried it
     
    
    }
    
    		$xml .= '</channel>
    				</rss>';
    echo $xml;
    ?>
    



    PS2: I may have put this in the wrong forum section, if so don’t hesitate to move it elsewhere ^^’
      • 9674
      • 170 Posts
      Be kind to him !

      He is our trainee !

      Philippe
        • 35824
        • 6 Posts
        Okay now I feel a little stupid..
        The program already retrieve the items and sort them by date in an array, i didn’t understood that until now..

        My problem is now more simple : how to display them in my webpage (I think it’s like one line of code but i really don’t know the solution)
        I’ve tried this :

        foreach ($first_items as $item){
         
        	$xml .=$item;
        
        }
        


        and for some reason it won’t work either, it displays nothing and i find this in the source code of the page:

        <?xml version="1.0" encoding="UTF-8"?>
        <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
        <channel>
        <atom:link href="http://vgd-pinceaux.eu/test-rss.xml" rel="self" type="application/rss+xml" />
        <title></title>
        <link>http://vgd-pinceaux.eu/</link>
        <description></description>
        <lastBuildDate></lastBuildDate>
        <language>fr-fr</language> a56f5adca6e45169c12c6158fd48dcf1c6029092afaec857dae679fd0b10749e2d988d53cc629a5406e091a40624fafb2a0c4a63ba599d066459fb81155b079fb38adca78c153d7ea69611890a7c908ceb12bc158af4fa75378af13b8917ea740306e2517a0c7654c54c15ae427ce9591d92449fcbf64dd8f22a4d390a73fb9aef1003eaf1d66636468eaf3f774400acfbf286a37a534de6197b2bb156bbafd7</channel>
        				</rss>
        


        Any ideas?

        Thanks in advance,

        Calvin