Maris Svirksts

Sandbox for those few moments without coding
Sandbox for those few moments without coding

Publishing to Facebook page wall from your site (stream.publish)

Here is one way to get your site post information posted on facebook wall.

  • Add your application to Facebook: developers - button to "Set Up New Application", and fill out all required fields - just try to save it, then add those fields it wants you to add. Note that you can change that information later. Remember you application ID, key and Secret.
  • Download php library from facebook: facebook-platform, upload it somewhere on your server.
  • Generate¬† temporary (one time - if you waste it, you need to repeat this step again) token:
  • Generate session key:
    require_once '<path to facebook library, you uploaded>/facebook.php';
    $FB_APIKEY="<Your Api Key>";
    $FB_SECRET="<Your Api Secret>";
    $fb = new FacebookRestClient($FB_APIKEY, $FB_SECRET);
    $testtoken= "<Your Temporary Token>";
    $result = $fb->call_method('facebook.auth.getSession',
    array('auth_token' => $testtoken, 'generate_session_secret' => true));
    echo "<br /><pre>";
    echo $session_key = $result['session_key'];
  • Give permission (for application) to publish on your wall,publish_stream,offline_access
  • Publish message. You can get information on what more to add on facebook developers page about stream.publish
    define('FB_APIKEY', '<Your Api Key>');
    define('FB_SECRET', '<Secret>');
    define('FB_SESSION', '<Session>');
    echo "post on wall";
    echo "<br/>";
    try {
     $facebook = new Facebook(FB_APIKEY, FB_SECRET);
     $facebook->api_client->session_key = FB_SESSION;
     $facebook->api_client->expires = 0;
     $message = '';
     $attachment = array(
     'name' => $_POST["name"],
     'href' => $_POST["href"],
     'description' => $_POST["description"],
     'media' => array(array('type' => 'image',
     'src' => $_POST["src"],
     'href' => $_POST["href"])));
     $action_links = array( array('text' => 'Visit Us', 'href' => '<link to some place here>'));
     $attachment = json_encode($attachment);
     $action_links = json_encode($action_links);
     $target_id = "<Target Id>";
     $session_key = FB_SESSION;
     if( $facebook->api_client->stream_publish($message, $attachment, $action_links, null, $target_id)) {
     echo "Added on FB Wall";
    } catch(Exception $e) {
     echo $e . "<br />";

In order to show page as the author of post you need to do two more things:,publish_stream&enable_profile_selector=1
Be Sociable, Share!

2 Responses to “Publishing to Facebook page wall from your site (stream.publish)”

  • Maris Svirksts says:

    Hi Alex,

    Sorry for delay, Akismet thought your post was spam :(

    First of all, yes, "Publish to streams" permission is enough - just rechecked it on the Facebook page I added that functionality for:
    Question: Can you, please, recheck $target_id string from the last code? Maybe it's wrong.

  • alex says:

    hello mr Svirksts,

    first of all thank you for sharing this tutorial with us. im sure it will help a lot of people. i already have a running "post to wall" application and i would like it to post on my facebook page. when i try the last step to get the necessary permissions for my app i get the following message "Allow to publish posts or comments without prompting me." i selected my facebook page and confirmed. now i tried to post on that wall from my app but it doenst work. i dont get an "no permission" message but the post doenst appear ob the wall. when i edit the application settings of my page there is only the option "Publish to streams", but when i look at the same settings for my account there are also other options like "offline_access" etc. is the "Publish to streams" permission enough to post on that facebook? if so then do you have an idea what else i could be doing wrong? thanks a lot in advance,


Leave a Reply