问题
I am trying to pull facebook posts onto my site so that I can save and display it in my own format. I am trying to use the facebook-php-sdk library, found on the Facebook Developer pages. I used the following code after I required the library:
$facebook = new Facebook(array(
'appId' => 'MY APP ID',
'secret' => 'MY APP SECRET',
));
// Get User ID
$user = $facebook->getUser();
print_r($facebook);
print_r($user);
The print_r into $facebook showed me a full facebook object, so I know its being created properly.
object(Facebook)#325 (9) { ["sharedSessionID":protected]=> NULL ["appId":protected]=> string(15) "[MYAPPID]" ["appSecret":protected]=> string(32) "[MYAPPSECRET]" ["user":protected]=> int(0) ["signedRequest":protected]=> NULL ["state":protected]=> NULL ["accessToken":protected]=> string(48) "[MYAPPID]|[MYAPPSECRET]" ["fileUploadSupport":protected]=> bool(false) ["trustForwarded":protected]=> bool(false) }
(I took my app id and app secret codes out of the quote above).
However, the print_r into $user displayed
int(0).
What am I missing here?
回答1:
“What am I missing here?” Authenticating the user. You’re calling getUser()
, but until you authenticate, Facebook doesn’t know which user to get details for. I’m also unsure why you’ve missed it, as it’s the example on the GitHub repo for the PHP SDK.
回答2:
I don't know what this getUser()
function is, but what you have to do is very very simple and you don't even have to use a huge library (I prefer to use what I can check easily, or what I have written, if I manage). It looks like a duplicate of this question, or the other way around. Anyhow, I am repeating it here too:
In order to get your feed, you have to use a token. To retrieve that token, make a cURL call to using this url https://graph.facebook.com/oauth/access_token?type=client_cred&client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET
Let's say you got it already and you called it $token
. Use that to get your feed.
$pageFeed = someCURLfunction("https://graph.facebook.com/{$yourid}/feed?{$token}");
//you don't need a login. You have to get an auth token though
$jsonStuff = json_decode($pageFeed); //it's a json object. You need to decode it
foreach ($jsonStuff as $feed)
{
//loop through it and
// finish it here
}
来源:https://stackoverflow.com/questions/18533184/trying-to-use-use-facebook-php-sdk-library-to-retrive-facebook-posts