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     

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.




         Submit to Reddit     Stumble


Related Posts by Tags: , , , , , , , ,



  • http://twitter.com/fbleadgen fbleadgen

    Great post – I’ve found that I try and use CURL whenever possible over the PHP SDK since it is a more direct and efficient path. These days most PHP-equipped servers have CURL anyway, so compatibility is not really an issue!

    - DB, http://fbleadgen.com

    • Anonymous

      Yes, as long as CURL is available on the server, using it to perform the various methods from the FB API is the best way out!

  • http://twitter.com/learn_everyday learneveryday

    this realy nice help. Thank you very much this helps me to solve my problem which i try to solve from two days.
    You solution helps me to find out how to use curl function in php.
    visit : http://www.learneveryday.net for beginners tutorial.

  • andrei

    If I am using the php code to post a message to a user’s wall (we assume here everything is already set up), how can I know if the message was posted or not? I mean what reply should I look for from facebook after the message is posted and how do i know if it was successful or not?

    • Debjit

      Thank you for raising this doubt. I have updated the post with the answer to your query, check at the end of the post. Thanks again :-)

      • andrei

        Oh, that was fast!
        I was actually looking for the solution of this problem and you wrote it just in time.
        So, I am the one who must say “Thank you” :)

  • Pingback: Post on Facebook Wall Using PHP-SDK and Graph API

  • kaspar

    I am getting back this error:

    {“error”:{“type”:”OAuthException”,”message”:”Error validating application.”}}

    Any clue what might be wrong?

  • http://profiles.google.com/manishgupta2879 manish gupta

    thanks for providing sound knowledge its work fine on my application.

  • http://www.getaphpprogramer.com php programer

    Keep up the good Business to Business posts- great work.

  • Ultimatefrisbee1

    Thank you!  This was very helpful to me!

  • hello

    test

  • Gevovo

    thank you for your explaination!! But i have juste a few question: is it possible to comment a feed sent by our friends with php sdk or other method? i would like to say: not post to the wall but answer messages that our friends sent

    Alan
    gevovo@hotmail.fr

    • Debjit

      You can do that, by issuing a POST on POST_ID/comments using the Graph API. See this as a reference: http://developers.facebook.com/docs/reference/api/post/



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)