Facebook Application About Page

5 Steps to publish on a facebook wall using php

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.

Create New Facebook application

Provide the application name, click agree for facebook terms and click on “Create Application” button.

Fill in application Name

create app step1

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.

Facebook Application About Page

Now Click on Website Link to get API Key

Facebook Application web Site

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.

Facebook Application Facebook Integration

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.

allowaccess

generatemylogin info

savemyinfo

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:

http://www.facebook.com/login.php?api_key=YOURAPIKEY&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&session_key_only=true&req_perms=read_stream,publish_stream,offline_access

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:

&session={“session_key”:”SESSIONKEY”, “uid”:USERID, “expires”:0 || UNIXTIME, “secret”:”SESSIONSECRET”}

In continuing with the above example, the redirect for a successful login would be:

http://www.facebook.com/connect/login_success.html?session=%7B%22session_key%22%3A%223.kxhAu6W0qo_bLGjmdWrgfw__.86400.1243443600-688626964%22%2C%22uid%22%3A%22688626964%22%2C%22expires%22%3A1243443600%2C%22secret%22%3A%220NVNMxpO6jVyDcVCvVv_PA__%22%2C%22sig%22%3A%22ac1c0c77c137567389defea70481b7aa%22%7D

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.

allowpermission

allowpublishing

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.

published on facebook

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

Steven Fan Page on Facebook

Please don’t copy this content to any site. This is fully protected by TheUnical Technologies

Post Laster Updated on 13 March 2011

308 thoughts on “5 Steps to publish on a facebook wall using php”

  1. 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…

  2. 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

  3. 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.

  4. 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.

  5. Thank you, thank you, thank you! 🙂 I have been searching the web for 3 days for this and nothing worked! Excellent tutorial!

  6. 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

  7. 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

  8. 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?

  9. 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

  10. I sent a message to the user’s wall, the operation was successful, but the message was in spam on the wall. Why?

  11. 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.

  12. 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

  13. 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 :’))

  14. 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.

  15. @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.

  16. 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?

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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,..

  22. 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

  23. 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!

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image