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:
    https://login.facebook.com/code_gen.php?api_key=YOUR_API_KEY&v=1.0
  • Generate session key:
    <?php
    
    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>";
    
    print_r($result);
    
    echo $session_key = $result['session_key'];
    
    ?>
  • Give permission (for application) to publish on your wall
    http://www.facebook.com/login.php?api_key=YOUR_API_KEY&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
  • Publish message. You can get information on what more to add on facebook developers page about stream.publish
    <?php
    
    define('FB_APIKEY', '<Your Api Key>');
    define('FB_SECRET', '<Secret>');
    define('FB_SESSION', '<Session>');
    
    require_once('facebook.php');
    
    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:

    http://www.facebook.com/connect/prompt_permissions.php?api_key=YOURAPIKEY&v=1.0&next=http://www.facebook.com/connect/login_success.html?xxRESULTTOKENxx&display=popup&ext_perm=read_stream,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: http://www.facebook.com/BeenAndSeen
    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,

    alex

Leave a Reply