开发者

Posting a status to Facebook API, Help :(

In short, I want to use FB api for posting the status, in this scenario :

the user have to type some update (in my website) which will post开发者_JAVA百科ed to db, the user may check (post to fb) so that the update also appears in his FB profile.

for posting the update in my website I have a textarea in an HTML form which points the action to another php file (to write it in the DB)

Finally I wil I'll show you both files to be safe and give accurate answers

How to integrate both actions (posting to both FB and DB) , what should I do and where?

Thx


The following code should help;

$facebook = new Facebook(array(
 'appId'  => FACEBOOK_APP_ID,
 'secret' => FACEBOOK_SECRET,
 'cookie' => false,
));

Enter Application settings

$facebook_url = $facebook->getLoginUrl(array(
 'next' => '',
 'cancel_url' => '',
 'req_perms' => 'email,publish_stream,status_update'
));

Use this url to authenticate the user, as it will grant permission to post status updates

$attachment =  array(
 'message' => '[Status message]',
 'name' => '[Post title]',
 'link' => '[Post image & title link]',
 'description' => '[Post description]',
 'picture'=> '[Post Thumbnail Location]'
);

$facebook->api('/me/feed', 'POST', $attachment);

This will post the contents of $attachment to the authenticated user's wall.


Following the new php-sdk workflow should be easily:

your page should contains the login button with the necessary JS and server-side script to hold the session just like the example.php. One important note here is that you need to ask the user for the publish_stream permission so your login button should look like:

<fb:login-button perms="read_stream,publish_stream"></fb:login-button>

Now on refresh, you check that you have a valid session so you show the submission form, or the "facebook" checkbox...

After submission and in your action file, for example post.php:
You get the $_POST['user_status'] validate it and submit it to your DB and then check for the facebook checkbox AND if you have a valid session:

require '../src/facebook.php';
$facebook = new Facebook(array(
  'appId' => 'APP_ID',
  'secret' => 'APP_SEC',
  'cookie' => true,
));
$session = $facebook->getSession();

if($session && isset($_POST['fb_checkbox'])) {
    $facebook->api('/me/feed', 'post', array('message'=>$_POST['user_status']));
}

More details on what you can post is available here.

This should give you an idea how to start. :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜