Since Facebook has stopped supporting Java we need to use PHP for now to post on a wall.
According to facebook Wiki posting on a wall is very confusing and very difficult to write a program to publish on a wall.
I spent around 2 weeks reading many articles and many documents, Wiki on facebook but in vein and tried many ways and finally I got one workable model. Below are 5 simple steps to publish on a Facebook wall.
Step1: Login to Facebook to Create a Facebook App
First login to Facebook and goto the url http://www.facebook.com/developers/ then
Click on “+ Set Up New Application” button to start creating the application as shown below.
Provide the application name, click agree for facebook terms and click on “Create Application” button.
Fill in application Name
Now Application is created for you. Just copy the Application API Key, Application Secret and Application ID details which will be used to write the application code.
Fill the Name and Description of your application in About Link.
Now Click on Website Link to get API Key
Now goto Facebook Integration link and enter any url. Here is where you will get Secret Key.
Enter the Canvas URL and page name. This will be the return URL of your Application.

That’s it You now just successfully create a Facebook Application, now lets work on program
Then click on Save. That’s it we have created a facebook application with an Iframe.
Now Copy Application API Key, Application Secret and Application ID details which will be used to write the application code.
Now download the php library from facebook “http://blog.theunical.com/wp-content/uploads/2010/05/facebook-platform.tar.gz”
Step2: Generate One Time Token to generate Session key
How to generate One Time Session key:
Run below URL to get temporary token for the particular user by logging into Facebook.
https://login.facebook.com/code_gen.php?api_key=API_KEY&v=1.0
Note: Please replace “API_KEY” with your application API key from above page.
Then we will get a temporary token key.
This is one time token key that we can use to generate a permanent session key. Careful! Don’t try to execute this programs many times
Step3: Generate one time session key (permanent session key)
Below is the sample PHP code to generate one time session key.
<?php // FB_APIKEY is your facebook application api key // FB_SECRET is your application secrete key $FB_APIKEY="YOUR_API"; $FB_SECRET="YOUR_SECRET"; $fb = new FacebookRestClient($FB_APIKEY, $FB_SECRET); $testtoken= "ONETIMETOKEN"; // Replace this value with your Token Value $result = $fb->call_method('facebook.auth.getSession', array('auth_token' => $testtoken, 'generate_session_secret' => true)); echo "<br /><pre>"; print_r($result); echo $session_key = $result['session_key']; ?>
(OR)
For example, the full URL for logging in a user could be:
If the user is redirected to the URL specified by the next
parameter, then Facebook grants your application a session. This session is appended to the URL as a JSON-decodable object of the form:
In continuing with the above example, the redirect for a successful login would be:
If the user grants your application the offline_access extended permission, 0 gets returned for expires
and the session never expires unless the user removes the application. In this case, you should store the session key so the user doesn’t have to log in the next time he or she launches your application.
Note: The above code will execute only once. We should note down the above session key, This will be used to auto login into the application.
If you don’t save these session key values you should generate other token key to create one time session key.
We can use the token only once.
Step4: Setting Permission for wall
Giving Permission to your application to publish on facebook wall. To give permission just replace the your API key in below URL and execute in browser.
http://www.facebook.com/login.php?api_key=APIKEYXxxxxxxxxxxxxxxxxx&connect_display=popup&v=1.0&next=http://www.facebook.com/connect/login_success.html&cancel_url=http://www.facebook.com/connect/login_failure.html&fbconnect=true&return_session=true&req_perms=read_stream,publish_stream,offline_access
Then you must see the below 3 Screens
If you don’t see the below then your call back URL or Canvas URL must be incorrect, please check again your application settings and execute the URL again.
Then on final page, you will see a success message. That’s it you have given permission for read_stream,publish_stream and offline_access.
Step5: Publishing the message on Facebook Wall
PHP Code to create facebook object with onetime session key and publishing the message on Facebook Wall.
<?php define('FB_APIKEY', 'YOUR_APIKEY'); define('FB_SECRET', 'YOUR_SECRET'); define('FB_SESSION', 'YOUR_SESSION_key'); require_once('facebook-platform/php/facebook.php'); echo "post on wall"; try { $facebook = new Facebook(FB_APIKEY, FB_SECRET); $facebook->api_client->session_key = FB_SESSION; $fetch = array('friends' => array('pattern' => '.*', 'query' => "select uid2 from friend where uid1={$user}")); echo $facebook->api_client->admin_setAppProperties(array('preload_fql' => json_encode($fetch))); $message = 'From My App: publish steven on facebook'; if( $facebook->api_client->stream_publish($message)) echo "Added on FB Wall"; } catch(Exception $e) { echo $e . "<br />"; } ?>
Now you will see the post on wall Great.
So thrilling isn’t we can also publish photos videos on wall.
In my next post see how to publish images and videos to facebook is done
http://blog.theunical.com/facebook…/add-picture-on-facebook-wall-using-php/
http://blog.theunical.com/facebook…/post-video-on-facebook-wall-using-php/
See here how to publish on facebook page as a facebook user
http://wiki.developers.facebook.com/index.php/Stream.publish
have a great day
— Steven
Please don’t copy this content to any site. This is fully protected by TheUnical Technologies
Post Laster Updated on 13 March 2011
I am Stuck in Step 2
Generate One Time Token to generate Session key
I am Not able to get the token , an error occurred while clicking the link with my app key…
Raj Check if you created your Facebook app correctly.
FaceBook is deprecating some APIs from Jan 11, so please beware of when you create facebook apps
http://blog.theunical.com/facebook-integration/facebook-deprecating-some-apis-from-07-jan-11/
it seems that some people here are stuck at step 2. I had the same problem (see post 198), and did some further research. it turns out that there is a simple alternative solution with the Graph API- got it working with a 4 step procedure as described below.
It is still a manual solution though… I would be very interested in getting this automated. apparently one should use Graph API in future anyways, as some of the API functions used above might be deprecated soon. So a combination of the solution posted below and the php-solution to automatise this procedure as described in previous posts would be terrific… ideas anyone?
Here’s the how I did it with the Graph API:
1) Register a Facebook application and get App ID and App Secret http://developers.facebook.com/docs/guides/canvas
2) Ask the user for permission to publish posts on his wall
(see http://developers.facebook.com/docs/authentication/#authenticating-users-in-a-web-application)
https://graph.facebook.com/oauth/authorize?client_id=FACEBOOK_APP_ID&redirect_uri=http://www.URL/&scope=offline_access,publish_checkins,publish_stream
– with client_id = your Facebook app ID, and is your and client_secret = your facebook application secret.
– use scope argument for extended permissions (i.e. offline_access, publish_stream), you need those in order to post messages on a user’s wall (http://developers.facebook.com/docs/authentication/#requesting-extended-permissions)
3) You will be redirected to the specified redirect_url. Copy verification string in the ‘code’ argument from the redirect URL, and paste it into this URL to get (permanent) Access Token:
https://graph.facebook.com/oauth/access_token?client_id=FACEBOOK_APP_ID&redirect_uri=http://www.URL/&client_secret=VERIFICATION_STRING
4) Browser returns Access Token. Use Access Token and Graph API functions to post messages to user’s wall
…for further infos and examples see http://developers.facebook.com/docs/reference/api/post, e.g.
curl -F ‘access_token=ACCESS_TOKEN’ \
-F ‘message=Hello World. I like this new API.’ \
-F ‘link=http://www.example.com/article.html’ \
-F ‘picture=http://www.example.com/article-thumbnail.jpg’ \
-F ‘name=Article Title’ \
-F ‘caption=Caption for the link’ \
-F ‘description=Longer description of the link’ \
-F ‘actions={”name”: “View on Zombo”, “link”: “http://www.zombo.com”}’ \
-F ‘privacy={”value”: “ALL_FRIENDS”}’ \
-F ‘targeting= {”countries”:”US”,”regions”:”6,53″,”locales”:”6″}’ \
https://graph.facebook.com/FACEBOOK_USER_ID/feed
Hi,
This seems to be a good starting point. I was able to create an app and get my wordpress blog into facebook, but wanted to add functionality like updates to users on new post etc.
I am yet to try the steps provided, but from the detailed instructions I think it should not be such a difficult task.
Thank you for the wonderful tutorial.
Thanks a lot Steven, for your good tutorial for us.
I have a Quiz Application in the facebook Platform, I want to publish my quiz result in the news feed from a quiz application.The application is developed in IFRAME base.
Please give me code or instruction to me.
Please help me.
@Mbu The above same code works, while giving permission you just need to give for news feed too.
Thank you, thank you, thank you! 🙂 I have been searching the web for 3 days for this and nothing worked! Excellent tutorial!
Hello steven,
First of all thanks a lot for a great article. I have one question If i want to post on my friends wall who is using this application. What modification i need to do in your script. So that i can post on my friends wall without login.
Thanks
Great post. Thank you for your contribution. Now we can finally post to our wall using php.
Dear
I have a Quiz Application in facebook platform and when I click the submit
button after correct the question then form data would not submit in the result
page using the Internet Explorer but in Firefox this is correct.
Also if I try secondly after first time in IE the result of a form data would
submit and result is come.
I was spent my huge time to correct this but i can’t do it.
What is the problem and which will be suggestion to me to solve this problem.
Please suggest me…
With regards
Nice post. This is how to post a message in your facebook wall, do you know how to post a link and change its description to facebook?
Hi Steven,
I have follow your guide, all sucess until step 5. I got only an echo “post on wall” print on screen and then blank page, and there was no post public on my facebook wall.
I see that you answer all the previous post, hopefully mine too. Thanks in advance.
Vicky
@Vicky Check the permissions again, or see if the page is authorized to publish the post.
@Madmax see my other post who to post images u will get it. Add Picture on facebook
I sent a message to the user’s wall, the operation was successful, but the message was in spam on the wall. Why?
Hi Steven, firstly thanks a lot for your tutorial. I have a question though: I followed your steps and was able to post to my facebook wall from my website. However even when someone else accesses my website it posts to my wall only. Not their wall. Am I missing something? Do we need to perform all the 5 steps with each user? Please guide me. I am very new to facebook API and I am probably doing something very foolish. Appreciate your help.
I am done with all the steps successfully…wow..great explanation..I am PHP developer but new to Facebook App….
I am done with Steps 1,2 and 4 ..yeah I got success page .. But I am unable to generate one time session key(Step 3) …I GET BLANK WHITE PAGE..no matter how many times I am trying(From past 1 day)..
What is the issue..please suggest….Thanks in advance
Hello All
How can we transform the update text to a update link ?
How use $action_links and $attachement with this code ?!
My code:
$page_id =”xxxxxxxxxxxxxxxxxxx”;
if( $facebook->api_client->stream_publish($message, $attachment,$action_links, null,$page_id))
Thanx a lot :’))
@Tokio Hotel see this post http://blog.theunical.com/facebook-integration/add-picture-on-facebook-wall-using-php/
Hi Steven, today I have an error on “Step2: Generate One Time Token to generate Session key”
I changed the “API_KEY” to mine from the link:
https://login.facebook.com/code_gen.php?api_key=API_KEY&v=1.0
But it gave me an error message of:
“An error occurred. Please try again later.” with “Okay” button underneath.
I clicked on that “Okay” button, but it gave me the same message.
What does it mean? And how to resolve this? Thank you.
PS:
Well, I got it work fine yesterday, until facebook thought there’s something ‘funny’ going on and got my facebook temporary ‘suspended’ (maybe because I kept on trying to post quite frequently using php? :p).
Then facebook asked to reset my password.
And it went back to normal again.
BUT, my apps GONE 🙁
Couldn’t be bothered to try it again last night, so I tried your 5 steps again today.
And everything works but not until I got to step 2.
Help please?
Thank you, Steven.
@lizzy, I seen that many FB users have similar problem, FB doesn’t work same for all users all the time, it will give some errors sometimes, keep try after some time.
I am getting the following error in Step 3 (Generating the Permanent Session Key).
Fatal error: Class ‘FacebookRestClient’ not found in /home1/mermagic/public_html/test/session-key.php on line 7
Can someone please tell me where the Facebook php sdk should be located in order for the code to use it?
Hi Steven, i want to ask something, what is “{$user}” in step 5 line 23?
I still can not post something to my wall.. when i execute step 5 its just show “post on wall” on the browser.. no error message or “Added on FB Wall”
@ Joe Davila: FacebookRestClient class is on \facebook-platform\php\facebookapi_php5_restlib.php
Hello
When i go for step two i am getting error : An error occurred. Please try again later.
I am not able to move forward.
I have verified the API key which is correct then what would be the possible reason.
Please reply.
Thanks
Mukta
@Mukta It seems facebook has changed some settings, After you created your application try to given permissions step4 then try other steps.
Hello Stevan
Thanks for your reply.
As per your replay I given permissions to my application as mentioned in step4 but facing the same error.
Now what would be the possible reason?
You wrote :
# 24th Feb 2011 11:38 pm
@Mukta It seems facebook has changed some settings, After you created your application try to given permissions step4 then try other steps.
Thanks
Mukta
Hello Stevan
Suddenly step two error has stopeed coming and I have moved to step4.
But in step 4 I am getting following url and I am not able to complete all the steps.
http://www.facebook.com/developers/login_error.php?app_id=165631503489774&connect=1&type=connect
Please try to give me solution ASAP.
Thanks
Mukta
@Mukta For Step4 u need to use this URL to give permission
http://www.facebook.com/login.php?api_key=APIKEYXxxxxxxxxxxxxxxxxx&connect_display=popup&v=1.0&next=http://www.facebook.com/connect/login_success.html&cancel_url=http://www.facebook.com/connect/login_failure.html&fbconnect=true&return_session=true&req_perms=read_stream,publish_stream,offline_access
replace APIKEY with your key
thanks i was looking for this code for posting a message on user’s wall , thanks again.
need help here. I can’t find CONNECT tab.
@fern, FB has changed its menu bars, soon I will update this post, mean while check for remaining menus
Hello Steven,
In step 4 if i m getting following url and I am not able to complete all the steps.
http://www.facebook.com/developers/login_error.php?app_id=138228446242989&connect=1&type=connect
@Steven Robert: please update this as soon as possible. tnx!
I’ll wait for the update.
hi, I would like to automaticly post on tmy application user’s wall when they use the application with a ,sentence, image back link, and connect link or something.
i have the permission set
can you help me please? and also how can i sometimes post on all user’s wall once in a while?
thank you
Hello All, I just updated the post will all latest screen shots and updated post content.
Every time when i post n this blog, the comment will not approve immediately, it takes time, but after taking time my comment was not appearing on the topics why is it so, is there any reason please mention,..
Hi,
I am done with posting my data on wall.I was facing problem with step 4 for permission. I have used the following url:
http://facebook.com/authorize.php?api_key=MYAPPAPIKEY&v=1.0&ext_perm=publish_stream&next=http://blinkbee.com/profile
@Steven:Thanks a lot ur tutorial helped me a lot.
@khadija Did u replaced the MYAPPAPIKEy with your facebook app id?
@Steven:No, I replaced my app key with MYAPPAPIKEy.
@khadija use the same privilages http://www.facebook.com/login.php?api_key=APIKEYXxxxxxxxxxxxxxxxxx&connect_display=popup&v=1.0&next=http://www.facebook.com/connect/login_success.html&cancel_url=http://www.facebook.com/connect/login_failure.html&fbconnect=true&return_session=true&req_perms=read_stream,publish_stream,offline_access
offline access/ read stream /publish
Hi Steven,
If i m using that url i m getting following error:
http://www.facebook.com/developers/login_error.php?app_id=138228446242989&connect=1&type=connect
Thanks,
khadija
Hello,
I have been trying to follow the steps in your description, but when I try to create an app I get the page “Your account must be verified before you can take this action. Please verify your account by adding your ….”
When I put the code specified on a page (internal), the link takes me to a credit card info page. Does this code need to go on a public page? Why does it take me to a payments page? To create an app is free, right?
Thanks
@Michael ,it is free, before that verify your phone number by entering the code that u will get from SMS.
Hi,
When we use https://login.facebook.com/code_gen.php?api_key=API_key&v=1.0
whith own api key . it gives error “An error occurred. Please try again later.”
I’m just new in your topic. But exactly, I always had a bad time I posting in the wall.
Your post could be my key to ease my problem. Thanks for sharing about it!
thank you for such a great, detailed tutorial — excellent!
Some of the Facebook code has changed since you posted this, and I was getting an assortment of error messages from functions that didn’t exist in the libraries I had. I finally got a clean version of matching libs at:
http://pearhub.org/get/facebook-0.1.0.tgz
Now everything works as you described… except I have one problem…
I need to post to an “organization” page but the comments are going to the regular user page (the administrator of the organization).
Is there any way to post to the wall of an organization/company/etc instead of posting to the wall of the user who is that org’s admin?
anybody?
Thanks!
@Pankaj, Many times FB gives same error try after sometime u will get it.