appunti su come interaggire con FB per leggere lo status

appunti su come interaggire con FB per leggere lo status

[http://johndoesdesign.com/blog/2011/php/adding-a-facebook-news-status-feed-to-a-website/]

A while back I wanted to add a custom Facebook feed to a website in order to display posts from a client’s Facebook page.  The website was beautifully designed and I didn’t want to ruin the aesthetic by plonking a Facebook branded iframed news feed into it that looks like this.

So I struggled through the Facebook Developers website and after a few weeks of turmoil I was able to put together a PHP script which interacts with the Facebook API and extracts the feed data to allow you to display it however you want!  Here’s a Demo.

You can download the plugin over at Smash Balloon but for those feeling adventurous I wanted to give a brief overview of how to get and display the JSON data from Facebook in case you wanted to try it yourself.

Here’s the basic sequence of events:

1) Register as a developer with Facebook, create an “App” and obtain an Access Token.

You can follow the intructions here which will walk you through how to do that. You need this in order to access the JSON version of a Facebook page’s feed.

2) Find the Page ID of the Facebook page you want to display. 

If you have a Facebook page with a URL like this: https://www.facebook.com/nike then the Page ID is just ‘nike’. If your page URL is structured like this: https://www.facebook.com/pages/name-of-business/123654123654123 then the Page ID is actually the number at the end, so in this case ‘123654123654123′.

3) Access the JSON feed.

Simply put your Page ID and Access Token into the URL below and you now have access to a JSON array of your data.

https://graph.facebook.com/YOUR_PAGE_ID/posts?access_token=YOUR_ACCESS_TOKEN

It’ll look something like this:

facebook-feed-json

4) Get the data and decode it.

$page_id = 'YOUR_PAGE_ID';
$access_token = 'YOUR_ACCESS_TOKEN';
//Get the JSON
$json_object = @file_get_contents('https://graph.facebook.com/' . $page_id . 
'/posts?access_token=' . $access_token);
//Interpret data
$fbdata = json_decode($json_object);

5) Loop through the array of data and display each post

foreach ($fbdata->data as $post )
{
$posts .= '<p><a href="' . $post->link . '">' . $post->story . '</a></p>';
$posts .= '<p><a href="' . $post->link . '">' . $post->message . '</a></p>';
$posts .= '<p>' . $post->description . '</p>';
$posts .= '<br />';
}

 

Putting it all together we get:

$page_id = 'YOUR_PAGE_ID';
$access_token = 'YOUR_ACCESS_TOKEN';
//Get the JSON
$json_object = @file_get_contents('https://graph.facebook.com/' . $page_id . 
'/posts?access_token=' . $access_token);
//Interpret data
$fbdata = json_decode($json_object);

foreach ($fbdata->data as $post )
{
$posts .= '<p><a href="' . $post->link . '">' . $post->story . '</a></p>';
$posts .= '<p><a href="' . $post->link . '">' . $post->message . '</a></p>';
$posts .= '<p>' . $post->description . '</p>';
$posts .= '<br />';
}

Which looks something like this: http://johndoesdesign.com/dev/fbfeed/

To view the basic PHP code behind the plugin then check out the original tutorial here. Or if you want to add photosvideosevents, be able to filter out certain post types and display likesshares and comments then check out the Custom Facebook Feed plugin.

 

Some notes:

1) The Facebook page must be public, meaning that should so be able to access the page without having to log into Facebook. Here’s some more information on how to check whether your page is public or not.

2) If your web server PHP settings have allow_url_fopen set to Off, then file_get_contents won’t work. If this is the case then you have two options: either set it to on by editing your php.ini file (on apache) and setting allow_url_fopen = On, or use an alternative method to get the JSON – such as cURL – as demonstratedhere.

 

If you’re struggling to get your feed to work or just don’t want to spend the time figuring out how to display it the way you want then consider purchasing the plugin I built.  It’s available as both a WordPress plugin and PHP Standalone version, is very customizable and allows you to display full-width photos, videos, events, comments and more.

Check out the demo, and if you have any questions then just let me know in the comments below.

 

 

 

LINK PER OTTENERE TOKEN

https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=APPID&client_secret=APPSECRET

 

LINK PER PRENDERE LO STATUS

https://graph.facebook.com/35760318756/posts?access_token=166912163505303|u__H3ZsTIeCYY78m6tJ09TxfJFw

 

35760318756–> è il codice id della pagina pubblica da cui lo leggo

 

 

TUTORIAL PER USARE js SDK

http://hayageek.com/facebook-javascript-sdk/

 

SNIPPETTINO

function getLastStatus(){
$openUrl = “https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=|*APPID*|&client_secret=|*APPSECRET*|”;
$openUrlToken = “https://graph.facebook.com/35760318756/posts?access_token=166912163505303|u__H3ZsTIeCYY78m6tJ09TxfJFw”;
$handle = file_get_contents($openUrlToken);
$getted = json_decode($handle);
$getted= array(‘post’=>$getted->data[0]->message,’excerpt’=> substr($getted->data[0]->message, 0,90).’ …’, ‘img’=>$getted ->data[0]->picture);
return $getted;
}