This tutorial will teach how to update Facebook status using PHP graph API.It uses the recent Facebook PHP SDK available on Github.
Before beggining get acquainted with basics of facebook app development
Step 1: Download the SDK from github and extract the files.
Step 2: Create a new Facebook application
Go to https://developers.facebook.com/apps and click Create New App to provide application name and namespace.
Namespace will be used on the URL of application.[example: apps.facebook.com/namespace]
If you need hosting there is an option to use Heroku’s free hosting service. For now we are not going to use it hence leave the option unchecked.
The one advantage of using the hosting service is they provide the SSL enabled page “ https:// “ .Secure canvas url will be displayed if the user have secure browsing enabled.
Step 2: Fill the details
Select the “App on the Facebook” option and enter the canvas URL details.
The canvas page is the actual location of the file which will be rendered in the facebook.
Change the application icon as per your needs.
Click save changes to complete the setup.
Step 3: After completing the setup take a note of app id and app secret key.
Step 4: Check the canvas page
Now open the application url to check whether the canvas is loading correctly.
Note: If your namespace is devlupapps then your app url will be apps.facebook.com/devlupapps
If there is any problem on this step it means there is an error in your canvas page location.
Step 5: Authorize
When a new user opens the application page he should authorize the application before accessing the information.This is called as authorization For updating your facebook status you need to get authorized by the user to post the update on his behalf.
You should redirect the page if the user is not logged in to Facebook.
Step 6: Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php require 'src/facebook.php'; // Create our Application instance (replace this with your appId and secret). $facebook = new Facebook(array( 'appId' => '236528216480691', 'secret' => 'ef0169cd4eebb072e00e0d597ff2c7af', )); // Get User ID $user = $facebook->getUser(); if ($user) { try { // Proceed knowing you have a logged in user who's authenticated. $user_profile = $facebook->api('/me'); } catch (FacebookApiException $e) { error_log($e); $user = null; } } |
To publish update to time line you need to get Publish_stream permission.
1 2 3 4 5 6 7 8 9 | // Login or logout url will be needed depending on current user state. if ($user) { $logoutUrl = $facebook->getLogoutUrl(); } else { //redirect user to authorize dialog $login_url = $facebook->getLoginUrl($params = array('scope' => "publish_stream")); echo ("<script> top.location.href='".$login_url."'</script>"); } ?> |
If you click allow button a new access token will be generated.Using this token you can post the status update.
1 2 3 4 5 6 7 | $access_token = $facebook->getAccessToken(); $attachment = array( 'access_token' => $access_token, 'message' => "This is a new status message", ); $facebook->api('/'.$user.'/feed', 'POST', $attachment); echo "Your status Posted"; |
Design a html form to get the input text and post as status message.
Complete Code
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <?php require 'src/facebook.php'; // Create our Application instance (replace this with your appId and secret). $facebook = new Facebook(array( 'appId' => '236528216480691', 'secret' => 'ef0169cd4eebb072e00e0d597ff2c7af', )); // Get User ID $user = $facebook->getUser(); if ($user) { try { // Proceed knowing you have a logged in user who's authenticated. $user_profile = $facebook->api('/me'); } catch (FacebookApiException $e) { error_log($e); $user = null; } } // Login or logout url will be needed depending on current user state. if ($user) { $logoutUrl = $facebook->getLogoutUrl(); } else { //redirect user to authorize dialog $login_url = $facebook->getLoginUrl($params = array('scope' => "publish_stream")); echo ("<script> top.location.href='".$login_url."'</script>"); } ?> < !doctype html> <html xmlns:fb="http://www.facebook.com/2008/fbml"> <head> <title>php-sdk</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>Whats on our Mind</title> <link rel="stylesheet" type="text/css" href="src/view.css" media="all"/> <script type="text/javascript" src="src/view.js"></script> </head> <body id="main_body"> <img id="top" src="images/top.png" alt=""/> <div id="form_container"> <h1><a>Whats on our Mind</a></h1> <form id="form_545340" class="appnitro" method="post" action="process.php"> <div class="form_description"> <h2>Whats on our Mind</h2> <p>Update your facebook status easily.</p> </div> <ul> <li id="li_1" > <label class="description" for="element_1"> </label> <div> <textarea id="txtstatus" name="txtstatus" class="element textarea small"></textarea> </div> </li> <li class="buttons"> <input type="hidden" name="form_id" value="545340" /> <input id="saveForm" class="button_text" type="submit" name="submit" value="Publish" /> </li> </ul> </form> <div id="footer"> <?php if ($user): ?> <a href="<?php echo $logoutUrl; ?>">Logout</a> <?php else: ?> <div> Login using OAuth 2.0 handled by the PHP SDK: <a href="<?php echo $loginUrl; ?>">Login with Facebook</a> </div> <?php endif ?> </div> </div> <img id="bottom" src="images/bottom.png" alt=""/> </body> </html> |
process.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php require '/src/facebook.php'; // Create our Application instance (replace this with your appId and secret). $facebook = new Facebook(array( 'appId' => '236528216480691', 'secret' => 'ef0169cd4eebb072e00e0d597ff2c7af', )); // Get User ID $user = $facebook->getUser(); //get the status message $status=$_POST["txtstatus"]; $access_token = $facebook->getAccessToken(); $attachment = array( 'access_token' => $access_token, 'message' => $status, ); $facebook->api('/'.$user.'/feed', 'POST', $attachment); echo "Your status Posted"; ?> |
I am getting below error :
Warning: require(/src/facebook.php) [function.require]: failed to open stream: No such file or directory in /home/jeyagan/public_html/projects/phpFB/process.php on line 2
Fatal error: require() [function.require]: Failed opening required ‘/src/facebook.php’ (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /home/jeyagan/public_html/projects/phpFB/process.php on line 2
Shyam,
Remove the “/” before the src (/src/facebook.php)