Facebook RSS truncate item?

Discussion in 'HOWTO-Related Questions' started by melat0nin, May 27, 2008.

  1. melat0nin

    melat0nin New Member

    Hi all

    I've successfully implemented the Facebook RSS application, and limited the number of feed items to 8. However, I'd like to limit the amount of each item that shows, as per the Magpie tutorial here: http://www.howtoforge.com/rss_syndication_magpieRSS

    This is the code they suggest:

    Code:
    if (strlen($desc) >= 100)
    {
            $desc = substr($desc,0,99)."...";
    }
    I don't see where this can go in the rss.php code from the HOWTO.

    Any thoughts?
     
  2. falko

    falko Super Moderator Howtoforge Staff

    Try this:

    Code:
    <?php
    define('MAGPIE_CACHE_DIR', './magpie_cache');
    define('MAGPIE_CACHE_ON', 1);
    define('MAGPIE_CACHE_AGE', 600);
    
    require_once('appinclude.php');
    
    require_once('rss/rss_fetch.inc');
    $rss = @fetch_rss('http://www.howtoforge.com/node/feed');
    
    $fbml = '<div style="margin:0 10px 0 10px;">';
    $fbml .= '<table border="0" width="100%" style="margin: 5px 5px 5px 5px;"><tr><td valign="top" width="80%"><a href="'.$rss->channel['link'].'" style="font-weight: bold;">'.$rss->channel['title'].'</a></td><td valign="top" width="80%"><fb:share-button class="meta">
      <meta name="medium" content="blog"/>
      <meta name="title" content="'.htmlspecialchars(strip_tags($rss->channel['title'])).'"/>
      <meta name="description" content="'.htmlspecialchars(strip_tags($rss->channel['description'])).'"/>
      <link rel="target_url" href="'.$rss->channel['link'].'"/>
    </fb:share-button></td></tr></table>';
    
    
    foreach ($rss->items as $item) {
            $fbml .= '<div style="border-bottom: 2px solid #CCCCCC; padding-bottom:5px;"><br><div style="border-bottom: 1px dotted #CCCCCC; border-top: 1px dotted #CCCCCC;"><table border="0" width="100%" style="margin: 5px 5px 5px 5px;"><tr><td valign="top" width="80%"><a href="'.$item['link'].'" style="font-weight: bold;">'.$item['title'].'</a></td><td valign="top" width="80%"><fb:share-button class="meta">
      <meta name="medium" content="blog" />
      <meta name="title" content="'.htmlspecialchars(strip_tags($item['title'])).'" />
      <meta name="description" content="'.htmlspecialchars(strip_tags([B][COLOR="Red"]substr($item['description'],0,99)[/COLOR][/B])).'" />
      <link rel="target_url" href="'.$item['link'].'" />
    </fb:share-button></td></tr></table></div>';
            if($item['description']) $fbml .= $item['description'];
            $fbml .= '</div>';
    }
    $fbml .= '</div>';
    
    $facebook->api_client->profile_setFBML($fbml, $user);
    echo $fbml;
    ?>
     
  3. melat0nin

    melat0nin New Member

    Thanks falko, that works, although the extra code has to be put here as well:

    Code:
    <?php
    define('MAGPIE_CACHE_DIR', './magpie_cache');
    define('MAGPIE_CACHE_ON', 1);
    define('MAGPIE_CACHE_AGE', 600);
    
    require_once('appinclude.php');
    
    require_once('rss/rss_fetch.inc');
    $rss = @fetch_rss('http://www.howtoforge.com/node/feed');
    
    $fbml = '<div style="margin:0 10px 0 10px;">';
    $fbml .= '<table border="0" width="100%" style="margin: 5px 5px 5px 5px;"><tr><td valign="top" width="80%"><a href="'.$rss->channel['link'].'" style="font-weight: bold;">'.$rss->channel['title'].'</a></td><td valign="top" width="80%"><fb:share-button class="meta">
      <meta name="medium" content="blog"/>
      <meta name="title" content="'.htmlspecialchars(strip_tags($rss->channel['title'])).'"/>
      <meta name="description" content="'.htmlspecialchars(strip_tags($rss->channel['description'])).'"/>
      <link rel="target_url" href="'.$rss->channel['link'].'"/>
    </fb:share-button></td></tr></table>';
    
    
    foreach ($rss->items as $item) {
            $fbml .= '<div style="border-bottom: 2px solid #CCCCCC; padding-bottom:5px;"><br><div style="border-bottom: 1px dotted #CCCCCC; border-top: 1px dotted #CCCCCC;"><table border="0" width="100%" style="margin: 5px 5px 5px 5px;"><tr><td valign="top" width="80%"><a href="'.$item['link'].'" style="font-weight: bold;">'.$item['title'].'</a></td><td valign="top" width="80%"><fb:share-button class="meta">
      <meta name="medium" content="blog" />
      <meta name="title" content="'.htmlspecialchars(strip_tags($item['title'])).'" />
      <meta name="description" content="'.htmlspecialchars(strip_tags(substr($item['description'],0,99))).'" />
      <link rel="target_url" href="'.$item['link'].'" />
    </fb:share-button></td></tr></table></div>';
            if($item['description']) $fbml .= [COLOR="Red"]substr($item['description'],0,99)[/COLOR];
            $fbml .= '</div>';
    }
    $fbml .= '</div>';
    
    $facebook->api_client->profile_setFBML($fbml, $user);
    echo $fbml;
    ?>
    Thanks again :)
     
  4. melat0nin

    melat0nin New Member

    I've got a problem with the script. When there are no items in my RSS feed I see the following error on my facebook application page/profile:

    Code:
    Warning: array_slice() [function.array-slice]: The first argument should be an array in /data/www/domain.co.uk/www/fb/rss.php on line 20
    
    Warning: Invalid argument supplied for foreach() in /data/www/domain.co.uk/www/fb/rss.php on line 22
    
    Presumably it's because the array is empty. My php skills don't stretch to working out what to do though :(
     
  5. falko

    falko Super Moderator Howtoforge Staff

    Try this:
    Code:
    [B][COLOR="Red"]if(is_array($rss->items) && !empty($rss->items)){[/COLOR][/B]
    foreach ($rss->items as $item) {
            $fbml .= '<div style="border-bottom: 2px solid #CCCCCC; padding-bottom:5px;"><br><div style="border-bottom: 1px dotted #CCCCCC; border-top: 1px dotted #CCCCCC;"><table border="0" width="100%" style="margin: 5px 5px 5px 5px;"><tr><td valign="top" width="80%"><a href="'.$item['link'].'" style="font-weight: bold;">'.$item['title'].'</a></td><td valign="top" width="80%"><fb:share-button class="meta">
      <meta name="medium" content="blog" />
      <meta name="title" content="'.htmlspecialchars(strip_tags($item['title'])).'" />
      <meta name="description" content="'.htmlspecialchars(strip_tags(substr($item['description'],0,99))).'" />
      <link rel="target_url" href="'.$item['link'].'" />
    </fb:share-button></td></tr></table></div>';
            if($item['description']) $fbml .= substr($item['description'],0,99);
            $fbml .= '</div>';
    }
    [B][COLOR="Red"]}[/COLOR][/B]
     

Share This Page