Tuesday, September 4, 2007

Whip Up a Yahoo! Mashup Using PHP Part I

By Akash Mehta http://bitmeta.org

So, you want to create your own mashup? Great! Mashups -- web applications that grab information from different external sources and mix it together in new and exciting ways -- are fun to build, popular with users, and oh-so-very Web 2.0.

Web services, a collection of standards and data formats that enable web applications to communicate with each other and share data, are the core technology behind mashups. They allow developers to directly feed data from databases without having to resort to messy screen-scraping, and enable developers to build rich content applications that make information more useful to users.

These days, numerous companies publish public APIs for their various web services. In this article, I'll show you how to use the powerful collection of Yahoo! APIs to build a mashup with PHP 5. First we'll take a look at what APIs are, and the various offerings from Yahoo! that we can take advantage of. I'll demonstrate how to search the web using Yahoo!'s entire database with only three lines of code, then take you through the process of building an entire application to search for 'Pizza' in 'Palo Alto, CA' with only 25 lines of PHP code.

This tutorial assumes a decent knowledge of PHP 5 -- specifically, that you're comfortable with the basic syntax, classes and objects, and have some basic background on web services. Take a look at the Wikipedia article on web services and SitePoint's other PHP tutorials if you think you may have trouble with these.

The Yahoo! Developer Network

The Yahoo! Developer Network is the central reference site for all Yahoo! APIs and a way of connecting with developers and enabling them to provide Yahoo!-powered services in their applications. Check it out for more in-depth reference information.

Available APIs

A quick glance through the sidebar of the developer network homepage reveals just how many APIs Yahoo! provides. Many of the standard Yahoo! services have public APIs available, such as Search, Maps and Answers, and each API has its own set of documentation. A number of startups acquired by Yahoo! also provide APIs to their services, including del.icio.us, Flickr and Upcoming.org, although each of these APIs is handled differently -- check their documentation for details. Here's a quick summary of the most popular ones.

Flickr

One of the most popular APIs available from Yahoo!, the Flickr web services offer access to all the photos on Flickr, photo meta data, tags and more. Popular uses of the API include visualizing locations on maps, galleries, posters and games. You can find links to examples on the Flickr services page including Retrievr, an image search engine that allows you to search for images matching your hand-drawn sketch.

Searching for images on retrievr.com (click to view image)

Yahoo! Maps

The Yahoo! maps API is extremely powerful and allows you to create interactive maps with high quality geographic imagery, adding the power of visual location to your web application. Popular applications of the API include mapping journeys and locating users. Runningmap.com is an excellent example of the Yahoo! Maps API, allowing you to plot your exercise routes and check your distances and other interesting statistics.

A map on Runningmap.com (click to view image)

Yahoo! Search

Want to build a search function for your web site, or add related links to your web application? One of the more traditional APIs from Yahoo!, the Search API allows powerful querying of the Yahoo! web search database. Christian Langreiter offers a comparison of Yahoo and Google search results in one of his mashups.

The application compares Yahoo! and Google results (click to view image)

Each API has different usage restrictions, so check the relevant guidelines before you begin to develop an application. Some APIs have rate limiting -- for example, the Web Search API is restricted to 5000 queries per IP address per 24 hour period. In addition, not all APIs can be used for commercial purposes. Yahoo! requires developers to register their applications with the developer network to receive an application ID, and include this application ID in each request they make to an API. While this data doesn't affect rate limiting, it allows Yahoo! to monitor API usage and contact developers if needed. You can get an application ID -- and you'll need one to try out the sample code in this article.

No comments:

About Me

Ordinary People that spend much time in the box
Powered By Blogger