How do I update Facebook status from my own website?

后端 未结 1 1772
旧时难觅i
旧时难觅i 2021-01-03 15:19

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?

1条回答
  •  迷失自我
    2021-01-03 15:35

    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/

    
    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'));
    }
    ?>
    

    0 讨论(0)
提交回复
热议问题