Facebook’s Graph API simplifies the exchange of user data with the Facebook servers. An app can inquire about user’s private information including feeds, comments and likes, see his connections with friends, and do lot more stuffs in easy steps.
Using Graph API, a Facebook app can also make post on user’s wall after getting permission.
Today we build Facebook application that post on user’s wall by making HTTP POST request to Graph API.
1. Get User Authentication
First we need to get user authentication with permission to post updates on his behalf [publish_stream permission].
Here is the total syntax for making such authentication request,
<?php $auth_url = "https://www.facebook.com/dialog/oauth?client_id=" . $app_id . " &redirect_uri=" . urlencode($canvas_page)."&scope=publish_stream"; echo("<script> top.location.href='" . $auth_url . "'</script>"); ?>
For an app named(namespace) ‘post-on-user-wall’, with app_id=’12345689′ the authentication url would look like,
<?php $auth_url = "https://www.facebook.com/dialog/oauth?client_id=123456789 &redirect_uri=" . urlencode('https://apps.facebook.com/post-on-user-wall')." &scope=publish_stream"; echo("<script> top.location.href='" . $auth_url . "'</script>"); ?>
2. Get Access Token
Once our app is given authentication, we need the access token. Access token is needed to tinker with user data. As we’ll be publishing updates, so we need access token here too.
2.1 Before getting access token, include Facebook php sdk files in your app.
<?php require_once('../facebook-php-sdk/src/facebook.php'); ?>
2.2 We’ll get access token by calling function of Facebook php sdk.
<?php $access_token = $facebook->getAccessToken(); ?>
3. Post update
Now we have everything we needed to post on user’s wall. Its time to take user input. We’ll take this input and post to his wall.
<form method='POST' id='flipform'> <textarea name='outputdata' id='outputdata' class='iodata'></textarea> <input type='button' class='but' value='Post my update' id='sub' onclick=posttofeeds('<?php echo $access_token; ?>')> </form>
Since we’re making Ajax call using jQuery, first we download the jQuery file and then make the call to Facebook’s Graph API.
That’s all! In the example, an alert will show up when update is posted successfully.