Display latest recent posts in my facebook page into my website

September 21, 2013

Mix, PHP

How to display your facebook wall posts into your own web site.For a example you can display facebook page’s latest posts into your web site sidebar as “latest posts”. This is actually displaying as rss feeds.You can check my demo how this is implemented.

1-First go to the facebook fan page and copy the page ID from the url.

Get Facebook fan page ID



2-The put the user agent type as following.

Select Code
1
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');



3-Replace the page ID in to this rss url.

Select Code
1
2
// This URL is the URL to the Facebook Page's RSS feed.
    $rssUrl = "http://www.facebook.com/feeds/page.php?id=YOUR_PAGE_ID_HERE&format=rss20";



4-Then get feeds as xml file.

Select Code
1
$xml = simplexml_load_file($rssUrl); // Load the XML file



5-Above result is getting xml object and we can get xml data into the array.

Select Code
1
2
$data = $xml->channel->item;
    //print_r($data);



Put this all together.

Select Code
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');

    // This URL is the URL to the Facebook Page's RSS feed.
    $rssUrl = "http://www.facebook.com/feeds/page.php?id=308199725882057&format=rss20";
    $xml = simplexml_load_file($rssUrl); // Load the XML file



    // This creates an array called "data" that puts each <item> in FB's
   // XML format into the array
    $data = $xml->channel->item;

     
       $returnMarkup = '';

    /**
     * Make clickable links from URLs in text.
    */
    function make_clickable($text) {
      return preg_replace_callback(
        '#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', 
        create_function(
          '$matches',
          'return "<a href=\'{$matches[0]}\'>{$matches[0]}</a>";'
        ),
        $text
      );
    }
    
  

    /**
     * Get Title from the text.
    */
    function cleaner($url) {
      $U = explode('htt',$url);
      return $U[0];
    }

    

    for ($i = 0; $i < 5; $i++) {
         $returnMarkup .= "<h3>".cleaner($data[$i]->title)."</h3>"; // Title 
         $returnMarkup .= "<p>".make_clickable($data[$i]->link)."</p>"; // Link 
         //$returnMarkup .= "<p>description-".$entry[$i]->description."</p>"; // content
         $returnMarkup .= "<p>".$data[$i]->pubDate."</p>"; // The date published
         $returnMarkup .= "<p>".$data[$i]->author."</p><hr/>"; // The author 
    }
 
    // Facebook page feeds displaying.
     echo $returnMarkup;

NOTE:
Above example is displaying latest 5 posts.You can change number of posts in the for loop.

Check the demo I have implemented in my fan page of WebExplorar.com
View Demo>


Download Example (2 KB)

VN:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)
Display latest recent posts in my facebook page into my website, 5.0 out of 5 based on 1 rating
, ,

About Sumith Harshan

Sumith Harshan is the Managing Director of the WebXtreams(Pvt) Ltd in Sri lanka.Specializes in Java Script, JQuery, HTML4/5, CSS2/3, PHP, CodeIgniter, CakePHP, J2SE, J2EE, XML, Hacking & Security, Mobile Interface Developing, Magento, Wordpress, Joomla, Drupal and PHPBB.

View all posts by Sumith Harshan