Questioner
Questioner

Reputation: 7463

How do I change this example Facebook code to post feed data automatically?

I took this code from Facebook's documentation to get me started in learning how to get my app to post to a feed.

It works as described, in that when I go to my app's canvas URL, I am presented with a dialog where I can enter in text, click "share", and it posts into my timeline. So far, so good.

But I want to alter it so that instead of explicitly typing something in and clicking a button, a post is automatically sent to the feed, triggered be events in my PHP code.

However, and I realize this is a newbie kind of question, I can't figure out how to adjust the code to make that happen. My experiments either just break the code or end up with the same dialog.

How do I get the PHP to post a message straight into the feed, and then display the app's canvas URL immediately after (so as not to get caught in a loop of constantly reloading and posting over and over...)?

For convenience, here is the same code from the Facebook documentation:

<?php 

         $app_id = "YOUR_APP_ID";

         $canvas_page = "YOUR_CANVAS_PAGE_URL";

         $message = "Apps on Facebook.com are cool!";

         $feed_url = "https://www.facebook.com/dialog/feed?app_id=" 
                . $app_id . "&redirect_uri=" . urlencode($canvas_page)
                . "&message=" . $message;

         if (empty($_REQUEST["post_id"])) {
            echo("<script> top.location.href='" . $feed_url . "'</script>");
         } else {
            echo ("Feed Post Id: " . $_REQUEST["post_id"]);
         }
?>

Upvotes: 0

Views: 817

Answers (1)

Juicy Scripter
Juicy Scripter

Reputation: 25918

You may achieve this by requiring publish_stream permission (see Publishing Permissions).

Once your app have that permission granted by user you may publish feed stories without showing Feed Dialog to user.

This also allow you to publish content to user's feed without need of user's active access_token and using application access_token for that purpose.

It's really easy to implement this using PHP-SDK:

$facebook = new Facebook(/*...*/);

$facebook->api('/USER_ID/feed', 'post', array(
  'message'=>'Text entered by user'
));

Probably publish_actions permission can be sufficient for that task, but documentation isn't yet updated across developers site so it's safer to use publish_stream (see statuses for user object).

The publish_stream permission is a superset of publish_actions allowing everything that publish_actions allows plus more. Some of the additional publishing capabilities are:

  • posting to a friend's feed
  • posting questions
  • creating notes
  • posting content to events or groups

Upvotes: 1

Related Questions