5 Steps to publish on a facebook wall using php

November 1, 2009
Filed under: FaceBook Integration, Featured, 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.

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

application page

Now goto Canvas link and enter any url.

canvaspage

Next goto Connect URL link and enter the same url.

connecturl

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://svn.facebook.com/svnroot/platform/clients/packages/facebook-platform.tar.gz

Step2: Generate One Time 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

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'];

?>

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/

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

Similar Posts

Comments

65 Comments on 5 Steps to publish on a facebook wall using php

  1. Mustafa on Sun, 1st Nov 2009 7:16 am
  2. Excellent:

    Here is another way to Publish on Facebook Wall Using JSON using Coldfusion:

    http://blog.abusalah.info/2009/05/publish-on-facebook-wall-using-json-fb.html

  3. Ramachandran on Wed, 11th Nov 2009 7:20 am
  4. Nice script, thanks for your script…..

    when i use this script .I am getting the error as 104(incorrect signature).
    And we can able to run the script suceessfully upto step 4.But i am struck in the STEP 5 because of this error .
    Do you aware of this error?
    How to fix this.
    Any input is appreciated.

  5. Steven Robert on Wed, 11th Nov 2009 11:06 pm
  6. After step4 did you see the privilege screen from facebook.
    we need to give permissions to publish on facebook. Let me know if you have any questions.

  7. Hardik on Wed, 2nd Dec 2009 6:32 am
  8. hello,
    i have followed the instruction word by word and i have also succed.But I have one doubt thats is where should I use that two script.
    I am developing an iphone application which connect to facebook.thats i will login on facebook through my iphone and after login i want to publish on my wall as well as user walls directly.I hope you understand what my doubt is.

  9. Steven Robert on Wed, 2nd Dec 2009 1:13 pm
  10. Yes u can do that. If you have the api key and secret key. You need to save this keys in ur application.

  11. chad james on Sat, 26th Dec 2009 6:06 am
  12. Hey I created a facebook application for my blog called QIT Interact, The problem I’m running into is I’m new to all this etc and I’m trying to work out if there is way that I can have my application published to feeds say when a new post has been posted etc rather then looking up the application etc. Ideally what I’m wanting to do is rig it so it will always send a message to streams any help would be greatly appreciated

  13. Steven Robert on Sat, 26th Dec 2009 10:53 pm
  14. Yes, We can. what kind of application you are building

  15. vishal on Thu, 31st Dec 2009 9:05 am
  16. I am trying to publish my offline data on facebook but its not giving me any response.. below is my script .. please give me solutions .
    api_client->session_key = $_REQUEST['fb_sig_session_key'];
    $facebook->require_frame();
    $user = $facebook->require_login();
    $userinfo = get_prints($user);
    $name = $userinfo[0]['title'];
    $description = $userinfo[0]['description'];

    $attachment = array(’name’ => $name, ‘href’ => ‘http://apps.facebook.com/vishpoint/’, ‘description’ => $description, ‘media’ => array(array(’type’ => ‘image’, ’src’ => ‘http://www.jaisiyaram.com/adv/banners/23-1-1260415655.jpg’, ‘href’ => ‘http://apps.facebook.com/vishpoint/’)));

    $action_links = array(array(’text’ => $name, ‘href’ => ‘http://apps.facebook.com/vishpoint/’));
    $attachment = json_encode($attachment);
    $action_links = json_encode($action_links);
    $has_permission = $facebook->api_client->users_hasAppPermission(”publish_stream”);
    //echo $has_permission;
    $ans=$facebook->api_client->stream_publish(”Testing.. “,$attachment,$action_links);
    if($ans){
    echo “Added on FB Wall”;
    }else{
    echo “Not Added on FB Wall”;
    }
    }catch(Exception $e) {
    echo $e . “”;
    }
    ?>

  17. Steven Robert on Thu, 31st Dec 2009 11:02 pm
  18. You cannot connect to facebook offline.

  19. vishal on Fri, 1st Jan 2010 3:42 am
  20. Hi Steven, Actually i want to post my data from my pc on the facebook wall.. like image, title and description by above script…Is it possible? how?

  21. Steven Robert on Fri, 1st Jan 2010 9:45 pm
  22. Yes but the image should be in a public path like it should be accessible from any where.

  23. vishal on Mon, 4th Jan 2010 5:15 am
  24. HI steven,
    I have admin section on my website. I am updating news content on website daily. I want to publish same content on my facebook account wall. What I want is when I submit content in my websites’s admin area, it should also update facebook wall with same contents.

    I have set up facebook application and grab the API Key and other required information. I am using iframe and not fbml.

    I am using stream_publish() function.

    Problem is here when I submit data using stream_publish, it redirects me to login page.

    Is there any method through which I can login to facebook and publish content to my facebook wall.

    Below is the code.
    require_frame();
    $user = $facebook->require_login();
    $attachment = array(’name’ => $name, ‘href’ => $link, ‘description’ => $description, ‘media’ => array(array(’type’ => ‘image’, ’src’ => $imagelink, ‘href’ => $link)));
    $action_links = array(array(’text’ => $name, ‘href’ => $link));
    $attachment = json_encode($attachment);
    $action_links = json_encode($action_links);
    $facebook->api_client->stream_publish($description,$attachment,$action_links);
    ?>

    Please help.

    Thanks in advance.

  25. Steven Robert on Mon, 4th Jan 2010 9:47 pm
  26. Hello Vishal,
    You need to complete Step 2, 3, 4 to create a one time session key and give permission to publish on wall. so that next time it will not ask you to login to facebook.
    Let me know if you done these correctly.

  27. Vishal on Mon, 11th Jan 2010 6:41 am
  28. Hello

    I tried to perform all steps mentioned below.
    1) I have created application as mentioned.
    2) I have retrived one time session key.
    3) I have granted permission as well.
    4) In application setting, I have canvas call back url, this would call the script in step 5.

    Now what I do is I logged out from facebook. Then I typed url in address bar as http://apps.facebook.com/theunicalblogtest/

    This shows me “Sign up for Facebook to use Blog TheUnical.”

    What I am doning wrong here? can you please let me know here?

  29. Steven Robert on Mon, 11th Jan 2010 10:53 pm
  30. Hi Vishal,
    Why you want to type this URL “http://apps.facebook.com/theunicalblogtest/”. you can just execute your program.

  31. Tomee on Fri, 15th Jan 2010 3:37 am
  32. Hi there,
    the code from the step 3 where should be copied to run, ‘couse saving to a file and just run it does not gives you a valid response.

  33. Steven Robert on Fri, 15th Jan 2010 10:56 pm
  34. Just Copy the php code and replace with your keys and execute to generate a one time session key. Tomee, you need to execute this only once, if you try to execute this 2nd time you will get some errors.

  35. Steven Robert on Fri, 15th Jan 2010 10:58 pm
  36. Tomee, If you get errors then try again from step2.

  37. Tomee on Sat, 16th Jan 2010 9:00 am
  38. i obtained the one time code Steven, and the API code and secret also. When I run the file it gives me the “FacebookRestClient” undefined function error, which is kinda normal I think ‘couse it is nowhere defined in the php file.

  39. Steven Robert on Mon, 18th Jan 2010 12:44 am
  40. Hi Tomee here is the same code that is working for my id , replace with your’s and try. http://blog.theunical.com/?attachment_id=267142.
    let me know if you still have any issues.

  41. shyam on Tue, 19th Jan 2010 9:20 am
  42. I follow the instruction but i have a problem when i am trying to generate session since there is class page in not included in STEP 3 so firstly is say class not found and even after adding the related class now is saying

    Class ‘Facebook’ not found in /var/vhosts/apartmentsneakpeeks/httpdocs/facebook-platform/php/facebookapi_php5_restlib.php on line 82

    Rest was fine but in the last due to session value is “” i could not make the post done.

    Will you please mail me regarding this.
    Thanks and Regards,
    SHyam

  43. Steven Robert on Wed, 20th Jan 2010 4:32 am
  44. Shyam did you tried downloading the code from here? http://blog.theunical.com/?attachment_id=267142

  45. Steven Robert on Wed, 20th Jan 2010 4:33 am
  46. Shyam did you tried downloading the code from here?
    http://blog.theunical.com/?attachment_id=267142

  47. Maria on Thu, 21st Jan 2010 7:37 pm
  48. Steven, I cannot execute step 3. Is the php code supposed to be executed on a server? I have plugged in my values, but I get php parsing errors.

  49. Maria on Thu, 21st Jan 2010 7:40 pm
  50. Plus, line 11 in step 3 should be:

    $fb = new FacebookRestClient($FB_APIKEY, $FB_SECRET);

    FB_APIKEY and FB_SECRET are php variables. And should the code not include the facebook php library? I am very confused.

  51. Maria on Thu, 21st Jan 2010 7:54 pm
  52. And finally, this link does not show any code :(

    http://blog.theunical.com/?attachment_id=267142

  53. Steven Robert on Thu, 21st Jan 2010 10:50 pm
  54. There is a link that you can download from, check again.
    This is that link http://blog.theunical.com/wp-content/uploads/2010/01/fb_app.zip

  55. Steven Robert on Thu, 21st Jan 2010 10:51 pm
  56. Maria, This should be replaced by facebook secret key and facebook app key

  57. Steven Robert on Thu, 21st Jan 2010 10:53 pm
  58. Yes Maria, the code should be executed on server.

  59. Tomee on Fri, 22nd Jan 2010 11:16 am
  60. Thank you steven I managed to publish on my wall finally :) can you help in making publish possible from my app ?

  61. Steven Robert on Fri, 22nd Jan 2010 10:52 pm
  62. Hi Tomee, You can just create a Iframe of your existing application and try it in Facebook App.

  63. Tomee on Sat, 23rd Jan 2010 6:49 am
  64. Steven what I would like to do is, anyone who uses my facebook app and clicks on a link to be able to publish on his own wall a message like “come and play this” or that game :) I was looking on the facebook dev forums but it’s a mess majority of the examples are using deprecated functions, and no working example can be found :( disappointing

  65. Steven Robert on Sat, 23rd Jan 2010 10:56 pm
  66. Tomee, To Publish on Users you need to first generate a one time session key and later they need to give authentication, so that you can publish on their wall.

  67. Fabio on Mon, 25th Jan 2010 11:08 pm
  68. Hi,
    i’m enchanted … wow , your article “5 Steps to publish on a facebook wall using php” is the unique resurce that explane how to publish on Facebook wall without use form login.
    I googled a lot and fortunately i found your blog.
    But i’m also curious,so i would ask you :
    1. How did you find this solution? and all the links used to open a session? On developer Facebook section i could not find nothing about. Are maybe secret links ? ;-)
    2. And, is possible to publish on a Fan-page wall instead of User wall ? If yes, how can i customize it?
    Thanks a lot.
    Bye from Italy

    Fabio

  69. Steven Robert on Mon, 25th Jan 2010 11:10 pm
  70. Hi Fabio,
    Even I too have to do some research like you when I got requirement to publish on a wall and I tried a lot at last I was successful to publish on wall. So I thought, since there are no post that truly describes how to publish on a wall. So I have to write this post. The solution is in facebook but not on a single page you need to read some 20- 40 pages and try lot of examples then you will get.
    Yes, you can publish on Fan-page too, but you need to get permissions of that page too.
    - Steven

  71. Fabio on Tue, 26th Jan 2010 7:32 am
  72. Good…i have permission…i’m administrator of the Fan Page.
    Can you give me some php code suggestion?

  73. Steven Robert on Tue, 26th Jan 2010 10:11 am
  74. Hi Fabio,
    Here is the example PHP code and the screenshots in my next post to publish on a Facebook fan’s page.
    http://blog.theunical.com/facebook-integration/simple-steps-to-publish-on-facebook-fan-page-using-php/
    If you still have any difficulties we can develop any facebook applications for you.
    -Steven

  75. shyam on Thu, 28th Jan 2010 5:52 am
  76. Hello Steven Robert,

    Thanks for reply, thanks for write such page to helps other.
    Perhaps i was wrong some where now i can post without log in.
    Will you please tell me how to add image /url along with the post and what are the purpose of this line ?
    “‘query’ => “select uid2 from friend where uid1={$user}”));”
    is this related to our table or FB’s table.
    Thank you in advance and waiting for your reply.
    I was late to write this because i could not test the application after your message.
    Today i did .

    Regards,
    SHyam

  77. Steven Robert on Thu, 28th Jan 2010 9:59 pm
  78. Hi Shyam, That is a facebook table.
    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/

  79. shyam on Fri, 29th Jan 2010 1:15 am
  80. Hello Steven ,

    I am very pleased to see your reply and next blog for video and photo.
    Thanks for all of them.

    Steven i have one more query—-
    can i sync the wall post to my twitter account ? Mean can i directly link these posting as a twitter tweet.
    I will be very thankful for your reply.
    Regards,
    SHyam

  81. faemino on Fri, 29th Jan 2010 8:31 am
  82. Just thanks for all your articles about Facebok conection from PHP. Great resource and nice job. :-)

  83. Maria on Fri, 29th Jan 2010 3:24 pm
  84. Steven, I tried all of your code for step 3, and I cannot get it to work …. grrrrrr!!! I download the php library from facebook to my server. I copy the generatesession.key.php to the same directory on my server and name it index.php. Then I try to call my application from Facebook, but I get error message. What am I doing wrong?

  85. Steven Robert on Fri, 29th Jan 2010 9:58 pm
  86. Hi Maria,
    Did you created the facebook application, as I described in Step1? Iam able to publish with a new account in 10 mins. May be you are doing something incorrect.
    Also please make sure that you enter any working URL in Canvas call back URL and Connect URL. if this is missing then the application will not work.

  87. Rangga on Sat, 30th Jan 2010 7:24 am
  88. I have 2 questions as below:

    1. What is the real purpose of fetching friend list on your code, since I didn’t see any relation with it main topics; to put something on our wall.

    2. Is there something that need to change from your code if I want to implement it on my mobile web ?

    Thx 4 sharing..

  89. Jim on Sat, 30th Jan 2010 3:48 pm
  90. Thank you! After many days of search, google’ing and testing this page was the key to posting messages to an offline user

  91. Steven Robert on Sat, 30th Jan 2010 11:16 pm
  92. Rangga, this code work’s perfect on mobile web too.

  93. fede on Mon, 1st Feb 2010 2:58 pm
  94. genius you save my week work, the documentation of facebook is a bullshit and you explain us so clear and concise.
    Thank you very much.
    Fede from rosario,argentina.
    Success.!!!!!!!!!!!!!!11

  95. Sebastien on Wed, 3rd Feb 2010 10:09 am
  96. Thank you so much for this code. I have been reading the facebook wiki for days trying to achieve this and they’ve managed to confuse me beyond belief. I was able to connect thanks to you!

    I do have one question though, is the one-time code a link between the application and the facebook account? If I give my users the ability to post on their walls from my website can I use the token (assuming I save it somewhere and get their permission) indefinitely? I’m trying to figure out if the session key will expire.

    Thanks again!

  97. Steven Robert on Wed, 3rd Feb 2010 9:57 pm
  98. Sebastien , do you want to post users on your wall. If so, you can create a simple app and give to your users. If the users has to post on their own wall then you can simply use any bookmarking tool like Shareit, Addit etc.

  99. Aziz on Thu, 4th Feb 2010 9:33 am
  100. I am stuck on step 3. I am assignning the apikey, secret and token, but when i run it i am not receiving the session key.

  101. Kapil Jadhav on Fri, 5th Feb 2010 9:10 am
  102. Hi Thank you for the code and description. The steps and code work like charm. I am successful implementing this but just wanted to know if there is a way to ask user ib a popup if they want to publish or skip.

    Thank you a tons,
    Kapil

  103. Aziz on Mon, 8th Feb 2010 6:07 am
  104. Ohh i am so stupid ))) Thx it worked. this method is great!!

  105. roded on Mon, 8th Feb 2010 11:53 am
  106. thanks for this article
    you save my day…man, facebook framework is quite pain in the neck…hehehe

  107. Keval on Wed, 17th Feb 2010 8:11 am
  108. hi Steven

    I have write message on my application wall

    Your coding is used to message display on user wall

    have u help me …??

    thanks in advance

  109. TheTester on Fri, 19th Feb 2010 7:16 am
  110. Great tutorial but I have one question. What if want to make not temporary session key but something to call stream.publish in background(for example as a cron job). In such scenario I can’t ask user for session key so I need some kind of persistent session key. How can I obtain it?

  111. Steven Robert on Fri, 26th Feb 2010 12:22 am
  112. hello TheTester, Do u want to publish on user own wall, if yes then there in no need of any session key, you can just any bookmarking tool for this, like I used one in this blog.

  113. Sylvester Faro on Thu, 4th Mar 2010 6:48 am
  114. This is GREAT… after wasting hours on <> Facebook API docs, I got this working in less than 5 minutes.

    Like “TheTester”, I will be using this on a Cron Job to publish on my own wall… I have same concerns for “how long will the session key be valid?”… But I guess I will have to monitor “invalid session key” publishing errors and then generate a new session key…

  115. Steven Robert on Thu, 4th Mar 2010 9:52 pm
  116. Hi Faro, The session key will be valid until something unusual happens, Like if you hit facebook continuously , like in 1min you tried to hit some 50 times then that key will be invalid then you need to generate again a new key. If not then it will be valid for a longer time.

  117. abrahan on Mon, 8th Mar 2010 8:33 pm
  118. Hey!, when i try 2 and 3 steps, i get this error:

    Canvas Callback URL must point to a directory (i.e., end with a “/”) or a dynamic page (i.e., have a “?” somewhere).
    Connect URL must point to a directory (i.e., end with a “/”) or a dynamic page (i.e., have a “?” somewhere).

    do you know why appears the error? and… can you help me to solve it?

  119. Steven Robert on Mon, 8th Mar 2010 10:45 pm
  120. Hi Abrahan, This Canvas URL and connect URL should be a publicly accessible URL.

  121. Dinesh on Mon, 15th Mar 2010 4:02 pm
  122. Hi Steven,
    I have follow all five steps
    I have done with Publish to wall functionality.
    Post has also display on users wall
    But I am missing result means attachment object text, href,image and media on publish poppup….

    I am used Asp.net MVC Framwork 2.0
    Net Facebook API Client.
    and in my old application Publish to wall functionality not working bcz of Facebook stop supporting to fb.connect.confirmDialog…method.

    Please help me

  123. navjot singh on Tue, 16th Mar 2010 6:42 am
  124. its a very nice post, but i am unable to get 100% out of it. Can you let me know where the code generated by user as permanent code for my site need to be used.
    each time after the permission if try to publish i get the below error.
    ‘Session key invalid or no longer valid’
    please guide me where i am doing the wrong implementation
    any kind of fast help will be very thankful

    regards
    navjot

  125. Steven Robert on Tue, 16th Mar 2010 9:01 am
  126. Hi Navjot, After you execute Step3: Generate one time session key , on your browser you can see a session key printed. This is what the Permanent session key. This key will be in two parts separated by a hi-fen. Let me know if you still have any issues.

  127. Dinesh on Tue, 16th Mar 2010 9:36 am
  128. Hi Steven,
    Pls help me

  129. Steven Robert on Tue, 16th Mar 2010 10:04 am
  130. Dinesh The URL need to be Public Available URL and not a local URL, Check it once. Are you trying with a public URL?

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!