Learning Facebook Application Development - Bain Dr Mark Alexander - E-Book

Learning Facebook Application Development E-Book

Bain Dr Mark Alexander

0,0
20,53 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

In Detail

Using Facebook Applications, developers can add custom features to one of the most popular websites in the world. Facebook is the biggest social network among college students, and is gaining ground among professionals too. Facebook applications enable you to add new ways for users to interact with each other using Facebook.

Facebook applications are written in a server-side language, and hosted on the application developer's own server. They interface with Facebook, so that they appear to users to be part of Facebook itself.

This book shows PHP developers how to quickly get started building powerful Facebook applications, how to work with data stored in Facebook, including photos, and how to handle multimedia and other custom data within Facebook applications.

The book covers how to send notifications and invitations from within an application, update a user's profile, create application control panels for users, and much more.

A complete guide to creating Facebook applications using PHP, this book focuses on realistic, complete examples-building common features step-by-step, with plenty of practical advice.

Approach

This book builds on your knowledge as a PHP developer to provide a fast-paced, step-by-step tutorial in building Facebook applications. The book is packed with example code and demo applications, so that you can see techniques in action. Along the way we provide quick references to the most important aspects of the API, so you can refer back as you develop your own applications with the book.

Who this book is for

This book is for PHP5 developers who want to create custom Facebook applications. It assumes no knowledge of the Facebook API or Platform, but does assume that you are a Facebook user.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 189

Veröffentlichungsjahr: 2008

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Learning Facebook Application Development
Credits
About the Authors
About the Reviewers
Preface
What This Book Covers
Conventions
Reader Feedback
Customer Support
Downloading the Example Code for the Book
Errata
Questions
1. Getting to Grips with the Facebook Platform
The Purpose of the Facebook Platform
The Standard Web Application Model
The Facebook Web Application Model
The Facebook Platform Elements
The Facebook API
The Facebook API Test Console
FBML
The FBML Test Console
FQL
Obtaining the Facebook Client Libraries
Adding the Developer Application
Setting Up Your Application
Required Fields
Optional Fields
Base Options
Installation Options
Integration Points
Summary
2. Building a Facebook Application
A Simple Facebook Application
Getting the Server Ready for Action
Creating Your First Facebook Application
The Application Initiation Code
The Application Code
Viewing the New Application
Developing the Simple Application
Examining Variables
Using FQL in Your Application
Writing to the Facebook Profile
Updating the Facebook Cache
Mock AJAX and Your Facebook Profile
Using a Form on Your Profile
Creating a Dynamic Combo-Box
Limiting Access to the Form
Storing Data—Keeping Files on Your Server
Storing FBML on Your Server
Storing Data on Your Server
Summary
3. Databases, Dashboards, and the Profile
Setting up the Database
Setting the Root Password
Creating the Database
Adding a User
A Table for Our Data
Migrating Data
Accessing the Database from Your Facebook Application
Connecting to the Database
Using a Remote Database
Running Queries on the Database
Obtaining Data from the Database
Inserting Data into the Database
Deleting Data from the Database
Analyzing the Database
Calculating the Number Items in a Table
Grouping and Ordering Data
Updating Everyone's Profile
Updating the Facebook Cache—Remotely
Obtaining the Application Session ID
Using the Session ID to Write to a Profile
Updating More Than One User's Profile
Automating the Facebook Cache Update
A Simple Batch File
Using At Command to Control the Batch File
Using Cron to Control the Batch File
Pros and Cons of the Methods
Tracking Users
A Table for Your Users
Finding Your Application ID
Adding New Users
Removing a User
Defining a Post-Remove URL
Producing a List of Users
The Facebook Dashboard
Adding a Dashboard
Adding Help to the Dashboard
Adding an Action to the Dashboard
The Dashboard Create Button
Facebook JavaScript
The Facebook Contextual Dialog
Summary
4. Cooking User Data
How to Fetch User Data
FBML
fb:name
fb:pronoun
fb:profile-pic
fb:user
Logical Tags
fb:is-in-network
fb:if-can-see
fb:if-can-see-photo
fb: if-is-friends-with-viewer
fb:is-if-group-member
fb:is-own-profile
fb:if-is-user
fb:if-user-has-added-app
fb:if-is-app-user
FQL
Let's Have a Look at the Schema
JOIN as Sub Query
Effective Searching with Structures
Common Snippets
Find All the friends of a User
Find All Friends Who Have the Current Application Installed
Find All Friends Whose Birthday Is Tomorrow
Find Which Groups a Specific User Is Subscribed To
Find All the Photos Uploaded by the User and in Any Album
Functions You can Use in FQL
Using Facebook API to Retrieve Information
Retrieving Profile Information
Retrieving List of Friends
Finding Out If Some Users are Friends
Retrieving Group Member's Information
Retrieving a User's Photo
Facebook Developers Tool
Summary
5. FBJS—The JavaScript
How FBJS Works
DOM Manipulation Using FBJS
DRY Means Don't Repeat Yourself
Managing Stylesheet with FBJS
Creating a Drop-Down Menu in FBJS
Dialog
Displaying Information
Accepting User Input
Contextual or Pop Up?
Advanced Input System Using Dialogs
Events
AJAX
Common Tasks
Posting Parameters to Server Side Code
Posting Multibyte Characters
Canceling an AJAX Request
Making Sure that Users have to Log In for the AJAX Request
Summary
6. Feeds
What Are Feeds?
How to Publish Feeds
Sample Application to Play with Feeds
So How Did it Work?
Publishing News Feed
Difference between These Two Methods
feed_publishTemplatizedAction
Summary
7. Invitations and Notifications
Invitations
fb:request-form
fb:req-choice
fb:request-form-submit
fb:multi-friend-selector
fb:multi-friend-input
fb:application-name
Notifications
notifications_send
notifications_sendEmail
Tips for Effective Notification and Emailing
Summary
8. Photos
Photo API
photos_getAlbum
Accessing from an External Application
Uploading Photo
Tagging Your Photos
Slideshow Application
Summary
9. Tools and Multimedia
Creating a Discussion Board
Taking Comments from Users
Making a Wall Using fb:wall
Increasing Usability Using fb:switch
Using fb:random to Display a Random Option
Increasing Usability Using fb:switch
Sharing Content
Audio Sharing
Video Sharing
Multimedia Contents
Displaying Photo
Managing Audio
Managing Video (FLV Format)
Summary
Index

Learning Facebook Application Development

Hasin Hayder

Dr Mark Alexander Bain

Learning Facebook Application Development

Copyright © 2008 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: May 2008

Production Reference: 1200508

Published by Packt Publishing Ltd.

32 Lincoln Road

Olton

Birmingham, B27 6PA, UK.

ISBN 978-1-847193-69-8

www.packtpub.com

Cover Image by Nilesh Mohite (<[email protected]>)

Credits

Authors

Hasin Hayder

Dr. Mark Alexander Bain

Reviewers

Junal Rahman

Md. Nur Hossain

Senior Acquisition Editor

David Barnes

Development Editor

Nikhil Bangera

Technical Editor

Ajay Shanker

Editorial Team Leader

Mithil Kulkarni

Project Manager

Abhijeet Deobhakta

Indexer

Hamangini Bari

Proofreader

Camille Guy

Production Coordinator

Aparna Bhagat

Cover Work

Aparna Bhagat

About the Authors

Hasin Hayder, graduated in Civil Engineering from the Rajshahi University of Engineering and Technology (RUET) in Bangladesh. He is a Zend-certified Engineer and expert in developing localized applications. He is currently working as a Technical Director in Trippert Labs and managing the local branch in Bangladesh. Beside his full time job, Hasin writes his blog at http://hasin.wordpress.com, writes article in different websites and maintains his open-source framework Orchid at http://orchid.phpxperts.net. Hasin lives in Bangladesh with his wife Ayesha and his son, Afif.

Thanks goes to my colleagues at TrippertLabs and phpExperts, for supporting me during this time. Thanks to Abhijeet, Nikhil, and David for their patience while I was late. And defi nitely, thanks to my family members, especially Ayesha and Afi f for their support.

Dr. Mark Alexander Bain first started customizing CRM systems back in the mid '90s when he was team leader for Vodafone's Cascade project—the team took the 'out-of-the-box' Clarify CRM and turned it into a radio base station planning application, complete with a workflow engine for passing jobs between the different departments involved in the planning, building, and implementation of a radio network. Since then, he's lectured at the University of Central Lancashire. Currently Mark writes articles on all things Linux and Open Source for Linux Format, Newsforge.com and Linux Journal. SugarCRM customization, therefore, seems the obvious choice for this, his second book, since it combines Mark's knowledge of working with commercial CRMs and the Open Source philosophy. Mark works from his home on the edge of the Lake District in the UK, where he lives with his wife, two dogs and two cats, and gets the odd visit from his sons—Michael and Simon.

About the Reviewers

Junal Rahman is a Computer Science graduate from Independent University of Bangladesh. His areas of expertise include programming with .Net and PHP framework. He has worked for several software companies as a web application developer. During his undergraduate studies, Junal completed his internship program at Alliance Creation where he worked on several projects including a garments inventory system for Opti Garments. Following his graduation, he became a full-time employee at Alliance Creation and continued working with the PHP framework. Later, he moved on to work for Systech Digital where he became a PHP team leader, managing and supervising projects for Systech Digital clients. As an aside, during his time at Alliance Creation and Systech Digital, Junal worked for a freelancing company called Allomatch as a freelance developer, where he gained experience in Smarty, a unique programming language. Finally, he joined Trippert Labs, where he now works as a Development Engineer. At Trippert, Junal works collaboratively to create Facebook applications. Apart from keeping up with the ever changing field of information technology, he spends much of his private life pursing his interests in screenplay and script writing. In the future, Junal hopes to create films and short dramas, and eventually establish his very own software company.

I would like to thank Hasin Hayder, my mentor, for writing this amazing book and dedicate my effort to my maternal uncle Shafi qul Karim for his eternal inspiration and continual encouragement.

Md. Nur Hossain is a B.Sc graduate from Shahjalal University of Science & Technology specializing in Computer Science & Engineering. For the past four years, he has been working in the field of web development and performance tuning. His area of interest is design pattern and efficiency of large DB schema. He is currently working as a senior developer at Trippert Labs.

I am thankful to my parents who are always encouraging. Thank you Abhijeet, for managing the resources and communications so smoothly. And thanks Hasin, and all my colleagues for sharing your tremendous knowledge with me.

To you, the mastermind behind each and every Facebook application.

Preface

Using Facebook applications, developers can add custom features to one of the most popular websites in the world. Facebook is the biggest social network among college students, and is gaining ground among professionals too. Facebook applications enable you to add new ways for users to interact with each other using Facebook.

Facebook applications are written in a server-side language, and hosted on the application developer's own server. They interface with Facebook, so that they appear to users to be part of Facebook itself.

This book shows PHP developers how to quickly get started building powerful Facebook applications, how to work with data stored in Facebook, including photos, and how to handle multimedia and other custom data within Facebook applications.

The book covers how to send notifications and invitations from within an application, update a user's profile, create application control panels for users, and much more.

What This Book Covers

Chapter 1 takes a look at the elements used by your application to interface with Facebook—the Facebook API, FBML, and FQL—that is the Facebook Platform.

Chapter 2 will cover how to extract the Facebook information to be displayed by your application, store information in the Facebook cache so that it can be displayed on your users' Profile pages, and store your own custom data.

Chapter 3 will cover how to manipulate data stored in a database, update your users' profiles automatically, track the users who are using your application, and use the Facebook Dashboard.

Chapter 4 will help you understand Facebook tags to display information in a hassle free and easier way. You will also get an introduction to Facebook Query Language (FQL), and understand why it is sometimes more effective to use FQL rather than the Facebook REST API.

Chapter 5 will see the scripting ability in Facebook, using a subset of standard JavaScript functions developed by Facebook, which is called FBJS.

Chapter 6 will go into the details of managing feeds (both news feed and mini feed) using Facebook REST APIs.

Chapter 7 will focus on creating a successful invitation system, creating a successful notification system, using notifications efficiently, and sending emails.

Chapter 8 will show you how to do the following: creating a photo importer from Facebook, creating a slide show application, and letting viewers upload photos to your album.

Chapter 9 will cover the usage of various tags in Facebook and create an interesting project using them.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

There are three styles for code. Code words in text are shown as follows: "We can include other contexts through the use of the include directive."

A block of code will be set as follows:

canpost="true" candelete="false" canmark="true" cancreatetopic="true"

When we wish to draw your attention to a particular part of a code block, the relevant lines or items will be made bold:

index.php <? include_once("prepend.php"); //the Lib and key container ?>

New terms and important words are introduced in a bold-type font. Words that you see on the screen, in menus or dialog boxes for example, appear in our text like this: "clicking the Next button moves you to the next screen".

Note

Important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader Feedback

Feedback from our readers is always welcome. Let us know what you think about this book, what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply drop an email to <[email protected]>, making sure to mention the book title in the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or email <[email protected]>.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer Support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the Example Code for the Book

Visit http://www.packtpub.com/files/code/3698_Code.zip to directly downlad the example code.

The downloadable files contain instructions on how to use them.

Errata

Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing this you can save other readers from frustration, and help to improve subsequent versions of this book. If you find any errata, report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the Submit Errata link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata are added to the list of existing errata. The existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Questions

You can contact us at <[email protected]>if you are having a problem with some aspect of the book, and we will do our best to address it.

Chapter 1. Getting to Grips with the Facebook Platform

If you've read even this far, then it's safe for me to assume that you're already well acquainted with Facebook, and (given the name of this book) it's also fairly safe to assume that you don't want an in-depth discussion on how to use Facebook—you just want to get on with building a Facebook application. So, that's exactly what we're going to do—we're going to build Pygoscelis P. Ellsworthy's Suspect Tracker as an example:

However, before we jump into building a Facebook application, we need to spend some time looking at the Facebook platform, and by the end of this chapter you will:

Understand what the Facebook Platform is, and how it relates to your applicationKnow about the elements that make up the Facebook Platform, and how to test them without having to create an applicationKnow how to set up the Facebook Platform, ready for your new application

The Purpose of the Facebook Platform

As you develop your Facebook applications, you'll find that the Facebook Platform is essential—in fact you won't really be able to do anything without it. So what does it do? Well, before answering that, let's look at a typical web-based application.

The Standard Web Application Model

If you've ever designed and built a web application before, then you'd have done it in a fairly standard way. Your application and any associated data would have been placed on a web server, and then your application users will access it from their web browsers via the Internet:

The Facebook model is slightly different.

The Facebook Web Application Model

As far as your application users are concerned, they will just access Facebook.com and your application, by using a web browser and the Internet. But, that's not where the application lives—it's actually on your own server:

Once you've looked at the Facebook web application model and realized that your application actually resides on your own server, it becomes obvious what the purpose of the Facebook Platform is—to provide an interface between your application and itself.

There is an important matter to be considered here. If the application actually resides on your server, and your application becomes very successful (according to Facebook there are currently 25 million active users), then will your server be able to able to cope with that number of hits?

Don't be too alarmed. This doesn't mean that your server will be accessed every time someone looks at his or her profile. Facebook employs a cache to stop that happening:

Of course, at this stage, you're probably more concerned with just getting the application working—so let's continue looking at the Platform, but just bear that point in mind.

The Facebook Platform Elements

There are three elements to the Facebook Platform:

The Facebook API (Application Programming Interface)FBML—Facebook Markup LanguageFQL—Facebook Query Language

We'll now spend some time with each of these elements, and you'll see how you can use them individually, and in conjunction to make powerful yet simple applications. The great thing is that if you haven't got your web server set up yet, don't worry, because Facebook supplies you with all of the tools that you would need in order to do a test run with each of the elements.

The Facebook API

If you've already done some programming, then you'll probably know what an API (or Application Programming Interface) is. It's a set of software libraries that enable you to work with an application (in this case, Facebook) without knowing anything about its internal workings. All you have to do is obtain the libraries, and start making use of them in your own application.

Now, before you start downloading files, you can actually learn more about their functionality by making use of the Facebook API Test Console.

The Facebook API Test Console

If you want to make use of the Facebook Test Console, you'll first need to access the Facebook developers' section—you'll find a link to this at the bottom of every Facebook page:

Alternatively, you can use the URL http://developers.facebook.com to go there directly in your browser. When you get to this page, you'll find a link to the Tools page:

Or, again, you can go directly to http://developers.facebook.com/tools.php, where you'll find the API Test Console:

You'll find that the API Test Console has a number of fields:

User ID—A read-only field which (when you're logged on to Facebook) unsurprisingly displays your user ID number.Response Format— With this, you can select the type of response that you want, and this can be:
XMLJSONFacebook PHP Client
Callback—If you are using XML or JSON, then you can encapsulate the response in a function.Method—The actual Facebook method that you want to test.

Once you've logged in, you'll see that your User ID is displayed, and that all the drop-downs are enabled:

You will also notice that a new link, documentation, appears on the screen, which is very useful. All you have to do is to select a method from the drop-down list, and then click on documentation. Once you've done that you'll see:

A description of the methodThe parameters used by the methodAn example return XMLA description of the expected response.The FQL equivalent (we will discuss this later in the chapter.)Error codes

For now, let's just change the Response Format to Facebook PHP Client, and then click on Call Method to see what happens:

In this case, you can see that the method returns an array of user ids—each one being the ID of one of the friends of the currently logged in user (that is your list of friends because you're the person logged in). You could, of course, go on to use this array in PHP as part of your application, but don't worry about that at the moment. We'll see how to use the methods in a real application, in Chapter 2. For the time being, we'll just concentrate on working with our prototyping in the test console.

However, before we move on, it's worth noting that you can obtain an array of friends only for the currently logged in user. You can't obtain the list of friends for any other user. So, for example, you would not be able to use friends.get on id 286601116 or 705175505. In fact, you wouldn't be able to use friends.get for 614902533 (as shown in the example) because that's my ID and not yours.

On the other hand, having obtained a list of valid IDs we can now do something more interesting with them. For example, we can use the users.getinfo method to obtain the first name and birthday for particular users: