PHPEveryday.com PHP and Web Development Tutorial
What are you looking for?


Table of Content
Facebook Programming

First Application
Facebook Platform
First Facebook App

Facebook API
Facebook API
API Friends
API Events
API Groups
API Page
API Users

Facebook Markup Language
6 Type of FBML
FBML Users and Groups
Content Conditionally
fb:if-can-see
fb:if-can-see-photo
fb:if-is-app-user
fb:if-is-friend-with-viewer
Content in Profile Boxes
fb:if and fb:switch
UI Element and Widget
Using UI Element
Discussion Board
Creating Wall in Application
Building Request and Invitations
« Smarty Template Engine Tutorial Facebook Programming: API Friends »

Facebook Programming: Facebook API


Facebook Application Programming Step by step tutorial - part 3. In the previous post, you have tried to create a simple Facebook application. You also use Facebook API. API is a means to communicate between programs. For example, in the previous exercise, you create a program to display a list of friends on Facebook. You can make these programs because of the API. You use Facebook API to display data from Facebook servers.

Currently, the API is the trend in the programming world. Not only are making Facebook API. Google, Amazon, Yahoo, and others also provide an API. With this API, you can create applications by using the features or existing data on their servers.

Calling API

In this post, we will discuss further the API that is on Facebook. We will start from analyzing the program code that we wrote earlier. We'll see, where we use the Facebook API.

<?php

// Get these from http://developers.facebook.com
$api_key = '2c73ae8529134aaaaaaaaaaaaaaaaaaa';
$secret  = '3c041a7cbd797aaaaaaaaaaaaaaaaaaa';

include_once './facebook-platform/php/facebook.php';

$facebook = new Facebook($api_key, $secret);
$user = $facebook->require_login();

?>
<h1>Yummie Tester</h1>
Hello <fb:name uid='<?php echo $user; ?>' useyou='false' possessive='true' />! <br>
Your id : <?php echo $user; ?>.

You have several friends:<br>
<?
$friends = $facebook->api_client->friends_get();
?>

<ul>
<?
foreach($friends as $friend){
	echo "<li><fb:name uid=\"$friend\" useyou=\"false\"></li>";	
}
?>
</ul>

Where do you call the Facebook API?

This is the Object Oriented Programming (OOP). If you do not know about OOP in PHP, I recommend you read this post first. Let more deep understanding of the API. However, if you remain determined to continue to learn Facebook programming is also fine. With a little squeeze your brain, you will come to understand what it is OOP.

Back again to the code above. At the beginning of the line, you are prompted to insert the API Key and secret codes. This kind of entering a username and password database.

$api_key = '2c73ae8529134aaaaaaaaaaaaaaaaaaa';
$secret  = '3c041a7cbd7aaaaaaaaaaaaaaaaaaaaa';

next, we call Facebook client library:

include_once './facebook-platform/php/facebook.php';

On the client library, there is a class code to access Facebook. You must call and initialize a Facebook class.

$facebook = new Facebook($api_key, $secret);

Next, do check whether the user is already logged or not. If have login, you'll get the ID of the user. User ID is inserted into the variable $ user.

$user = $facebook->require_login();

Well, pay attention to the next line:

$friends = $facebook->api_client->friends_get();

This is where you begin to use the Facebook API. You call the method api_client(). Above, you call the Facebook API is called: friends.get. The results of the use of these APIs is a list of ID's friends are accommodated in the variable named $friends. Next you want to display in the browser with the code:

foreach($friends as $friend){
	echo "<li><fb:name uid=\"$friend\" useyou=\"false\"></li>";	
}

Maybe you're wondering, his name is friends.get, but why written friends_get()? Yes, for the PHP programming standards, Point (dot) replaced with underscores. Here's example:

  • Events.get written events_get()
  • Friends.get written friends_get()
  • Comments.get written comments_get()

Previous: Facebook Programming: My First Facebook Application
Next: Facebook Programming: API Friends





Tag: facebook, api Category: PHP API Post : November 05th 2009 Read: 4,266 Bookmark and Share

blog comments powered by Disqus

Database Tutorial
  • Learn PHP MySQL
  • Learn PHP ADOdb
  • Learn PHP Data Object/PDO
  • Learn PHP XML
  • Learn PHP SimpleXML
Security Tutorial
  • Learn PHP Security
  • Learn HTTP Authentication
  • Learn PHPSecureSite
Framework Tutorial
  • Learn CodeIgniter
  • Learn Joomla
  • Learn Smarty
  • Learn Zend Framework
Template Tutorial
  • Learn Joomla Template
  • Learn WordPress Template
API Tutorial
  • Learn Facebook
JS Framework Tutorial
  • Learn MooTools
  • Learn JQuery
AJAX Tutorial
  • Learn AJAX in 10 Minutes
  • Learn AJAX Client Side
  • Learn AJAX PHP
  • Learn AJAX Remote Server
  • Learn AJAX Repetitive
  • Learn AJAX MySQL
  • Learn AJAX Grid
Web Services Tutorial
  • Learn Web Services NuSOAP
  • Learn Web Services WSDL
  • Learn Web Services WSDL Array
  • Learn Web Services .NET Grid
  • Learn Web Services WDDX
Package Post
  • Joomla Intro
  • Joomla Component
  • Joomla Module
  • Joomla MVC
  • Joomla MVC Backend
  • PostNuke Intro
  • Zend Framework Intro
  • Zend Framework Action
  • Zend Framework Database
  • Zend Framework Registry
  • Zend Framework Config
  • Zend Framework Login
  • Zend Framework Session
  • PHP Array Tips
  • PHP File Tips
  • PHP Email
  • PHP Ms Excel
  • PHP Pattern
  • PHP SMS
  • Flash Database
  • PHP Multitier
  • jQuery Introduction
  • jQuery Selectors
  • Portable Web Server
  • Web Mobile Intro
  • Drupal Installation
  • Drupal Configuration