Dr. Dobb's Journal - January 2008 - (Page 55) d01gutm_p4ma 11/9/07 11:31 AM Page 55 by Andi Gutmans and Cal Evans Core Technology PHP: The Power Behind Web 2.0 Flexing the muscles of a Web 2.0 community • Delivering a richer desktop-like user experience (Ajax). • Exposing functionality as easily consumable services (web services). • Leveraging the user-base to create, enhance, and categorize information. The notion of Rich Internet Applications is all about delivering desktop-like experiences in the browser. JavaScript and Ajax are commonly used on the client side to deliver this experience. The main advantage of these technologies is their presence in popular web browsers, and that they can be implemented incrementally without requiring rewrites of existing web applications. In this model, the client depends on the server for orchestrating and delivering its data. PHP has been popular with Ajax applications because it’s exceptionally well suited for this task. Not only is PHP a rich integration platform that includes connectivity to all popular databases, languages, file formats, and services, but it also has user-friendly and robust support for both the XML and JSON formats, typically used in client/server communications. The second component of Web 2.0 is exposing functionality via web services. Although Service-Oriented Architectures (SOA) have been around for a few years, no one has been exactly sure how to bring them to life. Web services, with their ubiquity and ease of implementation, deliver on the promise of SOA. Likewise, an even more popular buzzword for Web 2.0 is “mash-up.” Strictly speaking, mash-ups are any web service that combines two or more services to create a new service. Mash-ups are the poster-children for what the Web can be. Again, PHP has excellent support for web services, with its built-in support for SOAP, XML, and HTTP. Finally, user-generated content is where these applications leverage users not only for creating content, but also for enhancing it by categorization. In this article, we show the power of these communities by leveraging Flickr’s photo collection and the categorization its users have contributed. For this project, we use pieces of the Zend Framework (framework.zend.com) on the back end and the Prototype.js (www.prototypejs.org) and Script.aculo.us libraries on the front end. The services we mash up are: Andi is cofounder and cochief technology officer of Zend Technologies. Cal is editor of Zend’s DevZone. They can be contacted at www.zend.com. To understand why websites and applications might be considered Web 2.0, we first need to agree on a definition of the term. While there is no formal definition for it, we believe there are three essential components to Web 2.0: • A news feed. • The Yahoo Content Analyzer service (developer.yahoo.com/ search/content/V1/termExtraction.html). • Flickr’s API (www.flickr.com/services/api). We call this mash-up the “Flickr News Network” (FNN) (the complete source code for FNN is available online; see “Resource Center,” page 5). As Figure 1 illustrates, the concept behind FNN is simple: We want to show pictures from Flickr to augment an article from a given news feed. The implementation is straightforward—take the articles from a news feed and select pictures for it. Because Flickr lets people tag photos, all we need to do is identify the keywords in the article and ask Flickr to search the tags for those keywords. Luckily, Yahoo has an API, the Yahoo Content Analyzer, which does just this. It takes in as a parameter the content you want analyzed, then returns a list of what it feels are the keywords. When searching Flickr for keywords, you usually get multiple hits. For simplicity, we take the one with the highest “interestingness” rating and display it with the article. In the process, we take advantage of all three Web 2.0 themes: We make heavy use of Ajax and JavaScript in the client, leverage web services (including RSS feeds and services from Yahoo), and take advantage of the tagging contributed by Flickr’s user-base. As you see in Listing One, there’s not much HTML. Typical of many mash-ups, the heavy lifting is done by JavaScript, not HTML. As a disclaimer, the sample code we present illustrates one way the code could be written, but it’s not the only way. In many cases, we omit important steps (filtering input, escaping output, error checking, and other important security measures) that production applications would include. Other than the list of included JavaScript libraries in the , the only JavaScript we actually show in the HTML is the function called on page load. pageLoad() handles the things that we can’t do inside the JavaScript files or that have to be done at runtime. Specifically, since the web server’s PHP interpreter does not interpret the JavaScript files and we need to set a value from PHP, we set that value in the pageLoad(). The other thing we do is set up a RequestWatcher so that whenever anything changes, we can update our outstanding requests display. January 2008 l www.ddj.com l Dr. Dobb’s Journal 55 http://www.zend.com http://developer.yahoo.com/search/content/V1/termExtraction.html http://developer.yahoo.com/search/content/V1/termExtraction.html http://www.flickr.com/services/api http://framework.zend.com http://www.prototypejs.org http://Script.aculo.us http://www.ddj.com
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.