Skip to content

Custom Facebook RSS Widget

For a project we wanted to bring in outside links and display them in a box on the homepage.  Instead of building out a whole custom system to handle the link, I suggested posing them through Facebook.

So in order to be able to style the feed however we wanted I put together a simple Curl code in PHP to pull in the Facebook Page RSS feed (every page has a feed it’ll be formated like this => http://www.facebook.com/feeds/page.php?id=245091913103&format=rss20).

This will check if there’s a link inside the body of the status update and use that, but then default to the link to the status message in Facebook if that doesn’t exist.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, "http://www.facebook.com/feeds/page.php?id=245091913103&format=rss20");
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
        $xmlTwitter = curl_exec($curl);
        curl_close($curl);
 
        $xmlObjTwitter = simplexml_load_string($xmlTwitter);
        $tempCounter = 0;
 
        foreach ($xmlObjTwitter->channel->item as $item) {                    
            if ( $tempCounter < 9 ){
                preg_match('/(http|https|ftp)(.*?) (id|target)/i', $item->description, $matches);
                $matches[0] = str_replace('" id','',$matches[0]);
                echo '<li><a href="'.($matches[0] ? $matches[0] : $item->link).'" target="_blank">'.$item->title.'</a>
                <div>'.date('F jS, Y',strtotime($item->pubDate)).'</div></li>';
            }
 
            $tempCounter += 1;
        }
 
        echo '</ul>';
?>
Published inPHPScripting

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *