Example of facebook app which uses offline access permission

Facebook iframe example app - batch posting (poster.php) 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.

No gravatar aivable
Andrey #
I have the same code (almost) but without getUser() function. I post on friends wall and the only option I have is saving tokens, posting and cleaning tokens from my DB for security reasons (just in case).

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.
Janar jürisson #
Thank you for your info.

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.
No gravatar aivable
Andrey #
There are two things that bother me:

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
Janar jürisson #
1. Actually, I don't understand exactly what you are referring to. And also I think app banning is not relevant to current post topic.

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.
No gravatar aivable
Andrey #
Here are some results for you if you interested.

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
No gravatar aivable
Lucas #
thanks a lot man, i really appreciate your fb-related posts!
No gravatar aivable
BigBee #
Good stuff. Your contributions are much appreciated.
No gravatar aivable
BigBee #
Here is a link to a blog post with code on how to Refresh lost access_tokens.
https://developers.facebook.com/blog/post/500/
No gravatar aivable
Andrey #
Well.

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.
No gravatar aivable
Alexandre de Deus #
Hi Janar.

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.
Janar jürisson #
to Andrey: http://developers.facebook.com/docs/reference/api/permissions/ read offline_access description there. It should answer your question. But I have not tested this 1-2 days later. Probably soon I'll need it in one of my works sos I can test it.
No gravatar aivable
Zoelfikr (its me again) #
1. Can we make delay between post without cron? it is like counting down at rapishare or mediafire link download :) i belive it can, but i dont know to figure it. (i just googling and find the tutorial how to make counting link download like at rapidshare but i dont know to figure it.

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.. :)
No gravatar aivable
Mirza Ali Nasrullah Khan #
Thanks Buddy u rock....!!!
No gravatar aivable
Lorenzo #
Hi Janar,
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
Janar #
Hi 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.
No gravatar aivable
Lorenzo #
Thank you Janar.
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.
No gravatar aivable
Lorenzo #
Uhm looking better I'm wrong. All FB Apps publish on user's feeds as if the use is writing himself....

No gravatar aivable
Joe #
Wao! This works like charm

@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 ?
Janar #
I know it is possible to get friends list. So You could get total count based on that.

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.

I am co-founder of web/media studio GIVE me. and (android)developer at start-up named Choco. Read my about page to learn more.