Skip to content

Instantly share code, notes, and snippets.

@krasimir
Last active December 10, 2015 07:38
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 krasimir/4402618 to your computer and use it in GitHub Desktop.
Save krasimir/4402618 to your computer and use it in GitHub Desktop.
Get your latest Facebook activities.
<?php
class FacebookFeed {
private $appID;
private $appSecret;
private $fbID;
private $appToken;
public function __construct($appID, $appSecret, $fbID, $appToken = false) {
$this->appID = $appID;
$this->appSecret = $appSecret;
$this->fbID = $fbID;
$this->appToken = $appToken;
}
public function getFeed($count = 10) {
// 1.
// getting access token for your application
// more information -> https://developers.facebook.com/docs/howtos/login/login-as-app/
if($this->appToken === false) {
$app_id = $this->appID;
$app_secret = $this->appSecret;
$app_token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&client_secret=" . $app_secret
. "&grant_type=client_credentials";
$tokenData = $this->request($app_token_url);
$this->appToken = str_replace("access_token=", "", $tokenData);
}
// 2.
// don't forget to authorize the app to read your wall's data. Login in facebook and visit
// https://www.facebook.com/dialog/oauth/?client_id=APP_ID&redirect_uri=THE_URL_ADDED_IN_THE_APP&scope=read_stream,user_status
// 3.
// getting the feed
if($this->appToken && $this->appToken != "") {
$feed_url = "https://graph.facebook.com/".$this->fbID."/feed?access_token=".$this->appToken."&limit=".$count;
$feedData = $this->request($feed_url);
return $feedData;
} else {
return false;
}
}
private function request($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
}
$ff = new FacebookFeed(YOUR_APP_ID, YOUR_APP_SECRET, YOUR_FACEBOOK_PROFILE_ID);
if($feedData = $ff->getFeed()) {
// success
} else {
// failed
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment