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"]);
}
?>
You may achieve this by requiring
publish_streampermission (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_tokenand using applicationaccess_tokenfor that purpose.It’s really easy to implement this using PHP-SDK:
Probably
publish_actionspermission can be sufficient for that task, but documentation isn’t yet updated across developers site so it’s safer to usepublish_stream(seestatusesforuserobject).