You Are Here: Home » How-To » Programming

How To Programmatically Post To Facebook Wall Via Graph API But Without Using PHP-SDK

By on February 4th, 2011 
Advertisement

Posting content on an user's Facebook wall is very easy using the PHP-SDK. However on various situations you might be required to do a wall post to an user without making use of the PHP-SDK and by directly accessing the Graph API URL. We will be using a PHP utility called CURL in order to perform this programmatic Post to a Facebook Wall using the Graph API but Without using the PHP-SDK.

If you want to Post to a Facebook User's wall using the standard method (Graph API + PHP-SDK) then see this tutorial.

This guide has been written after referring to these following resources from the Facebook Documentation.

developers.facebook.com/docs/reference/api/post/

Using the Graph API, any Facebook user's Wall feed may be accessed by using this URL:

https://graph.facebook.com//feed?access_token=

So, if you want to post something on the wall of an user whose Facebook ID is 12345678, then the URL changes to

https://graph.facebook.com/12345678/feed?access_token=

And if you want to post to the Wall of the current logged in user then you simply replace the USER_ID with me so that the URL becomes:

https://graph.facebook.com/me/feed?access_token=

Now that you know which URL to call in order to do the posting, let us come to the code snippet which will do the actual posting on the Facebook wall of a user via the Graph API without using the PHP-SDK. One thing you must remember that passing an access token is of utmost importance without which any Graph API method would fail, except for the ones which make their info available publicly.

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. Here is the code snippet:

/**************************************************************************************
$touid: Facebook Unique ID of an User. Set the variable to "me" if posting on logged in user's wall
$token: The access token of the user who is currently using your app
$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:

http://i.imgur.com/JCdGh.png

***************************************************************************************/ 

 

function FB_wallpost_wosdk($touid, $token, $msg, $title, $uri, $desc, $pic, $action_name$action_link){
if ( ($touid !="") && ($token !="") && ($msg != "") && ($title != "") && ($uri != "") && ($desc != "") && ($pic != "") && ($action_name != "") && ($action_link != "") ) {
$url = "https://graph.facebook.com/".$touid."/feed";
$attachmentarray(
'access_token' => $token,
'message' => $msg,
'name' => $title,
'link' => $uri,
'description' => $desc,
'picture'=>$pic,
'actions' => json_encode(array('name' => $action_name,'link' => $action_link))
);
// set the target url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)//to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);
return $result;
} else {
return 0;
}
}

 

 

 

 

 

 

 

 

 

 

--

How to find out if the Wall Post was successful?

The function in the code above returns a value which 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: debjit@digitizor.com. You can also hire me for developing Facebook applications for you.

Advertisement







How To Programmatically Post To Facebook Wall Via Graph API But Without Using PHP-SDK was originally published on Digitizor.com on February 4, 2011 - 10:15 pm (Indian Standard Time)