Facebook PHP API Applications: Basic Introduction

If you are a PHP developer interested in learning the PHP API applications of Facebook, then this tutorial can help you get started. Keep reading and you’ll soon know not only how to add Facebook features to your website, but how to develop your own Facebook applications.

Facebook API applications can be categorized into three broad features that can be implemented using an API in the PHP programming language.

  1. Integrating Facebook features, widgets and applications in your website (such as allowing your website users to log in using their Facebook account). In general, its like “adding Facebook to your website.”
  2. Developing Facebook apps (the ones you might encounter frequently when using Facebook).
  3. Facebook mobile apps – this is for mobile-related applications of Facebook.

This is the start of a Facebook PHP API tutorial series. Let’s tackle how to integrate Facebook into your website first, because it is a much simpler approach than the other two, so it provides an easy introduction to implementing the Facebook PHP API.

The Basic Requirements

As a PHP developer, you need to have at least the following to implement Facebook PHP API features.

  1. A verified Facebook account.
  2. Full FTP access to your website files and folders.
  3. A website powered by a PHP/MySQL environment in Apache server. (IIS will do, but using an Apache server is recommended for full compatibility).
  4. A CMS or blogging platform that supports full theme editing in PHP, such as WordPress or Joomla.

A verified Facebook account is required for your application to get an Application ID or Secret identification information.

To register, simply go to http://www.facebook.com/ and make sure your account is fully working. Registering to get an Application ID will be discussed later in this tutorial.

{mospagebreak title=The Facebook Like Button PHP Integration into Your Website}

One of the core benefits of enabling Facebook on your website is increasing traffic. Facebook is one of the most visited websites on the Internet, with high traffic potential for your website.

How the Facebook “Like” button works and how you will use it

Imagine you have hundreds of visitors a day to your website. You can assume that the majority of them have Facebook accounts. Now if someone likes your posts, with this feature they can click the Facebook “Like” button and share that content with their friends on Facebook.

What will happen next is that your post/story will be published or appear on the person’s Facebook profile update pages, like the News Feed. Your post will appear as a link pointing to your website post URL (if it is a blog).

The good thing is that once your post has been published in the news feed (because some Facebook user likes it), you might get additional traffic from referral links. This increase in traffic is beneficial, because it might translate into sales, newsletter sign-ups, etc.

The Facebook code for the “Like” button is this:

<iframe src="http://www.facebook.com/widgets/like.php?href=http://www.thisisyourwebsite.com/yourposturl"
        scrolling="no" frameborder="0"
        style="border:none; width:450px; height:40px"></iframe>

If you would like to integrate this into your website using PHP, you need PHP to grab the URL of the post, and then bind it to the href value of the Facebook iFrame tag.

In PHP you can get the URL by using the following code:

<?php
function getPostURL() {
 $postURL = ‘http’;
 if ($_SERVER["HTTPS"] == "on") {$postURL .= "s";}
 $postURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $postURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $postURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $postURL;
}
$postURL=getPostURL();
echo $postURL;
?>

The PHP variable $postURL contains the value of the post URL. Therefore, you can bind this variable to the Facebook Like Button, as shown in the following section.

{mospagebreak title=Final Working PHP Source Code for the Facebook Like Button}

Here is the source code that binds the variable of your blog post’s URL to the Facebook Like button appearing on the page.

<?php
function getPostURL() {
 $postURL = ‘http’;
 if ($_SERVER["HTTPS"] == "on") {$postURL .= "s";}
 $postURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $postURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $postURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $postURL;
}
$postURL=getPostURL();
 
echo ‘<iframe src="http://www.facebook.com/widgets/like.php?href=’.$postURL.’"
        scrolling="no" frameborder="0"
        style="border:none; width:450px; height:40px"></iframe>’;
?>

If you are using WordPress as a blogging platform, you can easily copy and paste the code above just before the “the_content” function:

<?php the_content( __(‘<p>Read the rest of this entry &raquo;</p>’, ‘arras’) ); ?>

It should then look like the the screen shot below:

If your theme background is black, to make the text more visible on a black/dark background, use the colorscheme=dark attribute, so the iframe code will become:

echo ‘<iframe src="http://www.facebook.com/widgets/like.php?href=’.$postURL.’&amp;colorscheme=dark"
        scrolling="no" frameborder="0"
        style="border:none; width:450px; height:40px"></iframe>’;
 
You can as well adjust the height (default as 40px) to make it look appropriate in your web page.

{mospagebreak title=The Recommendations Widget Facebook Plugin}

Sometimes when the user visits your website, he/she will have no clue as to what your best content is. You can make recommendations, but it might require you to write some complex PHP code. You can also use the most commented post of your blog, but this does not mean that it is the best content.

Therefore, the best content on your website is highly correlated to the number of Facebook users liking a specific piece of content. But how can you display at a single glance the best content of your website?

This is where you will use the Facebook “Recommended” Plugin. This is how it works:

Step 1: Go to the official Facebook recommendations page

Step 2: Under “Domain,” enter your domain. This can be tricky, especially if you are using sub-domains. So here is a quick guide:

  • To display recommended content for your root domain (e.g www.devshed.com), enter that URL under “Domain” (with the www).
  • To display the recommended content for your sub-domain, e.g. forums.devshed.com, enter the sub-domain URL under “Domain.” In this case, it will be: forums.devshed.com

Step 3: Configure the width, height and the other elements to create a good widget that you can paste into your website’s HTML source code.

After the configuration, your output should be similar to the screen shot shown below:

Step 4: Finally, to get the code, simply click “Get Code” and then paste the code into your web page.

{mospagebreak title=Getting an Application ID for Your Facebook API Applications}

The two plug-ins and features discussed already do not require an API for you to implement them in your website. However, some applications will require it — for example, if you need to allow users to log in to your website using their Facebook account.

Step 1. Go to this URL: http://developers.facebook.com/setup/. It requires you to log in using your Facebook account.

Step 2. If you are logged in, Facebook Developer asks you for two things, your site’s name and your site’s URL. 

Under Site Name, type your application name bound with your domain name. For example, if your domain name is php-developer.org, then it will be PHP Developer Apps.

Under Site URL, enter the full URL of your domain’s home page, for example: http://www.php-developer.org/

Step 3. Click “Create Application.”

Facebook will then provide the complete details for you to start your Facebook API Application, particularly the Application ID, Secret key, Application URL, and so forth. For backup, you can copy and paste this information in a text editor and save it safely to your hard drive.

API Implementation Flow Chart

To make the Facebook PHP API easy for you to understand, look at the flow chart below:

Say, for example, that you need to integrate Facebook into your website by allowing users to log in, and then your PHP application will grab user-related information from Facebook and display it on your website (or use it in your other applications).

It starts by checking to see if the user has been logged in. If the user is not logged in, a Facebook login button will be presented. Once the user has logged in, your PHP application will retrieve user-related information from Facebook.

In this process, Facebook will authenticate your PHP application using the Application ID and secret key (in addition to the Facebook Application URL you  provided during registration).

Once authenticated, you can use file_get_contents PHP function to retrieve user information, so that your PHP web application can use that data on your own website.

In the second part of this tutorial, you will learn how to create an actual PHP web application implementing the above flow chart.

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan