Few days back I had posted an article about Facebook Graph API connection and explained how to request facebook access token and reading home timeline feed. This post is sequel how to update Facebook wall status from third party site with existing access token in "users" table. Try demo at labs.9lessons.info
Live Demo
Previous Post: Facebook Graph API Connect with PHP and Jquery
Update.php
Contains HTML and PHP code. Getting facebook_access_token and facebook_id values from users table where uid=$user_session_id.
<?php
include('db.php');
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$status=$_POST['status'];
$sql=mysql_query("select facebook_id,facebook_access_token from users where username='$user_session'");
$row=mysql_fetch_array($sql);
$facebook_id=$row['facebook_id'];
$facebook_access_token=$row['facebook_access_token'];
//Facebook Wall Update
params = array('access_token'=>$facebook_access_token, 'message'=>$status);
$url = "https://graph.facebook.com/$facebook_id/feed";
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => $params,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_VERBOSE => true
));
$result = curl_exec($ch);
// End
}
?>
//HTML
<form method="post" action="">
<textarea name="status"></textarea>
<input type="submit" value=" Update "/>
</form>
include('db.php');
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$status=$_POST['status'];
$sql=mysql_query("select facebook_id,facebook_access_token from users where username='$user_session'");
$row=mysql_fetch_array($sql);
$facebook_id=$row['facebook_id'];
$facebook_access_token=$row['facebook_access_token'];
//Facebook Wall Update
params = array('access_token'=>$facebook_access_token, 'message'=>$status);
$url = "https://graph.facebook.com/$facebook_id/feed";
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => $params,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_VERBOSE => true
));
$result = curl_exec($ch);
// End
}
?>
//HTML
<form method="post" action="">
<textarea name="status"></textarea>
<input type="submit" value=" Update "/>
</form>
It's like I want to know something and BOOM! You make a tutorial. Thanks! :D
ReplyDeleteHi
ReplyDeletereally a helpful post.
I have a query.. In this code which variable should contain the status string.
brilliant...;)
ReplyDeleteWithout using curl:
ReplyDeleteinclude "facebook.php";
if ($_POST['boton'] == "Public" ){
$estado=$_POST['publicar'];
$propiedades=array('message' => $estado);
$facebook->api('/me/feed','post',$propiedades);
}
o api('/facebook_id/feed'
@UrbaN77
ReplyDeleteFacebook Official PHP SDK you mentioned also uses cURL to interact with their API.
@Tamada
There is a small syntax glitch in the code you provided for update.php at around line 12.
Facebook has become a great device for making stats and database...
ReplyDeletevery usefull and inportante post...thank's for sharing
ReplyDeleteworks great!! thx. how do you add an icon before for the name of the website
ReplyDeleteGr8 bro..
ReplyDeletenice post..
That's it.???
ReplyDeleteplease donate money to this guy, he is so helpful
ReplyDeletethank you brilliant exactly what i was looking for!!!
ReplyDeleteHi i am abhishek...........
ReplyDeletemy issue is
Notice: Undefined variable: user_session in C:\xampp\htdocs\FBStatus\home.php on line 3
// Javascript Code------------------------------------- // HTML Code---------------------------------------------
Notice: Undefined variable: facebook_uid in C:\xampp\htdocs\FBStatus\home.php on line 40
please help meee................
hi tamada! demo please ?
ReplyDeleteWhich variable contains the text - via "9lessons lab" ?
ReplyDeleteCould u give the script plzz... ?
ReplyDeleteanother thing, how can you impersonate the Page instead of using your current user ????
ReplyDeleteCan you make the this things. :P
ReplyDeletethe user or someone who is looking for comment can simply find it by choosing the time and date and so on. or name or places or any code:P hope you understand my english. thnx.
Ok so im a dummy, the users table? im guessing it contains the facebook id of the user right? is that something i have to get off the user? how does this work?
ReplyDeleteI want to place it below photos for commenting on my clients site
such great it's really help me!
ReplyDeleteCan you help me when I test it Error:
ReplyDeleteFatal error: Uncaught exception 'Exception' with message 'Facebook needs the CURL PHP extension.' in C:\wamp\www\Facebook_API\facebook.php:20 Stack trace: #0 C:\wamp\www\Facebook_API\test.php(3): include() #1 {main} thrown in C:\wamp\www\Facebook_API\facebook.php on line 20
i am getting following error.
ReplyDelete{"error":{"message":"Error validating access token: Session has expired at unix time 1321603200. The current unix time is 1321868555.","type":"OAuthException"}}
Very nice and efficient blog..
ReplyDeletei get the post id, but i don't see the post on my wall .. where is the problem ?
ReplyDeletehi will this still work for the new API? also, do you have a tutorial wherein the api can post status together with an image? something like this:
ReplyDeletehttp://i.imgur.com/Edtos.png
sorry for the confusing messages of the image. thanks in advance!
can we tell me about db.php file
ReplyDeleteplease tell me about tha db.php file
ReplyDeletenice post! but can u tell us how to post on the wall using API?
ReplyDeletepls upload the update.php here bcos i cant copy the code.tanx
ReplyDeleteparams = array('access_token'=>$facebook_access_token,
ReplyDeletefix it with
$params = array('access_token'=>$facebook_access_token,
:P nice tutorial !!
Thank's a lot for this tutorial!
ReplyDeleteWhen i am trying to use the code i am getting the following error "Malformed access token"
ReplyDeleteSame error :{"error":{"message":"Malformed access token
ReplyDeletewhere can i find db.php file ? plz help
ReplyDeleteCode no longer posts to facebook i think you need permission to the app which this code does not make the app do.
ReplyDeleteWhere can I find db.php?
ReplyDeletegd bro
ReplyDeleteThat's amazing web blog and thanks for sharing..
ReplyDeleteNice post
ReplyDelete