20,53 €
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.
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.
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:
Seitenzahl: 189
Veröffentlichungsjahr: 2008
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]>)
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
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.
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.
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.
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.
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:
When we wish to draw your attention to a particular part of a code block, the relevant lines or items will be made bold:
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".
Important notes appear in a box like this.
Tips and tricks appear like this.
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.
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.
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.
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.
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.
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:
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.
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.
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.
There are three elements to the Facebook Platform:
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.
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.
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:
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:
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:
