Adsense

Sunday, January 13, 2013

How to retrieve posts from Facebook Page using Facebook Graph API and php


Here's a simple php script that will split out the latest 100 posts from a given Facebook Page.

Note that you will need to modify the code at lines 1 and 2 by:

Have fun trying it out! :)


<? 
 $access_token="&access_token=YOUR_ACCESS_TOKEN_HERE";
 $pageid="THE_PAGE_ID";
 $graph_query_url="https://graph.facebook.com/" . $pageid . "?fields=posts.limit(100)" .  $access_token;
 $graph_query_result = @file_get_contents($graph_query_url);
 $graph_query_obj = json_decode($graph_query_result,true);
  
 foreach($graph_query_obj[posts][data] as $p)
 {
    echo $p[from][name] . "<br/>";
    if ($p[story]) echo $p[story] . "<br/>";
    if ($p[message]) echo $p[message] . "<br/>";
    if ($p[picture]) echo "<img src='" . $p[picture] . "'><br/>";
    if ($p[likes][count]) echo "Likes:" . $p[likes][count] . "<br/>";
    echo date("Y-M-d D h:m",strtotime($p[created_time])) . "<br/>";
    echo "<hr/>";
 } // end foreach
?>
Pin It

No comments:

Post a Comment