Created
March 6, 2016 11:32
-
-
Save biojazzard/740551af0455c528f8a9 to your computer and use it in GitHub Desktop.
Get Latest Post From a Facebook Page without PHP SDK (Graph Version v2.5)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
1.- Create an App. | |
2.- Go to: https://developers.facebook.com/tools/explorer/ | |
+ Select your new created app on the right top. | |
+ Select "Get App Token" | |
3.- Copy this "{ACCESS-TOKEN}" (is in the form: number|hash) | |
IMPORTANT: (This are not app_id|app_secret!!!) | |
4.- Query the URL: | |
+ https://graph.facebook.com/{PAGE-ID}}/posts?access_token={ACCESS-TOKEN} | |
(5).- Equivalent URL | |
+ https://graph.facebook.com/{PAGE-ID}}/feed?access_token={ACCESS-TOKEN} | |
*/ | |
function curl_helper($url) { | |
$ch = curl_init(); | |
$timeout = 5; | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
$data = curl_exec($ch); | |
curl_close($ch); | |
return json_decode($data); | |
} | |
// Facebook Page: take the last post from here. | |
$page_ID = '{PAGE-ID}'; | |
// Hardcode the App Acces token obtained in point 3. (Server Side Please.) | |
$access_token = '{ACCESS-TOKEN}'; | |
$posts = curl_helper('https://graph.facebook.com/'.$page_ID.'/posts?access_token='.$access_token); | |
$latest_post = $posts->data[0]; | |
list($pageID, $postID) = preg_split('/_/', $latest_post->id); | |
$post_url = 'https://www.facebook.com/'.$pageID.'/posts'.'/'.$postID; | |
$text = '<a href="'.$post_url.'">'.$latest_post->message.'</a>'; | |
echo $text; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any easy way to make it show the last x number of posts? Also what about displaying posts from multiple pages?
Thanks