Tutorial facebook SDK Version 4.0.0 for PHP . Facebook provides a free SDK for developers to develop applications to access Facebook Graph API. Facebook provides a web server for each programming language. In this case, I use the PHP SDK for communication with facebook API. In my opinion, the previous version of the PHP SDK version 3.2.3, it is relatively easier to use than using the php version to 4.0.0. For more information about the tutorial facebook SDK for php can be read directly at the source
In this article I will share the basics of using Facebook PHP SDK Version 4.0.0 step by step. The things to be learned like how to create a facebook app, facebook How to communicate with the API, a simple example of the use of facebook app.
Please know, Facebook has tightened the requirement for developers in the development of facebook app. In fact you need to do the filing to be able to access some APIs that are sensitive like the app details, manage notifications, manage page, or the most frequently used / searched looking for website developers that “publish action” and “publish page“. The terms of the filing are considered easy if you actually make an application with a positive goal.
Table of Contents
How to create your facebook app
1. Login to your facebook account developer here
2. Register new app as developer
3. Verify your new facebook app with your phone number and you will recieve confirmation code
4. Give your application name and click create application id button
5. Congrat, you will get app id and app secret number from your facebook app
The first step for creating facebook App is finished, Next you need to complete your application detail , so you can get more access to facebook API. Please complete your detail at your facebook app dashboard
Getting Started tutorial using facebook sdk 4.0.0 with PHP begginer
1. Download Facebook PHP SDK 4.0.0 from download facebook php sdk 4.0.0 (The Facebook SDK for PHP v4 requires PHP 5.4 or greater.)
2. Extract zip file and copy Facebook folder from src folder to your root folder of your site
3. create php file as index.php
Script how to get started using the PHP SDK 4.0 for facebook app
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 | <?php session_start(); require_once( 'Facebook/FacebookSession.php' ); require_once( 'Facebook/FacebookRedirectLoginHelper.php' ); require_once( 'Facebook/FacebookRequest.php' ); require_once( 'Facebook/GraphObject.php' ); require_once( 'Facebook/GraphUser.php' ); require_once( 'Facebook/FacebookSDKException.php' ); require_once( 'Facebook/FacebookRequestException.php' ); require_once( 'Facebook/HttpClients/FacebookHttpable.php' ); require_once( 'Facebook/HttpClients/FacebookCurl.php' ); require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' ); require_once( 'Facebook/Entities/AccessToken.php' ); require_once( 'Facebook/Entities/SignedRequest.php' ); require_once( 'Facebook/FacebookResponse.php' ); require_once('Facebook/FacebookPermissionException.php'); require_once('Facebook/FacebookAuthorizationException.php'); use Facebook\FacebookSession; use Facebook\FacebookRedirectLoginHelper; use Facebook\FacebookRequest; use Facebook\GraphObject; use Facebook\FacebookSDKException; use Facebook\FacebookRequestException; use Facebook\FacebookResponse; use Facebook\FacebookPermissionException; use Facebook\GraphUser; use Facebook\FacebookAuthorizationException; FacebookSession::setDefaultApplication('your-app-id', 'your-secret-id'); $helper = new FacebookRedirectLoginHelper('http://localhost/facebookapp/index.php' ); try { $session = $helper->getSessionFromRedirect(); } catch( FacebookRequestException $ex ) { echo $e->getMessage(); } catch( Exception $ex ) { echo $e->getMessage(); } if ( $session) { print_r($session); try { $request = new FacebookRequest($session, 'GET', '/me'); $response = $request -> execute(); $me = $response-> getGraphObject(); echo 'My facebook name is : '.$me->getProperty('name'); } catch(FacebookRequestException $e) { echo "Exception occured, code: " . $e->getCode(); echo " with message: " . $e->getMessage(); } } else { $auth_url = $helper->getLoginUrl(); echo '<a href="' . $auth_url . '">Login</a>'; } |
Check this script to your server! , at my example : http://localhost/facebookapp/index/php
You will get request application to your facebook account like image below
Choose “Okay” to accepted, and then you will redirect to your redirect link you have registered on your facebook account develepor.
You can change $me->getProperty(‘name’) with another facebook property like id, photos, location, etc, read the reference here
Yes, your first tutorial facebook sdk V4 is finish. How about auto post to your facebook timeline or page ? Copy this script below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | if ( $session) { try { $postrequest = new FacebookRequest($session, 'POST', '/me/feed',array('message' => 'my first timeline using facebook app')); $postresponse = $postrequest -> execute(); $postme = $postresponse-> getGraphObject(); echo $postme->getProperty('id'); } catch(FacebookRequestException $e) { echo "Exception occured, code: " . $e->getCode(); echo " with message: " . $e->getMessage(); } } else { $auth_url = $helper->getLoginUrl(); echo '<a href="' . $auth_url . '">Login</a>'; } |
I know, you will get error message something like this
Exception occured, code: 200 with message: (#200) The user hasn’t authorized the application to perform this action
How to post article to your facebook page and resolve error message “Exception occured, code: 200 with message: (#200) The user hasn’t authorized the application to perform this action” i will explain in the next article.
Leave a Reply