Book Image

Facebook Application Development with Graph API Cookbook

By : Shashwat Srivastava, Apeksha Singh
Book Image

Facebook Application Development with Graph API Cookbook

By: Shashwat Srivastava, Apeksha Singh

Overview of this book

<p>With a user base of nearly 800 million people, Facebook is the number one social networking platform. Applications can be created to interact with this huge user base in various ways both inside and outside Facebook. These applications, if developed effectively and efficiently, offer a free medium for promotion and publicity of a product or an organization.<br /><br />Facebook Application Development with Graph API Cookbook covers both the concepts and implementations necessary to develop Facebook applications and provides ready to use code for common scenarios faced by a developer while creating these applications. It incorporates the newly launched Facebook Graph API and also presents the reader with some intuitive ready to use applications.<br /><br />This book guides the reader step by step, from start to finish, through various stages of Facebook application development. It begins by exploring the Facebook application registration and discussing the verification and authentication technique. It then takes you through the various ways in which you can use Facebook Graph API for interacting with users such as posting on a user’s wall, tagging a user in a picture, etc. Accessing complex Facebook user data by formulating a series of queries, doing client side scripting and incorporating Facebook Dialog interface are some other features that have been incorporated in this book. Integration of various Facebook Social Plugins such as the like box in your web page has also been discussed. Further you will get to know the concept of virtual currency and how to programmatically derive Facebook analytics data. As the book progresses, you will learn to use and integrate many more advanced features in Facebook application development. The book contains ready to use code that can be deployed instantly. Towards the end, the book houses a variety of ready to use Facebook applications so as to help readers derive their own applications from them.</p>
Table of Contents (18 chapters)
Facebook Application Development with Graph API Cookbook
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Handling navigation in an iFrame Facebook application


Navigation for applications, which run inside Facebook, has to be handled differently. In a multi-page based application, which runs inside Facebook, when a user clicks on a hyperlink, it will be loaded inside the iFrame. Suppose you want to direct the user to an external URL, in this case, you won't want it to appear in the same iFrame. Thus, navigation becomes ineffective and has to be implemented efficiently.

Getting ready

You should have registered your application and created config.php.

How to do it...

Suppose we have two files, index.php and about.php. We want to create a hyperlink for about.php in index.php, as well create a hyperlink to an external site, say www.example.com. Simply follow these steps:

  1. Open config.php and add the following highlighted line to it:

    <?php
      require_once 'facebook.php';
      $facebook = new Facebook(array(
        'appId'      => 'your_application_id',
    	'secret'     => 'your_application_secret',
    	'cookie'     => true, 
      ));
      $appBaseUrl = 'http://apps.facebook.com/[your_app_canvas_name]';
    ?>
  2. Replace [your_app_canvas_name] accordingly and save the file.

  3. Now, add the following code to index.php:

    <?php
      require_once 'config.php';
    ?>
    <a href='<?php echo $appBaseUrl?>/about.php' target='_top'>About Us</a>
    <a href='http://www.example.com' target='_top'>External Link</a>
  4. Save index.php. Also, add some HTML code to about.php. Now run index.php.

How it works...

Suppose our domain name is http://www.example.com and we have uploaded index.php and about.php inside a directory named my_app. If we specify http://www.example.com/my_app/ as our Canvas URL in Facebook, then by default when we access the application base URL, index.php is rendered inside the iFrame. If we wish to open some other page of our application inside Facebook, we just need to add the name of the file after the base URL of our application. For example, to open about.php inside Facebook, we need to go to: http://apps.facebook.com/[your_app_canvas_name]/about.php. Facebook automatically does this mapping. Whenever you enter a filename after your application's base URL, Facebook searches for it inside the predefined directory and renders that file inside its iFrame.

Here we have defined our application's base URL in config.php and while forming hyperlinks, we just concatenate the filename after this base URL if the file is in the same directory as index.php, otherwise we can form links accordingly. We have also set the target attribute of the hyperlink as _top. This makes sure that when we click on it, the URL of iFrame's parent changes.

Now, when we click on About us, we will be directed to http://apps.facebook.com/[your_app_canvas_name]/about.php, whereas clicking on External Link will direct us to the external site www.example.com.