February 2, 2024 5:01 am

How to publish status on Facebook with Graph API

Few days ago I have started working on status publishing on Facebook using PHP and faced few problems to do that so, today I have decided to write an article on it how to publish status on Facebook using graph API with source code download and a demo so you never face any issue next time to make something like that.

How to publish status on Facebook with Graph API

[wpdm_file id=43]DEMO

To create facebook application follow instruction on my previous post How to Login with Facebook Graph API in PHP

Script contains two folders called oAuth and images with PHP files.

src

– base_facebook.php // Class Get user Information

– facebook.php // Class Get user Information

– config.php // Configuration file

images

html.inc // html design view

index.php // Main index file

PHP Code

Edit config.php

<?php

$config['callback_url']         =   'CALL BACK URL/?fbTrue=true'; //   /?fbTrue=true allow you to code process section.

//Facebook configuration
$config['App_ID']      =   'Your App ID';
$config['App_Secret']  =   'Your App Secret'; 

?>

Shows you 2 buttons ask for permission and show text box to add status to publish 2nd button publish status right after permission allowed.

index.php

<?php
require 'src/config.php';
require 'src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => $config['App_ID'],
  'secret' => $config['App_Secret'],
  'cookie' => true
));

$publish = $facebook->api('/me/feed', 'post',
        array('access_token' => $params['access_token'],
        'message'=>'This Messsage published by PHPGang.com Demo.',
        'from' => $config['App_ID']
        ));
        $message = 'Default status updated.<br>';
?>

In this file me show you instead me you can also write your id you also can change this me to any your friend id to post feed on any of your friends wall if he opens to do that you have to get its permission

Note: Publish status on your wall needs publish_stream permission

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:

21 responses to “How to publish status on Facebook with Graph API”

  1. Avais Khatri says:

    I am not able to publish a status on facebook. Although i have added app_id and app_secret _ config,ph

  2. test says:

    Great tut ! post status with image is possible ?

  3. Val says:

    It is possible to provide a link preview ?

  4. ashulbha12 says:

    can u please tell me concept of me in ‘$publish = $facebook->api(‘/me/feed’, ‘post’,’.

    because i m getting Uncaught OAuthException error

    • huzoorbux says:

      This line is api call to post your status. post your error here so i can help you more.

      • ashulbha12 says:

        Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in /home/yogendra/public_html/facebook/src/base_facebook.php on line 1243

    • ashulbha12 says:

      Uncaught OAuthException: (#200) The user hasn’t authorized the application. and i have changed i have change the secret id and app id in config.php .

      Should i replace ‘me’ to any ID?

    • huzoorbux says:

      If your app asks for more than than public_profile, email and user_friends it will require review by Facebook before your app can be used by people other than the app’s developers.

      The time to review your app is usually about 7 business days. Some extra-sensitive permissions, as noted below, can take up to 14 business days.

      Learn more about our review process https://developers.facebook.com/docs/apps/review

  5. ankit says:

    i have change the secret id and app id in config . and when i update the post it show me error “Uncaught OAuthException: (#200) The user hasn’t authorized the application to perform this action thrown in /facebook/src/base_facebook.php on line 1243”.
    what should i do for this?

    • huzoorbux says:

      If your app asks for more than than public_profile, email and user_friends it will require review by Facebook before your app can be used by people other than the app’s developers.

      The time to review your app is usually about 7 business days. Some extra-sensitive permissions, as noted below, can take up to 14 business days.

      Learn more about our review process https://developers.facebook.com/docs/apps/review.

  6. Max John says:

    I tested the demo online and i get this error

    Fatal error: Uncaught FacebookApiException: Duplicate status message
    thrown in
    /hermes/waloraweb002/b2497/pow.huzoorbuxcom/htdocs/phpgang/demo/post-status-facebook/src/base_facebook.php
    on line 1243

  7. Stefanie Beutlich says:

    Is it possible to get any answer from facebook after posting a status message? Like the ID of the post or anything?

    • huzoorbux says:

      Yes its possible use var_dump($publish); after publish and get result returned by facebook.

      $publish = $facebook->api('/me/feed', 'post',

      array('access_token' => $params['access_token'],

      'message'=>'This Messsage published by PHPGang.com Demo.',

      'from' => $config['App_ID']

      ));

      var_dump($publish);

  8. Sam Bot says:

    help me plz error :
    Fatal error: Uncaught CurlException: 7: couldn’t connect to host thrown in /home/xxxxxxxxx/facebook_sdk/scr/base_facebook.php on line 1012

  9. harpreet says:

    your code for post on page was successful but how can i post on facebook’wall??

Leave a Reply

Your email address will not be published. Required fields are marked *