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




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

advertisements


blog comments powered by Disqus