How to get a Facebook user's ID? - php

I followed this tutorial: http://developers.facebook.com/docs/authentication/
on how to setup Facebook authentication using php (server-side implementation). I can get a access token back from Facebook but how do I get the Facebook user ID of the user who has logged in?

You should use facebook PHP SDK:
https://github.com/facebook/php-sdk/
See the example file in order not to get Lost...
https://github.com/facebook/php-sdk/blob/master/examples/example.php
Facebook graph API is kinda annoying... so using this will be easier (at least for me)

The easiest way, if your framework/sdk/whatever doesn't provide it for you, would be to query https://graph.facebook.com/me?fields=id&access_token=....

If you're looking for a quick non-programatic way of getting your user-id, it's in your browser cookie called c_user I believe

Related

How to query Facebook's graph-api?

I am using PHP 5.3.8, therefore I can't use the latest Facebook SDK which works only with PHP 5.4.X.
given a url, I'd like my server to tell if this is a facebook-like-page, or not.
Do I have to use OAuth? I used to use the graph-api, but now I get all weird errors. like no access-token provided, no authorization etc.
My code was using cURL with GET and accessed https://graph.facebook.com/.urlencode("THE_PAGE_URL");
I don't know if this is not an overkill to do a whole authorization from my server, and if so, to whom? I must say I am lost in the facebook documentation, frankly.
I tried this sample code from Facebook, which does not work at all.
Is there a simple way to get info about a public like-page from facebook?
An easy way to make calls using the Facebook Graph API is with their Graph API Explorer.
You have to be logged into your Facebook developer account to access it obviously, but it is really helpful if you're experimenting with making different calls to a Facebook page.
Regarding oAuth, I would also highly recommend that you obtain a permanent page access token. It's the best way to mitigate the complexities of Facebook's oAuth process.
facebook: permanent Page Access Token?

Check if user like a page without creating facebook app

I have to check in my script if a user like a facebook page.
I would realize it without Facebook app. I would not create it.
I founded some examples, but they require a Facebook app, and that user gives it the correct permission.
Can you help me? Is it possible?
Thanks for any suggestion.
You need to use the Facebook API and ask for permission from the user to do this.
Resources:
Facebook API info
User info
If your application is an external website, then you need to use the Facebook API and ask for the user_likes permission + valid access and make a POST to USER_ID/likes/PAGE_ID
If your application is a canvas or a tab application, then you can use the signed_request parameters that Facebook send you.
https://developers.facebook.com/docs/howtos/login/signed-request/
https://developers.facebook.com/docs/reference/api/user/
Regards

How can I get the Facebook friends of a visiting user who hasn't installed my application?

I'm using the latest PHP SDK and have tried
$facebook->('/me/friends?fields=installed')
and it tells me I require an access token.
I tried $facebook->getUser(), and I get zero even though I am logged into Facebook.
I'm thinking that I figure out a user's friends who are on the application if I have the user's Facebook ID or with the fields=installed method.
Is there an easier way? If not, why am I having trouble getting the ID?
It turns out this can be accomplished rather trivially with a "Facepile". See https://developers.facebook.com/docs/reference/plugins/facepile/

Easy way to get facebook statuses from a particular page in PHP?

I need to grab the statuses from a Facebook page via PHP to display on the page. I don't want to have people logging in to facebook just for my script to grab the statuses from the page since it is a public page and isn't restricted. I have the Facebook API for PHP in my projects directory but have no idea how to use it to do what I want it to. Could someone give me a code example of how I'd do this? Thanks!
It's really a straight forward task if you follow the documentation. Since /PAGE_ID/feed requires any valid access_token, your app access_token is enough to get the data:
Open the Graph API Explorer (I'm querying coca-cola/feed)
Get an app access_token from the Access Token Tool and use it instead of yours
You're done!

How to post to facebook wall once I've got the access token

I'm making a facebook app and have got as far as getting the user to allow my apps permissions, which then generates a code. Then using this code to get an access token. So I now have a variable in my PHP script containing the access token. Now how do I use this to post to the users wall?
The easiest way to post a message on the wall is through the graph api. You require the publish_stream permission to post status messages. You can simply do a POST with the desired message and the access token.
See the official facebook documentation for more details: http://developers.facebook.com/docs/reference/api/status/
Facebook changes their API so often, it's best to read their docs, if those are up to date...
Check in IRC #facebook as well.
But basically, you pass that token in to the API call somewhere, somehow and it's in their docs somewhere.
Unless FB has decided you don't need it or ignores it or doesn't want to allow that this week.

Categories