Post update on user wall using Facebook’s Graph API


  1. Previous
  2. Next
  3. Recent

    1. SBI netbanking vs Axis Bank netbanking: Head to Head Comparision
    2. How to Reset Transaction Password in Axis Bank netbanking
    3. Create bootable Windows 7 pen drive from ISO
    4. Understanding Domain Name: Protocol, Hostname, TLD and Subdomain
    5. Domain vs Website vs Hosting, know the difference
    6. What is wwwz, www8 in website url?
    7. 16 flop Bollywood movies that deserves watching once
    8. Article vs Blog Posts: Difference explained with example
    9. 56 little known facts about Sachin Tendulkar (the god of cricket)
    10. Lock/Unlock your PC with Pen Drive using Predator

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.

We add a textarea to take the input. Then we call to JavaScript function ‘posttofeeds()’ that’ll make HTTP POST request to Graph API to publish update.


<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.


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script language='Javascript'>

 function posttofeeds(token)
 {
 var param='access_token='+token+'&message='+flipform.outputdata.value;
 $.post('https://graph.facebook.com/me/feed',param,function(data){alert('Update posted to user wall');});
 }

</script>

That’s all! In the example, an alert will show up when update is posted successfully.

Unlike us, most applications on Facebook make the same HTTP POST Request using JavaScript SDK. JavaScript SDK is a bulk sized file which can make an app slow. Slow Facebook apps often throw CurlException which can render an app unusable.


blog comments powered by Disqus