简体   繁体   中英

How do I update Facebook status from my own website?

I have my own site which I am creating a blog/news entries but I want these to upload directly to my Facebook page as I add them.

Is there a simple way to do this?

My code is something like this

<?php

$title = "$_Post['title'];
$article = "$_Post['article'];
// Upload sql and query code code

?>

Is there a code or api I can use to also update to my facebook page

You will need to sign up to become a facebook developer, create an application, and then using the PHP SDK you can do exactly this.

Developers site: http://developers.facebook.com/

PHP SDK: http://developers.facebook.com/docs/reference/php/

Example code:

http://www.masteringapi.com/tutorials/how-to-post-on-facebook-page-as-page-not-as-admin-user-using-php-sdk/31/

<?php
// This code is just a snippet of the example.php script
// from the PHP-SDK <http://github.com/facebook/php-sdk/blob/master/examples/example.php>
require '../src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'app_id',
  'secret' => 'app_secret',
));

// Get User ID
$user = $facebook->getUser();

if ($user) {
  try {
    $page_id = 'page_id';
    $page_info = $facebook->api("/$page_id?fields=access_token");
    if( !empty($page_info['access_token']) ) {
        $args = array(
            'access_token'  => $page_info['access_token'],
            'message'       => "I'm a Page!"
        );
        $post_id = $facebook->api("/$page_id/feed","post",$args);
    }
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl(array('scope'=>'manage_pages,publish_stream'));
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM