Post update on user wall using Facebook’s Graph API

  1. Previous
  2. Next
  3. Recent

    1. Signs 'He' May be Cheating on You
    2. 7 Vows of Hindu Marriage - Know What 'Saat Vachans' Mean
    3. RuPay vs Visa vs Mastercard vs American Express- A Comparison
    4. What is RuPay? Everything You Need to Know About India's Domestic Payment System!
    5. Top 10 Online Portals to Find a High Paying Job in India
    6. Top Indians who Revolutionized the IT Sector in India
    7. 8 Initiatives by Narendra Modi that Could Change the Future of the Nation
    8. 8 Work-from-home Jobs for Housewives, Students, and Part timers
    9. Complete Company Registration Process in India - Explained
    10. Change Name in India in 5 Steps - Filing Affidavit, Newspaper Ad & Gazette Notification

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