jQuery: reperire il feed di una pagina Facebook senza PHP

jQuery: reperire il feed di una pagina Facebook senza PHP

È possibile utilizzare le API di Google per reperire un feed XML con una semplice richiesta AJAX. In questo modo è possibile reperire il feed di una pagina Facebook con jQuery senza usare PHP.

Possiamo definire il seguente plugin:


(function( $ ) {
	$.fn.facebook = function( options ) {
		options = $.extend({
			pageID: "315199031854016", // L'ID della vostra pagina
			limit: 5
		}, options);
		
		var _getFeed = function( element ) {
			var fbURL = "https://www.facebook.com/feeds/page.php?format=atom10&id=" + options.pageID;
			var feedAPI = "https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=" + encodeURIComponent( fbURL );
			
			
			$.getJSON( feedAPI + "&callback=?", function( data ) {
				var html = "";
				$( data.responseData.feed.entries ).each(function( index ) {
					var n = index + 1;
					if( n <= options.limit ) {
						html += "<div class='fb-post'>";
						html += "<a href='" + this.link + "'>";
						html += this.contentSnippet;
						html += "</a>";
					    html += "</div>";
					}
				});
				
				element.html( html );
			});
		};
		
		return this.each(function() {
			var $element = $( this );
			_getFeed( $element );
		});
	};

})( jQuery );

Esempio d'uso:


$(function() {
	$( "#facebook" ).facebook();
});

Torna su