How To: Post A Message On The User Wall Using Facebook Graph API
This is a quick post to demonstrate how to post a status message on the user wall using the Facebook Graph API. First of all, you need the publish_stream
extended permission to perform this action.
Using the PHP-SDK
1
2
3
4
5
6
|
$args = array ( 'message' => 'Hello from my App!' , 'link' => 'http://www.masteringapi.com/' , 'caption' => 'Visit MasteringAPI.com For Facebook API Tutorials!' ); $post_id = $facebook ->api( "/me/feed" , "post" , $args ); |
In the above example we posted a message and a link using the PHP-SDK.
Using the JavaScript-SDK
This is an example of posting a message using the JavaScript SDK taken from the FB.api
documentation:
1
2
3
4
5
6
7
8
|
var body = 'Reading Connect JS documentation' ; FB.api( '/me/feed' , 'post' , { message: body }, function (response) { if (!response || response.error) { alert( 'Error occured' ); } else { alert( 'Post ID: ' + response.id); } }); |
Notes
Please note that you can still post to a user wall even if the user is not connected to your application and without offline_access
permission just by knowing the user id:
publish_stream:
Enables your application to post content, comments, and likes to a user’s stream and to the streams of the user’s friends. With this permission, you can publish content to a user’s feed at any time, without requiringoffline_access
. However, please note that Facebook recommends a user-initiated sharing model.
1
2
3
4
5
6
|
$args = array ( 'message' => 'Hello from app' , 'link' => 'http://www.masteringapi.com/' , 'caption' => 'Visit MasteringAPI.com For Facebook API Tutorials!' ); $post_id = $facebook ->api( "/$USER_ID/feed" , "post" , $args ); |