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.
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
Tutorial Categories:
I am not able to publish a status on facebook. Although i have added app_id and app_secret _ config,ph
What error you are getting on posting.
when someone click the login button the app ask for permsions than its redirect to call back function and no error occurs
Great tut ! post status with image is possible ?
Yes possible check our status update tutorial for pages same code needed to update status with picture on profile. https://www.phpgang.com/how-to-post-into-a-facebook-page-with-php-using-graph-api_481.html
It is possible to provide a link preview ?
can u please tell me concept of me in ‘$publish = $facebook->api(‘/me/feed’, ‘post’,’.
because i m getting Uncaught OAuthException error
This line is api call to post your status. post your error here so i can help you more.
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
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?
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
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?
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.
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
Just checked its working fine please test it again
Thanks now it is working. Should i re-download the file or the one i have downloaded is already corrected ?
No need to re-download it.
Is it possible to get any answer from facebook after posting a status message? Like the ID of the post or anything?
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);
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
your code for post on page was successful but how can i post on facebook’wall??