Example of facebook app which uses offline access permission
This example is written using facebook PHP SDK v.3.0.x
I decided to refresh my knowledge of developing facebook apps. API and PHP SDK has been changed somewhat since my last time I used it. Had to update my previous posts also. I decided to create simple app that collects users access tokens (with offline_access) into mysql database and later uses these to post on their walls. I chose it because lot of people were asking me about this and I need it too soon. I will not explain lot- just download the package and read comments in code. Should be straight-forward and easy to fallow.
Check it out
This app is published at moment. Feel free to check it out or download the source code and start playing. It is also aivable at github. In my working example I use cron to fire "poster.php". There is no self-timer what people have asking about.
Files in package
In this exsample I use only 3 files. config.php, poster.php and index.php.
config.php Needs no comments. Just Fill in. Variables there should be pretty self explanatory.
index.php This collects the access tokens provided by the users. It displays authorising link. After user accepts to give permissions it saves access token into database. For simpler start I included table creation into script- so it will be created automatically (if your mysql user has CREATE permissions).
poster.php This is where real action takes place. It has simple form with some fields. This fields make up post that will be sent on each user's wall after submitting. Afcourse there should be more things covered like token check to remove old and not working ones. But I let you to handle these yourself to keep main code clean.
Set it up
In this post you can read about setting up your iframe application in facebook. It should be fairly simple. You also need to download latest 'facebook.php' (php-sdk) from github and place it next to index.php. Now you should be ready to go.
Categories:
Facebook
It worked fine until Facebook displayed a message saying that token is expired and they decided to change it for security reasons. We are going live with our software on 20th. I hope no surprises will arrive.
I think my example is out of danger. At least if we are talking about http://developers.facebook.com/blog/post/520/ this change. PHP SDK was already updated when I wrote this example.
As I see you only need to update your PHP-SDK and should be fine. But we will see. I will dig into it if something brakes.
1. Why should I save token myself in the database? It does not follow any logical scenario. If Facebook has a possibility of offline posting, why don't they have a mechanism of secure token storage? It is a bug in design of the offline postage feature or it is not allowed at all. But if it's not allowed, why do they have the offline option at all? :)
I don't want Facebook to ban my application in the middle of campaign - this is my main fear.
2. Do they usually care about backward compatibility? Should I care about same-day-release of the Java SDK and my product? They don't have early SDK preview, do they?
Thank you
2. I have not used FB API deeply to confirm it. But over last year I had to modify my app 2 times to get it working with latest PHP SDK.
Given:
- 1 testing Facebook account
- 16 real posts in database
- Copied the 16 rows to 512
Results:
- Cron executed in 59 seconds (submitted successfully)
- Posts continued showing up on the testing account for another 3 minutes
- 161 finally displayed under the account.
- 351 posts are lost
We are not going to post multiple posts to one person, but one post per person. I hope this is the problem of missing posts.
Thank you for your help!
Andrey
https://developers.facebook.com/blog/post/500/
My application collects posts from people willing to surprise their friends at specific times. The application runs for four days already. Two days out of the 4 operated fine, but other 2 returned "OAuthException: (#210) User not visible".
Did you try to delay your posts as long as 20 hours and more?
The offline access is unlimited in time, as I understand.
Your articles are really the best I've found talking about facebook apps. I'd like know if you can help me, I need change this scripts to post the message in the wall automatically, just after user authorization, and store the user's email and phone.
Thanks in advance.
i think in this line we can insert that code:
------------
$output .= "Posting message on '". $row['name'] . "' wall success";
} catch (FacebookApiException $e) {
$output .= "Posting message on '". $row['name'] . "' wall failed";
----------------------------
2. Can we make a progress bar/percentage so we know how much time the progress post ongoing (in the same page)
3. The authorize page for user in this script is going to "www.facebook.. bla.. bla.." can we make a modified so if people that access the app from mobile phone so it will deliver to "http://m.facebook" i found it in this line: "if(!$user){$loginUrl = $facebook->getLoginUrl(array".. bla.. bla.. :)
I've noticed that the posts have the same "submitter" as the destination user. Do it is possible to post a message but "appearing" as if the FB App wrote it ? (with app icon and name).
thanks
Lorenzo
Are you sure? My example should post as application not as acting user. There are "via APP NAME" below the posts. Are you talking about something else? Maybe you are talking about posting as "facebook page"?
If so- you need some extra work (but still this example can be base for that). Bu I haven't done it myself yet, therefor I can't help with that.
Yes I'm looking for posting as "facebook page" or "facebook application" entity like some FB games do on user's feeds.
With your example the app generates a message like if the user write himself in self feed.
If someone can advice is wellcome :-)
thanks again.
@Janar, I wanted to be able to count friends of users with this:
How can one use this to count the number of friends?
I'm very new to facebook API
Can you help please @janar ?
Look my other example @ https://github.com/janar/facebook-php-sdk-example-app/blob/master/index.php
Find the row:
$friendsTmp = $facebook->api('/' . $userData['id'] . '/friends');
Friends count would be there count($friendsTmp)
You could use also $facebook->api('/me/friends'); to get current users friends.
Look this page too http://developers.facebook.com/docs/reference/api/ for other endpoints in API.