Tải bản đầy đủ (.pdf) (432 trang)

building applications to grow your facebook empire

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (8.85 MB, 432 trang )

Facebook Cookbook

Other resources from O’Reilly
Related titles
FBML Essentials
PHP Cookbook

JavaScript: The Good Parts
PHP Pocket Reference
oreilly.com
oreilly.com is more than a complete catalog of O’Reilly books.
You’ll also find links to news, events, articles, weblogs, sample
chapters, and code examples.
oreillynet.com is the essential portal for developers interested in
open and emerging technologies, including new platforms, pro-
gramming languages, and operating systems.
Conferences
O’Reilly Media brings diverse innovators together to nurture
the ideas that spark revolutionary industries. We specialize in
documenting the latest tools and systems, translating the inno-
vator’s knowledge into useful skills for those in the trenches.
Visit conferences.oreilly.com for our upcoming events.
Safari Bookshelf (safari.oreilly.com) is the premier online refer-
ence library for programmers and IT professionals. Conduct
searches across more than 1,000 books. Subscribers can zero in
on answers to time-critical questions in a matter of seconds.
Read the books on your Bookshelf from cover to cover or sim-
ply flip to the page you need. Try it today for free.
Facebook Cookbook



Jay Goldman
Beijing

Cambridge

Farnham

Köln

Sebastopol

Taipei

Tokyo
Facebook Cookbook™
by Jay Goldman
Copyright © 2009 Jason Goldman. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly
books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (). For more information, contact our corporate/
institutional sales department: 800-998-9938 or
Editor: Mary E. Treseler
Production Editor: Sarah Schneider
Copyeditor: Genevieve d’Entremont
Proofreader: Sarah Schneider
Indexer: Fred Brown
Cover Designer: Karen Montgomery

Interior Designer: David Futato
Illustrator: Jessamyn Read
Printing History:
October 2008:
First Edition.
O’Reilly and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Facebook Cookbook,
the image of a slow loris, and related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
To Bianca, whose face is tops in my book. I could
do nothing without you.

Table of Contents
Contributors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
1. Introducing Facebook Platform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 What Exactly Is Facebook Platform? 2
1.2 Skills to Pay the Bills 5
1.3 Facebook Platform Off-Facebook 7
1.4 Facebook Platform Versus Google OpenSocial 7
1.5 Saddle Up! 9
2. Ideation and Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1 Which Types of Apps Are the Most Popular? 12
2.2 Which Apps Are Most Popular? 13
2.3 Test-Driving Ideas with Facebook Polls 16
2.4 The Winning Formula for Facebook 19

2.5 Where’s the Money? 21
2.6 Google AdSense and Facebook 23
2.7 Funding Facebook Development 24
2.8 Facebook Platform Integration Points 25
2.9 Application Directory 26
2.10 Navigating the Applications Menu 31
2.11 Facebook Canvas Pages 33
2.12 Think Outside the Profile Box 34
2.13 Info Sections 36
2.14 News Feed and Mini-Feed 37
2.15 Noteworthy Notifications 43
2.16 Understanding Allocations 43
2.17 Attachments and the Publisher 45
2.18 Requests 47
2.19 Tabs 47
2.20 Guiding Principles of App Strategy 48
vii
2.21 Community Gardening 50
2.22 Finding Inspiration 51
3. Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
3.1 Installing the Facebook Developer App 55
3.2 Setting Up a New App 57
3.3 Hello World 66
3.4 Installing Your App: It’s All About the About Page 67
4. Architecture and Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
4.1 Under the Hood: How Facebook Apps Work 71
4.2 Architecting for the Future: Open Web Apps 73
4.3 Build Now, Scale Later: Getting Real 75
4.4 Scalability 77
4.5 Language Selection As Architecture 78

4.6 Cross-Language Development with Facebook Thrift 80
4.7 The Database Is Dead! Long Live memcached! 82
4.8 Advanced Caching with Nginx and memcached 83
4.9 Standing on the Shoulders of Giants: Hosting with Amazon
Web Services 84
4.10 Integrating Drupal and Facebook 85
4.11 App Design Process 86
4.12 The Facebook User Interface Widgets 91
4.13 Facebook’s Global User Interface 95
5. Setting Up Your Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
5.1 Creating a Test Account 99
5.2 Facebook Clients 101
5.3 JavaScript Client Library 101
5.4 Starting Out in PHP 103
5.5 Demo Applications 106
5.6 Developer Documentation 106
5.7 Test Consoles 107
5.8 Developer Forums: Help! 108
5.9 Facebook Bug Squashing 109
5.10 Facebook Developer Software Toolbox 109
5.11 Weekly Facebook Pushes 112
5.12 Hosting 112
5.13 Amazonian Backends: Simple Storage Solution 114
5.14 Amazonian Backends: Hosting on Elastic Cloud Computing 120
5.15 Staying Up-to-Date 123
viii | Table of Contents
6. Facebook Markup Language (FBML) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
6.1 What Is FBML? 125
6.2 Categories of FBML Tags 127
6.3 FBML Versions 128

6.4 A Rose by Any Other Name: Forced Renaming 129
6.5 Web Standards 130
6.6 Displaying a User’s Name 133
6.7 He Said, She Said: Dealing with Pronouns 135
6.8 Worth a Thousand Words: Profile Pictures 136
6.9 Linking to Events 137
6.10 Linking to Groups 138
6.11 Linking to Networks 138
6.12 Display Content to Group Members 138
6.13 Display Content to Network Members 139
6.14 Displaying Content to App Users 140
6.15 Displaying Content to Friends 140
6.16 Displaying/Hiding Content to/from Specific Users 141
6.17 Displaying Random Content 142
6.18 Displaying Content to Specific Browsers 143
6.19 Displaying Your Application’s Name 144
6.20 Formatting Relative Time 144
6.21 Making Content Visible to Some Users in Profile Boxes 145
6.22 Hiding Private Profile Content 147
6.23 Hiding Private Photos 148
6.24 Embedding an iFrame 149
6.25 Embedding Facebook Photos 150
6.26 Embedding MP3s 151
6.27 Embedding Adobe Flash 152
6.28 Embedding Flash Video 156
6.29 Embedding Microsoft Silverlight 157
6.30 Wide Versus Narrow Profile Boxes 158
6.31 Tabling Users 159
6.32 Page and Box Titles 160
6.33 Profile Box Subtitles 162

6.34 Forms the Facebook Way 163
6.35 Heads Up! Heading Your App Pages 169
6.36 Dashing Dashboards: Heading Your App Pages 170
6.37 Tabs Ahoy! 173
6.38 Media Headers 174
6.39 Errors, Explanation, and Success: Displaying Messages (Oh My!) 175
6.40 Discussion Boards Made Simple 177
6.41 Giving Users a Voice: Wall Posts in Your App 178
6.42 Adding Profile Boxes and Info Sections 181
Table of Contents | ix
6.43 Prompting for Extended Permissions 183
6.44 You Can Pick Your Friends 184
6.45 You Can Pick Your Friends (in Batches) 186
6.46 Invitations and Requests 189
6.47 Predicting the Future with Type-Ahead Controls 193
6.48 Using FBML Inside FBJS 194
6.49 Dialogs That Pop 196
6.50 Dialogs in Context 198
6.51 Dialogs with Ajax 198
6.52 Facebook Share Buttons 203
6.53 Feed Forms: Publishing Feed Stories from FBML 205
6.54 Redirecting to a Different URL 207
6.55 Painless Image Submitting 208
6.56 Hunting for Robots: CAPTCHA in Your App 208
6.57 FBML Caching 210
6.58 Analyzing Traffic with Google Analytics 211
6.59 Translations 213
6.60 Valid HTML Tags 218
6.61 Facebook and CSS: FBSS? 219
6.62 Beating the CSS, JavaScript, and Image Cache 221

7. Facebook JavaScript (FBJS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
7.1 Dealing with Sandbox Renaming 227
7.2 Losing Your < >s 230
7.3 Retrieving DOM Elements 230
7.4 Manipulating DOM Elements 231
7.5 Manipulating CSS Styles 233
7.6 Manipulating CSS Class Names 234
7.7 Dynamically Setting Content 237
7.8 Textbox Selections 238
7.9 Limiting the Length of Text Fields 239
7.10 Creating Elements Dynamically 240
7.11 Adding and Removing Event Listeners 241
7.12 Retrieving Data via Ajax 241
7.13 Displaying Pop-Up Dialogs 245
7.14 Displaying Contextual Dialogs 247
7.15 Avoiding Heartache When the DOM Changes 248
7.16 Linking to External FBJS Files 249
7.17 Use Firebug (No, Seriously) 250
7.18 Accessing the DOM Behind FBJS 252
7.19 Ajax Library: Backface 253
x | Table of Contents
8. Facebook Query Language (FQL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
8.1 Playing with FQL 256
8.2 FQL Query Structure 256
8.3 Functions and Operators 258
8.4 Indexed Facebook Tables and Fields 259
8.5 Advanced Relational Database Table Optimization 262
8.6 Album Table 264
8.7 Retrieving an Album 265
8.8 Counting All of a User’s Photos 266

8.9 Retrieving Five Albums for a User 266
8.10 Cookie Table 267
8.11 Retrieving All Cookies for a User 268
8.12 Retrieving a Specific Cookie 268
8.13 Retrieving All Cookies for a Specific Path 268
8.14 Metrics Table 269
8.15 Retrieving Yesterday’s Metrics 270
8.16 Retrieving Metrics for a Date Range 271
8.17 Alerting Yourself 272
8.18 Event Table 273
8.19 Event Member Table 274
8.20 Retrieving an Event 275
8.21 Retrieving Events Created by a User 275
8.22 Retrieving a User’s Events 276
8.23 Retrieving a User’s Events with a Specific RSVP 276
8.24 Retrieving Events Two Users Are Attending 276
8.25 Friend Table 277
8.26 Friend Request Table 277
8.27 Retrieving a User’s Friends 278
8.28 Checking Whether Two Users are Friends 279
8.29 Retrieving a User’s Pending Friend Requests 279
8.30 Checking for a Friend Request Between Two Users 279
8.31 Friend List Table 280
8.32 Friend List Members Table 280
8.33 Retrieving a User’s Friend Lists 281
8.34 Retrieving a Specific Friend List 281
8.35 Retrieving Friends in Friend Lists 282
8.36 Retrieving Friends in a Specific Friend List 282
8.37 Groups Table 282
8.38 Group Member Table 283

8.39 Retrieving a Specific Group 284
8.40 Retrieving a User’s Groups 285
8.41 Checking Whether Two Users Are in the Same Group 285
8.42 Listing Table 286
Table of Contents | xi
8.43 Retrieving a Listing 287
8.44 Retrieving a User’s Listings 288
8.45 Retrieving a User’s Friends’ Listings 288
8.46 Retrieving a User’s Listings by Category 288
8.47 Page Table 289
8.48 Page Fan Table 291
8.49 Retrieving a Page 292
8.50 Retrieving a User’s Pages 293
8.51 Photo Table 293
8.52 Photo Tag Table 294
8.53 Retrieving the 10 Most Recent Photos from a User 295
8.54 Retrieving All Photos a User Is Tagged In 295
8.55 User Table 296
8.56 App Friends 298
8.57 Birthday Friends 298
8.58 600 Errors 299
8.59 Preload FQL 300
9. Facebook API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
9.1 What’s an API? 301
9.2 Getting Started with the Client Library 304
9.3 RESTing with Facebook 304
9.4 Storable Data 305
9.5 Authenticating Users 306
9.6 Creating a Session Key 310
9.7 Creating an Infinite Session Key 311

9.8 Getting a Session (Desktop Only) 313
9.9 Creating an Auth Token (Desktop Only) 314
9.10 Making Calls (Desktop Only) 314
9.11 Logging Out 315
9.12 Getting Allocations 315
9.13 Getting Metrics 316
9.14 Getting and Setting Application Properties 318
9.15 Getting an App’s 4-1-1 320
9.16 Batching Calls 322
9.17 Getting and Setting Cookies 324
9.18 Getting Events 325
9.19 Getting Event Members 326
9.20 Refreshing FBML Caches 327
9.21 Getting a (Ref) Handle on FBML 328
9.22 Three Story Sizes: Working with Template Bundles 328
9.23 Publishing News and Mini-Feed Stories 332
9.24 Story Aggregation 333
xii | Table of Contents
9.25 Setting Info Sections 335
9.26 FQL Queries 337
9.27 Friends? 337
9.28 Get Friends 339
9.29 Get Friends Who Use My App 339
9.30 Get Friend Lists 340
9.31 Get Groups 341
9.32 Get Group Members 341
9.33 Creating/Modifying Marketplace Listings 342
9.34 Get Marketplace Listings 343
9.35 Get Marketplace Categories and Subcategories 344
9.36 Deleting Marketplace Listings 345

9.37 Searching the Marketplace 345
9.38 Sending Notifications 346
9.39 Get Notifications 348
9.40 Get Pages 348
9.41 Checking Page Properties 349
9.42 Create a Photo Album 350
9.43 Get Photo Albums 351
9.44 Get Photos 352
9.45 Uploading a Photo 353
9.46 Adding Tags to Photos 354
9.47 Getting and Setting Profile FBML 354
9.48 Get a User’s Info 357
9.49 Get Logged-In User 357
9.50 Has a User Added My App? 358
9.51 Setting Status 358
9.52 Extended Permissions 359
9.53 Checking Extended Permissions 360
9.54 Storing Data with the Data Store API 361
9.55 Granting Permissions to Other Applications Via the
Permissions API 364
9.56 Post-Remove (Uninstall) URL 366
9.57 Adding Missing PHP Client Library Methods 367
9.58 Error Codes 369
10. Marketing Your App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
10.1 Attracting Users Through Facebook Ads 371
10.2 Monetize, Measure, and Market with SocialMedia 375
10.3 Social Network Advertising with Cubics 377
10.4 Other Ad Networks 378
10.5 Spreading Your App via Google AdWords 378
10.6 Measuring Your Success 379

Table of Contents | xiii
10.7 Work the Integration Points 381
10.8 Continuous Improvement Through A/B Testing 381
10.9 The Great Apps Program 382
10.10 Application Verification Program 383
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
xiv | Table of Contents
Contributors
The following people contributed recipes to this book:
Jayant Agarwalla: Recipe 2.4, The Winning Formula for Facebook
There aren’t many people in the world who can claim to have a winning formula
for building Facebook applications. Jayant is one of those very, very few. I had the
pleasure of interviewing him on stage at the ICE08 conference, and I can honestly
say that I’ve never met anyone with a deeper understanding of what it means to
build a successful Facebook app. And he should know! At 21 years old, Jayant,
who comes from Kolkata, India, is the cofounder of and the
Scrabulous application on Facebook (now called Wordscraper). He’s currently the
VP of business development and marketing for Scrabulous.
Will Pate: Recipe 2.21, Community Gardening
When I set out to find someone who could write eloquently about some good
community gardening strategies, my list had exactly one person on it: Will Pate.
From starting the Infinity BBS when he was still in high school, through cofounding
Raincity Studios in Vancouver, to being the community manager for Flock, co-
hosting commandN, and in his current role as community manager for VenCorps,
Will has demonstrated an unparalleled understanding of how to grow a vibrant
community. You can find him at .
Alistair Morton: Recipe 2.22, Finding Inspiration
Al is one of the most talented designers I’ve ever met, and certainly the tallest.
We’ve collaborated on a few projects, which has given me the opportunity to notice
that even his on-the-phone doodles are individually perfect works of art. I asked

him to contribute a recipe that’s a little outside the regular scope you might have
come to expect, and to shine some light on where he finds inspiration for his cre-
ative endeavors. You can find him at .
Rajat Agarwalla: Recipe 4.4, Scalability
I’ve never met Rajat, but I have had the pleasure of sharing a stage with his brother,
Jayant. You might not recognize their personal names, but you’ve almost certainly
lost hours and hours of your life to their creation: Scrabulous. Few Facebook de-
velopers have dealt with the scaling issues that they have! The brothers hail from
Kolkata, India, and are cofounders of and the Scrabulous
xv
application on Facebook (now Wordscraper). Rajat is the CEO and chief software
architect.
Mark Slee: Recipe 4.6, Cross-Language Development with Thrift
I’ve only had the honor of meeting a handful of Facebook’s development team
members, which fortunately included Mark. I saw Mark present at the FSOSS07
conference on Thrift and immediately cornered him to contribute a recipe. He’s
one of the original authors of Thrift and is a product manager at Facebook. Prior
to that, Mark was a member of the engineering team, focused on systems infra-
structure, mobile applications, and general site development. He holds degrees in
computer science and mathematics from Stanford University, and spends the better
part of his spare time listening to and producing electronic music.
Ilya Grigorik: Recipe 4.8, Advanced Caching with Nginx and memcached
Every now and then, you run into a person who is so much smarter than you that
you’re really just dumbfounded. Ilya is one of those people. He’s the founder and
CTO of AideRSS (), an RSS filtering service designed to help
you find and read what matters. In his downtime, he maintains a popular blog
() where he talks about Ruby, Ruby on Rails, and best prac-
tices of scalable web architectures. The scope of his recipe is probably beyond all
but the most advanced readers, though I encourage you to consider it in your
architecture if you’re planning an app that really needs to scale.

James Walker: Recipe 4.10, Integrating Drupal and Facebook
Since some of you will be experienced PHP developers, there’s a good chance that
you’ve played around with Drupal or even built some sites on it. What you may
not know is that you can save yourself a lot of time and energy by building your
Facebook app on it! James, known to his loyal followers as Walkah, is a High Priest
in the religion of Drupal. He’s also a father, a geek, a drummer, a (former) hockey
goalie, a music nerd, a free software advocate, a beer drinker, a thinker, a Cancer,
a flirt, a closet singer, a dork, a hugger, a clown, and alive. James and I have shared
many conversations in which the answer ended up being “Drupal,” so I was thrilled
when the conversation about him contributing to this book started with “Drupal”
and ended with “Yes.” You can find him at and at http://lullabot
.com, where he’s the director of education.
Daniel Burka: Recipe 4.13, Facebook’s Global User Interface
Daniel is one of the friendliest people I’ve ever met. Maybe it’s because he’s a fellow
Canadian, but it’s always such a pleasure to run into each other and catch up. You
may not recognize his name, but you know his work: Daniel is a partner at
Silverorange and the design director at Digg, as well as a cofounder of Pownce. You
can find him at .
Jason DeFillippo: Recipe 5.4, Starting Out in PHP
Many of you will be new to the world of programming or won’t be overly familiar
with PHP. I asked my good friend Jason DeFillippo to contribute a recipe on a
simple but effective beginner PHP tip, which he happily did. Jason has been
xvi | Contributors
building websites professionally since 1994, working for companies such as Epson,
Paramount, Technorati, and 8020 Publishing. He specializes in social media and
blogging and is the cofounder and CTO of the Metblogs global network. Jason’s
blog can be found at , and his awesome photos can be ogled
at .
Martin Kuplens-Ewart: Recipe 6.5, Web Standards
Martin was a member of our team at Radiant Core and now consults on frontend

development for Zerofootprint. He’s a brilliant web designer and writes better
HTML than almost anyone I’ve ever met, which makes him perfectly qualified to
contribute a recipe about why web standards are important, even on Facebook.
Martin has helped major groups and brands, including Microsoft, Mozilla,
Toronto’s Hospital for Sick Children, UNESCO, YMCA, and Zerofootprint,
understand how to embrace web technologies and online community as part of
their core business, and he has developed online solutions for these and other major
organizations. He is an expert in the development of web applications using
standards-compliant methodologies and is a 10-time judge of the Web Marketing
Association WebAwards. Martin’s consulting services can be found at http://www
.apolitic.com.
Pete Forde with Rowan Hick: Recipe 8.5, Advanced Relational Database Table
Optimization
Pete Forde is a cofounder of Unspace Interactive, one of the world’s best Ruby on
Rails consulting firms. He’s also an amazing photographer, a charismatic leader of
the tech community in Toronto, founder of our Rails Pub Night, and a
co-organizer of the Ruby Fringe Conference. I asked Pete to contribute a recipe
about optimizing database performance and got more than I could have hoped for
when he dragged Rowan along for the ride. You can find Pete at pace
.ca or on Flickr at Rowan is at anhick
.com.
Alain Chesnais: Recipe 10.1, Attracting Users Through Facebook Ads
Alain is the vice president of product development for View 22, makers of the
SceneCaster application. Their Facebook application, a portal into the world of
SceneCaster, has quickly attracted over a million users. Alain has previously
worked at Alias|Wavefront, ATI, Tucows, and TrueSpectra, and has forgotten
more about the world of 3D than you or I will ever know.
Jeffrey Tseng: Recipe 10.6, Measuring Your Success
I was completely blown away when Albert Lai, Kontagent’s CEO and an old friend
of mine, showed me their demo. Albert’s a very successful serial entrepreneur and

has another great startup on his hands, cofounded with his CTO, Jeffrey Tseng.
Kontagent is focused on providing next-generation social analytics tools for de-
velopers, and Jeffrey is well-suited to his roll, having previously been the founder
of a startup that provided consulting services for wireless sensor networks. You
can find them at .
Contributors | xvii

Preface
One day, in the not too distant future, I fully expect my grandmother to ask me about
Facebook. She’s particularly hip, as grandmothers go, and is already all over email. She
even occasionally “surfs” the Web to read up on the latest events in her native South
Africa! You might not think she falls into Facebook’s target demographic, but I would
hate to be the person standing between her and her Mac if anyone told her that she
could learn even more about her beloved Toronto Maple Leafs by registering for a
Facebook account. She is, after all, their number-one fan.
There’s an important takeaway in there for everyone who has picked up this book in a
bookstore and is weighing the idea of building a Facebook empire: my grandmother,
and millions of people like her, are waiting for you to build the application that lures
them into the world’s fastest growing social network. Please don’t disappoint her,
because I’m her number one fan, and I have no objection to getting a little rough in the
corners, if you know what I mean.
Who Should Read This Book
The contents of this cookbook are primarily aimed at developers with a general back-
ground in web development who are interested in building Facebook web applications.
Although Facebook Desktop and Mobile apps are covered where applicable, the con-
tent in here is really aimed more at the web side of things. There’s a wide swath of
material covered, from how to plan an app, to really gritty API details and FQL calls,
to how to market and attract users, so there should be something for everyone.
Most cookbooks assume that the would-be chefs reading them have a basic knowledge
of how to cook, and this book is no different. I assume you know your way around the

following (even at a very fuzzy, somewhat-in-the-dark level): web development in the
areas of HTML, CSS, programming (particularly PHP), and SQL/database design. You
don’t need to be a master of any of them, and I’ve asked a few friends to contribute
some recipes to help you out if you’re just getting started (particularly Recipes 5.4 and
8.5, respectively). I’ve also pointed out some excellent books if you need to brush up
on some of the related topics.
xix
What’s in This Book?
Like all good cookbooks, this one is intended to be pulled off the shelf and rifled through
when you need to know how to embed an MP3 on a Canvas page (fb:mp3: see Rec-
ipe 6.26), how to look up friend lists using FQL (SELECT flid, name FROM friendlist
WHERE owner = $uid; see Recipe 8.33), or where to find inspiration when you’re faced
with App Developer block (all around you; see Recipe 2.22). You’re welcome to read
through it from cover to cover—and I hope you’ll find it entertaining and worth a few
laughs if you do—or to use it as a trusted reference while you take over the Platform
world.
This book is organized into 10 chapters:
Chapter 1, Introducing Facebook Platform
A general overview of Facebook, Facebook Platform, and an introduction to the
incredible opportunity it represents.
Chapter 2, Ideation and Strategy
If you don’t have an idea in mind already for an app, this is the chapter for you.
Learn about the Platform ecosystem, dig into the integration points and different
strategies for using them, and pick up a few techniques for doing app design quickly
and with the best possible results.
Chapter 3, Hello World
Time to get started building your first app! This quick chapter will walk you
through the classic Hello World first programming example.
Chapter 4, Architecture and Design
This chapter covers the best architectures for Facebook apps, some solid recom-

mendations for database performance, and an overview of the design and user
experience of winning applications.
Chapter 5, Setting Up Your Environment
Learn about all the things you need to download, how to add apps to Facebook,
the secret trick to setting up a test account, and how to get the lowdown on the
latest and greatest from Facebook.
Chapter 6, Facebook Markup Language (FBML)
FBML is the magic that makes the Facebook Platform world go ’round. We’ll cover
all of the tags, dig into some surprising behaviors you might encounter, and explore
some great tricks for building better frontends.
Chapter 7, Facebook JavaScript (FBJS)
If JavaScript is the duct tape that binds the Web together, FBJS is the glue that
makes Facebook apps stick (or something like that). This chapter explains why
you can’t just use regular JavaScript in your app, how to build great Ajax-like
interactions using Facebook’s Mock Ajax techniques, and goes into detail about
all of the handy functions available to you.
xx | Preface
Chapter 8, Facebook Query Language (FQL)
As FBML is to HTML, FQL is to SQL. We’ll take a look at the schema of the various
database tables you have access to, and catalog some really useful (and fast) FQL
queries you can use in your apps.
Chapter 9, Facebook API
Ah, sweet, sweet API. We would be nothing without you! This chapter will show
you the real power behind Platform, digging deep into the code that connects
everything together. We’ll go through each of the objects and methods you have
at your disposal, and I’ll give you some tips and tricks for desktop apps along the
way.
Chapter 10, Marketing Your App
Remember: if you build it, they won’t come—without persuasion. Marketing is
the art of persuasion, and this chapter goes over some general marketing options

for Facebook apps and some great techniques for measuring your success.
Code Samples
Many of the PHP code samples in this book require the Facebook API to be included
and instantiated before the sample can be run. Those precious few lines of code have
been omitted throughout to save space, so stick these in when you need them:
<?php
include_once 'resources/includes/config.php';
include_once 'resources/includes/facebook.php';
?>
<?php
global $api_key, $secret;
// Code Goes Here
?>
You’ll need to adjust the paths in the include_once lines to match where you’ve put the
files after downloading them. See Chapter 5 for more information on setting up your
environment.
Although there’s a lot of code in here that you could lift straight off these pages and
drop into your app, remember that none of this has been extensively tested as produc-
tion code and there’s very little error checking. Since Facebook throws exceptions when
things blow up, I’d suggest at least wrapping your code in some try/catch statements
and doing something useful with your contained explosions.
Preface | xxi
Keeping Up with the Facebookers
As I was getting to the end of writing this book (mid-2008), Facebook announced that
they were planning a major redesign of their Profiles and the way that applications
integrate with them. That decision sure made for exciting times in these parts!
Luckily, the timing worked out nearly perfectly, and this book now contains informa-
tion about the “Profile redesign” rather than the old way of doing things. The new
design was still in the initial phases of being rolled out as we were wrapping up the
manuscript, so some of the screenshots still show the old design we knew and loved,

rather than the new design we’re fumbling around with but are pretty sure is going to
become our number-one squeeze. Fear not: the screenshots were updated anywhere
that it was absolutely required. It’s also possible that some of the information changed
after we went to press, so check the Developers Wiki if you think that might be the case
( />It’s also worth noting that some of the applications profiled in this book are no longer,
and that some have completely changed their look, feel, and very purpose in life. Scra-
bulous, for example, is consistently used throughout as a paragon of what to do right,
but it has actually fallen prey to its legal battles and has been reborn, phoenix-like, as
Wordscraper ( />Using Code Examples
This book is here to help you get your job done. In general, you may use the code in
this book in your programs and documentation. You don’t need to contact us for
permission unless you’re reproducing a significant portion of the code. For example,
writing a program that uses several chunks of code from this book doesn’t require
permission. Selling or distributing a CD-ROM of examples from O’Reilly books does
require permission. Answering a question by citing this book and quoting example
code doesn’t require permission. Incorporating a significant amount of example code
from this book into your product’s documentation does require permission.
We appreciate, but don’t require, attribution. An attribution usually includes the title,
author, publisher, and ISBN. For example: “Facebook Cookbook by Jay Goldman.
Copyright © 2009 Jay Goldman, 978-0-596-51817-2.”
If you feel your use of code examples falls outside fair use or the permission given above,
feel free to contact us at
Safari® Books Online
When you see a Safari® Books Online icon on the cover of your favorite
technology book, that means the book is available online through the
O’Reilly Network Safari Bookshelf.
xxii | Preface
Safari offers a solution that’s better than e-books. It’s a virtual library that lets you easily
search thousands of top tech books, cut and paste code samples, download chapters,
and find quick answers when you need the most accurate, current information. Try it

for free at .
Comments and Questions
Please address comments and questions concerning this book to the publisher:
O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)
We have a web page for this book, where we list errata, examples, or any additional
information. You can access this page at:
/>To comment or ask technical questions about this book, send email to:

For more information about our books, conferences, Resource Centers, and the
O’Reilly Network, see our web site at:

Thanks!
Writing this book was hard work, so I’m lucky to have been surrounded by so many
great contributors who made gathering recipes so easy. To my gang of guest chefs: they
say that too many cooks spoil the batter, but obviously they were never backed by a
team like you. Your expertise and insight have made this book what it is. In the order
they appear in the book:
• Jayant Agarwalla
• Will Pate
• Alistair Morton
• Rajat Agarwalla
• Mark Slee
• Ilya Grigorik
• James Walker
• Daniel Burka

Preface | xxiii

×