• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
seegatesite header

Seegatesite.com

Seegatesite.com - Programming Tutorial , Sharing , How and Learn Together

  • TOOLS
    • Bootstrap Navbar Online Generator
    • Customize Sidebar Menu Bootstrap 3
    • Bootstrap Demo
  • ADVERTISE
  • CATEGORIES
    • Android
    • Blogging Tips
    • Database
    • CSS
    • Info Gadget
    • Javascript
    • Linux
    • PHP
    • Various
    • WordPress
  • Q&A
  • PHP
  • JAVASCRIPT
  • JQUERY
  • ANGULAR
  • WORDPRESS
  • SEO
  • REACT
🏠 » PHP » Tutorial Facebook SDK V4 for PHP for Beginners

Tutorial Facebook SDK V4 for PHP for Beginners

By Sigit Prasetya Nugroho ∙ May 12, 2015 ∙ PHP ∙ Leave a Comment

Share : TwitterFacebookTelegramWhatsapp

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

  • 1 How to create your facebook app
    • 1.1 Script how to get started using the PHP SDK 4.0 for facebook app

How to create your facebook app

1. Login to your facebook account developer here
2. Register new app as developer
How to create facebook app with php - Tutorial facebook sdk v4.0.0 php3. Verify your new facebook app with your phone number and you will recieve confirmation code
how to verify facebook app with free - Tutorial facebook sdk
4. Give your application name and click create application id button
how to use facebook sdk 4 with php
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

how to create facebook apps mobile

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

copy facebook sdk to your root folder
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
facebook app permission

Choose “Okay” to accepted, and then you will redirect to your redirect link you have registered on your facebook account develepor.

facebook app request name

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 resolve facebook app error code 200

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.

Another PHP Related Post :

  • Tutorial Create Simple POS Using ReactJS And Laravel Lumen Part 1
  • How To Replace String With Another String In PHP
  • Login Page – Tutorial CRUD Client and API Server Using JQuery And Lumen Part 2
  • Tutorial CRUD Client and API Server Using JQuery And Lumen Part 1
  • How To Solve Problems Illegal mix of collations (latin1_swedish_ci,IMPLICIT) In Laravel
  • How To Resolve No ‘Access-Control-Allow-Origin’ Header In Lumen

Avatar for Sigit Prasetya Nugroho

About Sigit Prasetya Nugroho

This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Welcome to my Home,

Avatar for Sigit Prasetya NugrohoThis site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.



Popular Articles

Checked checkbox AdminLTE Bootstrap in Jquery

November 4, 2014 By Sigit Prasetya Nugroho 7 Comments

Simple create date format validation with jqueryUI

December 21, 2014 By Sigit Prasetya Nugroho Leave a Comment

Create Simple Progress Bar for Fake Online Generator with Jquery

January 10, 2015 By Sigit Prasetya Nugroho Leave a Comment

22+ Coolest Free Jquery Plugin For Premium Theme

October 3, 2015 By Sigit Prasetya Nugroho Leave a Comment

Easy Build Your Anti Copy Paste Plugin

October 6, 2015 By Sigit Prasetya Nugroho Leave a Comment

Popular Tags

adminlte (15) adsense (13) adsense tips (4) affiliate amazon (13) amazon (12) Android (8) angular (16) angular 4 (12) angular 5 (4) asin grabber (3) Bootstrap (27) codeigniter (5) create wordpress theme (5) crud (8) css (6) free wordpress theme (7) google adsense (4) imacros (4) increase traffic (6) jquery (34) laravel (10) laravel 5 (5) learn android (5) lumen api (4) modal dialog (5) mysql (6) nodeJs (4) optimize seo (4) pdo (6) php (30) plugin (53) pos (8) Publisher Tips (5) react (6) Reactjs (9) SEO (37) theme (17) tutorial angular (5) tutorial angular 4 (6) tutorial javascript (10) tutorial javascript beginners (4) twitter (3) wordpress (18) wordpress plugin (13) XMLRPC (5)




  • About
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

©2022 Seegatesite.com