Hello I'm trying to get the FB feed from a user profile on my webpage: I'm using an app and the following is the code that I have tried:
<?php
//function to retrieve posts from facebook’s server
$token = 'https://graph.facebook.com/oauth/access_token?client_id=xxxxxx&client_secret=zzzyyyxxx&grant_type=client_credentials';
$token = file_get_contents($token);
function loadFB($fbID){
$url = "https://graph.facebook.com/".$fbID."/feed?limit=10";
$url.= '&access_token=xxxxxx|TLd1z0EZNH1SD1VfA';// *
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$page = json_decode(curl_exec($c));
var_dump($page);
curl_close($c);
if(isset($page->data)){
return $page->data;}else{echo "aabrakadaabra";}
}
$fbid = "myfbid";
date_default_timezone_set("America/Chicago");
$myPosts = loadFB($fbid);
foreach($myPosts as $dPost){
if($dPost->from->id==$fbid){
$dTime = strtotime($dPost->created_time);
$myTime=date("M d Y h:ia",$dTime);
?>
<ul>
<li><?php echo($dPost->message) . $myTime; ?></li>
</ul>
<?php
}
}
?>
Which is working fine, since I'm getting no error and to confirm the above I tried to see the feeds directly using the url below and it's returning empty data.
https://graph.facebook.com/myID/feed?&access_token=myToken
This is returning:
{
"data": [
]
}
I have tried several other answers posted on SO, but non helped.
User profiles are not really made for using the feed on an external Website, but if you really want to do that you will have to use a User Access Token to access it. Authorize with the read_stream
permission to get a User Access Token.
IMPORTANT: User Tokens are only valid for 60 days, after that you will have to refresh it. And you can´t do that automatically on your server.
Information about Authorization: https://developers.facebook.com/docs/facebook-login/login-flow-for-web/v2.2
You can only access a User feed with a User token of that specific User.
Better solution: User a Page, you don´t need to authorize for that, you only need an App Access Token.
More information about Tokens:
Add a scope 'read_stream' on login call of facebook.
Then use
$this->facebook->setAccessToken('access_token'); $this->facebook->api('/user_id/feed');
BUT feed will only disaply when user give you the permission to access data