Home » Mix » Display latest recent posts in my facebook page into my website

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.

  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.

    // 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.

    $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.

    $data = $xml->channel->item;
    //print_r($data);



Put this all together.

    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  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 "{$matches[0]}";'
		),
		$text
	  );
	}
	
  

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

    

    for ($i = 0; $i < 5; $i++) {
         $returnMarkup .= "

".cleaner($data[$i]->title)."

"; // Title $returnMarkup .= "

".make_clickable($data[$i]->link)."

"; // Link //$returnMarkup .= "

description-".$entry[$i]->description."

"; // content $returnMarkup .= "

".$data[$i]->pubDate."

"; // The date published $returnMarkup .= "

".$data[$i]->author."


"; // 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)

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.

Tags

Similar posts