My awesome friend Lewis King wanted to display the latest facebook post in the footer of a site that he’s building. He’s not a developer, and facebook didn’t make it easy.
The tangled web of facebook developer documentation and inadequate access token description. Most blog tutorials out there are outdated because of the introduction of required access tokens.
- Get yourself an access token (remember that you need to be an admin of the page that you want to read from). You will need to select “manage_pages” from the extended permissions section before authorising the token.
- Find your USER_ID by going to http://graph.facebook.com/USERNAME
- We have one access token, but we need to get a long-life access token for your page. You can get this and token for your apps at this URL: https://graph.facebook.com/USER_ID/accounts?access_token=ACCESS_TOKEN
- Take this new access token and go to: https://graph.facebook.com/PAGE_ID/promotable_posts?access_token=LONG_LIFE_ACCESS_TOKEN
- You will now see an object with the page’s latest posts with various fields available including the message and information about any URLs that were posts e.g. image and description.
That was the difficult bit done. It’s very simple from a code point of view to get the page post data and show the latest one and link to it.
$data = get_data("https://graph.facebook.com/PAGE_ID/promotable_posts?access_token=ACCESS_TOKEN"); $result = json_decode($data);
get_data is a wrapper function for curl.
With this information we can take the first item in the data array and get the message. Obviously this code should probably have more validation in it than it does.
<?php $latest_post = $result->data; $latest_post_text = $latest_post->message; $latest_post_link = $latest_post->actions->link; ?> <a href="<?php echo $latest_post_link ?>"><?php echo $latest_post_text ?></a>
There you go, Lewis.