You Are Here: Home » How-To » Programming

How To Programmatically Post To Facebook Wall Using Graph API & PHP-SDK

By Debjit on March 18th, 2011 
Advertisement

We have already told you how to programmatically post to an user's Facebook Wall via Graph API and CURL and  without using PHP-SDK. Well, In this article we will show you how to build and attachment for the Facebook Wall Post and post it to any user's Facebook wall using the PHP-SDK and the Graph API.

Please not that in order to post something on any user's Facebook wall, you will need to ask the user for the publish_stream extended permission. Wall posts can be easily made by issuing a POST request to a Facebook User's wall feed with the required attachment variable. Check this working code snippet below for more details.

We have assumed that you have already initialized the Facebook object. If not, then find out How to initialise a Facebook Object using PHP-SDK.

/**************************************************************************************
$touid: Facebook Unique ID of an User. Set the variable to "me" if posting on logged in user's wall
$msg: The Message to be posted above the actual link post
$name: Title of the URL to be posted
$link: Direct (Full) URL of the Link to be posted
$description: A short description text about the post
$pic: Absolute URL of the accompanying image to be posted
$action_name & $action_link: Title & URL of the Action link for the post, see this image:

View post on imgur.com


$facebook: The facebook object which can be obtained from the PHP-SDK
***************************************************************************************/
$attachmentarray(
'message' => $msg,
'name' => $title,
'link' => $uri,
'description' => $desc,
'picture'=>$pic,
'actions' => json_encode(array('name' => $action_name,'link' => $action_link))
)

//Posting to the wall of an user whose Facebook User ID is known
try {

$result = $facebook->api('/'.$touid.'/feed', 'post', $attachment);

} catch (FacebookApiException $e) {

//notify error

}

//Posting to the wall of the currently logged-in Facebook user
try {

$result = $facebook->api('/me/feed', 'post', $attachment);

} catch (FacebookApiException $e) {

//notify error

}

--

How to find out if the Wall Post was successful?

In the above code snippet, the variable $result contains the Facebook ID of the wall post you just made in the following format:

[FACEBOOK_USER_ID]_[POST_ID]

So, if the logged in user's Facebook id is 12345678 and the id of the wall post is 987654321, then the function's return value would be:

12345678_987654321

So, in order to find out if the wall post was successful, you can search for the logged-in user's Facebook ID in the return value using the PHP function strpos(). A TRUE value would imply that the wall post was successful.

If you are facing any problem with the implementation do write to me at: [email protected]. You can also hire me for developing Facebook applications for you.

Advertisement







How To Programmatically Post To Facebook Wall Using Graph API & PHP-SDK was originally published on Digitizor.com on March 18, 2011 - 2:37 pm (Indian Standard Time)