Generating a monthly "Post Count" from Facebook API - php

I am trying to figure out how I can query the FB Graph API (or FQL API) to be able to get number of posts (or status-updates) for a given period of time. I am able to generate an access token using
$access_token = $facebook->getAccessToken();
So that would lead me to believe that the PHP SDK is installed correctly and working. When I try to use the access token in a fql query it fails giving an error message of: A user access token is required to request this resource. The query I'm trying is just to return the current status_id. I tried using uid=me() as well as using an actual user id (testing id from the API). Here is the query url:
https://graph.facebook.com/fql?q=SELECT+status_id+FROM+status+WHERE+uid=me()&access_token=
I'm not sure where I'm going off track with this, so any help would be greatly appreciated.
Thank you for looking.

This is your app id and secret, please do not ever post this to public, anyone will have full control of your application. Reset your token immediately
The PHP SDK defaults to the application access token (which should never be posted in public). You need to have the user login with getLoginUrl() an example is given at
https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php

Related

Get Facebook User's real id using App Scoped Id

I know this question already has been discussed, But I'm here with a different way and a confusion.
First : if I do debugging in Graph API Explorer it returns different app_scoped_user_id and it return me correct json when I run graph.facebook.com/$app_scoped_user_id but with my app it generate different App Scoped ID which don't works with above method. Why that happen?
Second : I thought to get last redirect url using CURL but it returns http://www.facebook.com/login.php?next=http://www.facebook.com/app_scoped_user_id/$app_scoped_user_id . Now I've two thoughts (may be stupid thoughts),
Can I open that app scoped url in a popup window and get that window url after redirection and close the popup?
Can I login in Facebook anyhow using CURL so that i can get real user_id?
I know, app scoped user id can be used. But I want to give it a try.
Warning: Facebook old API in use
There's a way to get it, but you won't like it:
get the user to log in to your app via Facebook
make a second call to the API using the retrieved access token to get their username
https://graph.facebook.com/me?access_token={THE_ACCESS_TOKEN}
make a third call to that username without an access token to get their Facebook id
https://graph.facebook/{USERNAME} <= No access token here!
EDIT: sorry, old API is used here.

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!

Simple Facebook status return using FQL

I'm trying to create a box on the side of a page that displays the 6 latest posts from a Twitter account and Facebook page. Twitter is working exactly how I want it to - Facebook is not.
The way I am doing it at the moment is to fetch the latest statuses using FQL as JSON usin cURL, caching them on the server, then displaying them in the same way as I am the tweets in the actual box.
My problem lies with the access token. I went away last night with it working, came back this morning and noticed that the modified date of my cached file hadn't changed. I checked the cached error file (where the fetched JSON gets dumped if it doesn't contain the data I need) and I had a session expired error. I then created a small function that uses cURL (3 cURL calls or one box, I know - urgh) to retrieve a new access token using the this URL:
https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=XXX&client_secret=XXXX
But I now get an error that a User Session is required, but all I want to do is to read the latest posts, that isn't user dependant at all. I really want to avoid using the Facebook SDK if possible.
Tl;dr - FQL seems to require users to log in even just to read statuses off of a set page's wall - what's the correct way of getting this data that doesn't require a user session (preferably without using the SDK).
If you're trying to get the status from a User account then you will require a valid user access token.
If you're getting statuses from a Fan Page then you just require a valid app access token - which can be obtained from:
https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials
You can then use the returned access token to get the statuses from the target Fan Page.
SELECT id,status_id,message,time FROM status WHERE uid = XXXX LIMIT 6
You're querying by user id so that will require a valid user access token.

How do I access wall posts made by the user in a facebook app

i am having trouble accessing the wall of a user. I am making a request to 'https://graph.facebook.com/'.$userID.'me/feed?access_token='.$access_token
where $access_token contains my access_token.
the strange part is that it is working well when I make the request from the application page... that is: http://apps.facebook.com/myapplication/ but when i make the same request in my callback script, it returns only those posts which are made by some application, and it does not show user's own posts like, status messages etc.
Try using the new PHP SDK, i've seen people solving bugs like yours just by changing to the SDK. It usually doesn't require you to pass the access token and supports all methods of the API (i think).
With /me/feed you can access the feed of the currently logged in user. To get the feed of some other user, you need to use their id. I am assuming that's the reason why its working in the application page and not in the script.

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