Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tusko/e775ec713e3a5d132394251d37b6b2b1 to your computer and use it in GitHub Desktop.
Save Tusko/e775ec713e3a5d132394251d37b6b2b1 to your computer and use it in GitHub Desktop.
Get Latest Post From a Facebook Page without PHP SDK (Graph Version v2.5)
<?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;
?>
<?php
/**
Facebook Posts no-SDK
*/
function get_fb_posts($page_ID, $access_token){
$fb__posts = wp_remote_get('https://graph.facebook.com/'.$page_ID.'/posts?access_token='.$access_token);
$fb__feed = get_transient( 'fb_feed' );
if ( false === $fb__feed || empty($fb__feed) ) {
$data = json_decode( $fb__posts['body'] );
$fb__feed = $data->data;
set_transient( 'fb_feed', $fb__feed, 12 * '3600' );
}
return $fb__feed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment