Preface

One reason Twitter resonates with me is its simplicity. I’ve blogged in some form or another since 2000, when my first son was born. It takes a lot of time and thought to compose even a few paragraphs of meaningful text for a blog. You can add pictures and video, fiddle with the formatting, and reference many other sites with hyperlinks. It is an incredibly useful practice, but rarely does blogging fit into one of those natural moments between tasks. Twitter, on the other hand, won’t let you contribute more than a few thoughts or a link or two with each post, and only then if it fits into the 140-character limit. There is no formatting or multimedia embedding; it is just a simple act of thinking, sharing, responding, or emoting.

Since Twitter’s award-winning appearance at the South By Southwest (SXSW) conference in 2007, many have called for it to improve on the simple things it does. Why aren’t there groups? Can we make our posts longer? Will pictures show up in the timeline? How can I manage my private messages to other users? Although Twitter has on occasion responded to collective behavior or demand by implementing a new wrinkle (as with @username replies), the service has largely remained as it began: simple.

It is a credit to Twitter that it has resisted such changes. Making the service less simple would also make it less versatile. The void of unanswered user requests for functionality is filled by an ecosystem of third-party developers. The incentive for the innovation and resources these developers bring to the Twitter community would be critically lowered if the main service tried to do too much. A simple Twitter is better not only for the users trying to post their status updates, but also for the third-party applications trying to find their niches.

The purpose of Twitter API: Up and Running is to provide an introduction to using the Twitter API—the means to get at the rich Twitter data—to build web applications. This book has three main parts: an overview of the Twitter ecosystem and culture; background information on the languages and environment you need to create your applications; and working code for a suite of sample applications meant to get you started on your programming adventure. Novice readers should be able to gather working knowledge from the PHP scripts used to create the sample applications and see how the syntax works in context. Experienced readers will likely benefit from the references for the API methods as well as discussions about the context into which your applications will be placed.

As Twitter lowers barriers to publication through its simplicity, so this book will provide easy access to the skills and resources you’ll need to build web applications for its API.

Who This Book Is For

The cultivation of open API development represents another level of evolution in Internet participation. We aren’t just reading and writing content; we’re also cocreating the interactions surrounding that content. Twitter, in particular, has a low barrier for both. The most important property of the Twitter API is not found in the nuances of its syntax, but rather in the imaginative and prolific cocreation it inspires.

This groundbreaking book is for Twitter fans who want to do more than just answer the question, “What are you doing?” In this first book about working with the Twitter API, new and casual programmers are provided with explanations of how each part of the API functions and examples of how those parts can be assembled into web applications. We’ll also look closely at the culture of Twitter and how it has inspired programmers to build their own tools and games.

A prerequisite for this book is a basic understanding of how applications are built and hosted on the Web. However, you don’t need to be a professional coder to launch a Twitter web application successfully. The XHTML, CSS, PHP, and MySQL code necessary to the construction of the example applications will be provided and explained, as will some suggested criteria for securing a website. You should be able to pick up this book, follow the sample code, and have at your disposal a working application to use and modify.

Note

The sample code can be downloaded from this book’s website. It is open and available for anyone to use.

Among the wide range of readers of this book will be IT professionals in small organizations and Twitter members looking for a programming project. In the former scenario, an IT professional may be looking at Twitter as a potential platform to integrate existing services or products provided by his employer. He can use this book to survey some web tools that might serve as a foundation for a larger web application. In this context, it becomes a project companion with additional long-term value as a reference and directory of sample applications.

In the latter scenario, an active member of the Twitter community may have grown tired of waiting for someone else to provide missing functionality and be thinking about adding it herself. She might read this book first to see what is out there, in case someone has already built the desired tool, and then try to code the web application herself. She may not consider herself a programmer, but she can build off of the sample code and learn details by referencing the chapters on PHP and MySQL functions, selecting the sample application closest to what she has in mind and then making changes to add the desired behavior.

Twitter is a hot topic, but not much has been written about it yet. Therefore, the information this book contains on the history of the Twitter culture will also make it attractive to nonprogrammers who want to understand the phenomenon, such as decision makers for company development teams or active Internet users new to Twitter.

How This Book Is Organized

This book introduces the Twitter API in the context of a greater community culture, offering a suite of sample applications to help illustrate some key programming concepts. Here’s a synopsis of what you’ll find:

Chapter 1, Hello Twitter

Gives you a comprehensive overview of the Twitter culture, including the history of microblogging, the Fail Whale, the company business model, the API developer community, and creative uses of Twitter.

Chapter 2, Twitter Applications

Reviews more than two dozen existing third-party Twitter web applications you can use as inspiration for your own creations. The applications are grouped into seven tools categories—Publishing, Information Stream, Appropriation, Search, Aggregation, Statistics, and Follow Network tools—and each app is profiled with a screenshot and a description of what it does.

Chapter 3, Web Programming Basics

Provides a comprehensive starter kit for XHTML, CSS, PHP, and MySQL. This chapter is meant to be a primer for new programmers and a convenient reference for more experienced programmers. It also offers some advice on what to look for when searching for a web host to care for your new application.

Chapter 4, Meet the Twitter API

Gives the details on how to make requests of the Twitter API. Included in the general explanation are format differences, HTTP methods and error codes, authentication, and rate limits. This chapter contains a directory of all of the parameters used by the API and a description of each of the 40 methods, grouped into seven categories: Publishing, Information Stream, Follow Network, Communication, Member Account, API Administration, and Search. It also includes a discussion about security issues involving Basic Auth and a brief description of how to use cURL to test the API.

Chapter 5, Meet the Output

Takes a look at what comes out of the API as a response from Twitter. The various types of XML objects you will encounter—user, status, message, search, ID, response, and hash—are detailed with example output, explanations of the included XML elements, and a list of methods that return that object.

Chapter 6, Application Setup

Discusses the things you need to do to get your web environment ready, including creating a master Twitter account, making your MySQL database tables, creating your stylesheet, and uploading custom functions to a directory outside the web path. Each of the custom functions used in the sample applications is discussed in detail, with a description of what it does and PHP code provided as examples.

Chapter 7, Sample Applications

Describes the web interfaces from the suite of sample applications. For each of the seven applications, I’ll run you through how to use it and what it does, and then we’ll look closely at the code. Included are suggestions for how to make this starter code better.

Chapter 8, Automated Tasks

Describes the code for the programs from the suite of sample applications that run in the background. It includes a brief explanation of what each of the five scripts does and how the PHP code works.

Appendix A

Provides a bare-bones look at the Twitter API, listing the method path, whether it requires authentication, if it is charged against your rate limit, the HTTP method type, and any required and optional parameters.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Used for emphasis, technical terms where they are defined, URLs, email addresses, filenames, file extensions, and pathnames.

Constant width

Used for code samples, SQL statements, HTML and XML elements, methods, functions, variables and attributes and their values, objects, and class names.

Constant width italic

Used for user-replaceable items in code.

Constant width bold

Used for emphasis in code samples.

Note

This icon signifies a tip, suggestion, or general note.

Warning

This icon indicates a warning or caution.

Using Code Examples

You may use the code in this book in your programs and documentation. You do not 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 does not require permission. Selling or distributing a CD-ROM of examples from this book does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Twitter API: Up and Running, by Kevin Makice. Copyright 2009 Kevin Makice, 978-0-596-15461-5.”

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

Note

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.

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 http://my.safaribooksonline.com/.

We’d Like to Hear from You

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, and any additional information. You can access this page at:

http://www.oreilly.com/catalog/9780596154615/

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 website at:

http://www.oreilly.com

Acknowledgments

It has always been a dream to write a book. While for a long time I thought it would be about time travel or dragons, I’m delighted that my dream was fulfilled under the banner of O’Reilly Media. For that, I have a number of people to thank.

This book is dedicated to my family—Amy (@amakice), Carter (@cmakice), Archie, and the TBD baby we were creating during the nine months it took to write this tome—who went out of their way to give me time and space to type, type, type. By now, with me five years into an older-student Ph.D., they are used to helping me get my 3–5 hours of sleep each night, while keeping me fed and entertained. However, writing a book on top of graduate school is like adding a couple more dissertations to the workload. It takes a village to write a tech book. As they supported me, my family received support from Amy’s and my parents—Susan Clendening (@twobigdogs); Roger (@rjisb) and Jean Isbister; Gary and Carol Clendening; Joy and Pete Kottra—and our friends. I am particularly grateful for the supplemental financial support from that group and from my long-time friend, Tim Roessler, who can now take this dedication as a direct request to sign up for Twitter.

From the O’Reilly camp, Laurel Ruma (@laurelatoreilly) ran point on this project. Despite my being intimidated by both the brand and the endeavor, she held my virtual hand throughout the process and gently kept me on task. That this book arrived on bookshelves near you is a credit to her stewardship that made this project such a wonderful experience for me. I wouldn’t have met her at all if it weren’t for Jeffrey Bardzell (@jeffreybardzell), my professor and friend, who referred me to his agent Carole Jelen McClendon at Waterside Productions. Carole, now my agent, helped me pitch an idea for a Twitter book to John Osborn (@johnatlarge) and Laurel. Nine months later I was holding my first published book. From top to bottom, the O’Reilly Media organization was amazing. Rachel Head, Sarah Schneider, Marlowe Shaeffer, and Rachel Monaghan were also key to completing this project, and I thank them all for their professionalism and patience.

I’d also like to thank the great early tech reviewers that O’Reilly assembled to help improve the content in the book: Alex Payne (@al3x), Ed Finkler (@funkatron), Eric Stern (@Firehed), Cameron Kaiser (@doctorlinguist), Bill Ryan (@wgryan), Lisa Hoover (@lisah), Abraham Williams (@poseurtech), Dave Troy (@davetroy), Jeff Clark (@jeffclark), Matt Gillooly (@mattgillooly), Damon Cortesi (@dacort), and the Lollicode team. Ed was particularly helpful in answering follow-up questions after his initial review, improving security in the sample code, and taking a second peek at additional sections written into later drafts. I am also appreciative of the fact that Alex was willing to spend so much time looking at my words when he was writing his own O’Reilly book on Scala. This book is all the better for their participation.

My local Twitter community deserves props as well. I am in awe that a small university city could muster over 650 early adopters of the service, many of whom were among my peers at the Indiana University School of Informatics. Their use of Twitter is what makes my timeline so valuable. In the process of testing the code for this book, I had to rely on a number of people in my follow net to make sure I wasn’t inadvertently blowing up oil rigs in the Gulf. Thanks to my early reality checkers: Michelle (@MzHartz), Allison (@allisoncooke), Joel (@rhythmofself), Jonathan (@JonathanBranam), Noah (@noahwesley), Steve (@SoundSystemSDC), Daniel (@b00ger), Chintan (@tankchintan), Mike (@dmikeallen), Jenny (@jbhertel), and several others.

The Twitter version of this would be: “@everyone thanks!”

Get Twitter API: Up and Running now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.