PHP Web 2.0 Mashup Projects: Your Own Video Jukebox: Part 1

Exclusive offer: get 50% off this eBook here
PHP Web 2.0 Mashup Projects: Practical PHP Mashups with Google Maps, Flickr, Amazon, YouTube, MSN Search, Yahoo!

PHP Web 2.0 Mashup Projects: Practical PHP Mashups with Google Maps, Flickr, Amazon, YouTube, MSN Search, Yahoo! — Save 50%

Create practical mashups in PHP grabbing and mixing data from Google Maps, Flickr, Amazon, YouTube, MSN Search, Yahoo!, Last.fm, and 411Sync.com

$23.99    $12.00
by Shu-Wai Chow | February 2010 | AJAX MySQL Open Source PHP Web Development

In this article by Shu-Wai Chow, we will begin with a project to create our own personalized video jukebox. This mashup will pull music lists feeds from the social music site, Last.fm. We will parse out artist names and song titles from these feeds and use that information to search videos on YouTube, a user-contributed video site, using the YouTube web service. We will also take a look at the three different XML-based file formats from those two sites: XSPF for song playlists, RSS for publishing frequently updated information, and YouTube's custom XML format. Rather than creating our own XML-based parsers to parse the three formats, we will use parsers from PEAR, one for each of the three formats. Using these PEAR packages, we will create an object-oriented abstraction of these formats, which can be consumed by our mashup application.

Project Overview

What

Mashup the web APIs from Last.fm and YouTube to create a video jukebox of songs

Protocols Used

REST (XML-RPC available)

Data Formats

XML, XPSF, RSS

Tools Featured

PEAR

APIs Used

Last.fm and YouTube

 

Now that we've had some experience using web services, it's time to fine tune their use. XML-RPC, REST, and SOAP will be frequent companions when you use web services and create mashups. You will encounter a lot of different data formats, and interesting ways in which the PHP community has dealt with these formats. This is especially true because REST has become so popular. In REST, with no formalized response format, you will encounter return formats that vary from plain text to ad-hoc XML to XML-based standards.

The rest of our projects will focus on exposing us to some new formats, and we will look at how to handle them through PHP. We will begin with a project to create our own personalized video jukebox. This mashup will pull music lists feeds from the social music site, Last.fm. We will parse out artist names and song titles from these feeds and use that information to search videos on YouTube, a user-contributed video site, using the YouTube web service. By basing the song selections on ever-changing feeds, our jukebox selection will not be static, and will change as our music taste evolves. As YouTube is a user-contributed site, we will see many interesting interpretations of our music, too. This jukebox will be personalized, dynamic, and quite interesting.

Both Last.fm and YouTube's APIs offer their web services through REST, and YouTube additionally offers an XML-RPC interface. Like with previous APIs, XML is returned with each service call. Last.fm returns either plain text, an XML playlist format called XSPF (XML Shareable Playlist Format), or RSS (Really Simple Syndication). In the case of YouTube, the service returns a proprietary format. Previously, we wrote our own SAX-based XML parser to extract XML data. In this article, we will take a look at how PEAR, the PHP Extension and Application Repository, can do the XSPF parsing work for us on this project and might help in other projects.

Let's take a look at the various data formats we will be using, and then the web services themselves.

XSPF

One of XML's original goals was to allow industries to create their own markup languages to exchange data. Because anyone can create their own elements and schemas, as long as people agreed on a format, XML can be used as the universal data transmission language for that industry. One of the earliest XML-based languages was ChemXML, a language used to transmit data within the chemical industry. Since then, many others have popped up.

XSPF was a complete grassroots project to create an open, non-proprietary music playlist format based on XML. Historically, playlists for software media players and music devices were designed to be used only on the machine or device, and schemas were designed by the vendor themselves. XSPF's goal was to create a format that could be used in software, devices, and across networks.

XSPF is a very simple format, and is easy to understand. The project home page is at http://www.xspf.org. There, you will find a quick start guide which outlines a simple playlist as well as the official specifications at http://www.xspf.org/specs. Basically, a typical playlist has the following structure:

<?xml version="1.0" encoding="UTF-8"?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">
<title>Shu Chow's Playlist</title>
<date>2006-11-24T12:01:21Z</data>
<trackList>
<track>
<title>Pure</title>
<creator>Lightning Seeds</creator>
<location>
file:///Users/schow/Music/Pure.mp3
</location>
</track>
<track>
<title>Roadrunner</title>
<creator>The Modern Lovers</creator>
<location>
file:///Users/schow/Music/Roadrunner.mp3
</location>
</track>
<track>
<title>The Bells</title>
<creator>April Smith</creator>
<location>
file:///Users/schow/Music/The_Bells.mp3
</location>
</track>
</trackList>
</playlist>

playlist is the parent element for the whole document. It requires one child element, trackList, but there can be several child elements that are the metadata for the playlist itself. In this example, the playlist has a title specified in the title element, and the creation date is specified in the date element. Underneath trackList are the individual tracks that make up the playlist. Each track is encapsulated by the track element. Information about the track, including the location of its file, is encapsulated in elements underneath track. In our example, each track has a title, an artist name, and a local file location. The official specifications allow for more track information elements such as track length and album information.

Here are the playlist child elements summarized:

Playlist Child Element

Required?

Description

trackList

Yes

The parent of individual track elements. This is the only required child element of a playlist. Can be empty if the playlist has no songs.

title

No

A human readable title of the XSPF playlist.

creator

No

The name of the playlist creator.

annotation

No

Comments on the playlist.

info

No

A URL to a page containing more information about the playlist.

location

No

The URL to the playlist itself.

identifier

No

The unique ID for the playlist. Must be a legal Uniform Resource Name (URN).

image

No

A URL to an image representing the playlist.

date

No

The creation (not the last modified!) date of the playlist. Must be in XML schema dateTime format. For example, "2004-02-27T03:30:00".

license

No

If the playlist is under a license, the license is specified with this element.

attribution

No

If the playlist is modified from another source, the attribution element gives credit back to the original source, if necessary.

link

No

Allows non-XSPF resources to be included in the playlist.

meta

No

Allows non-XSPF metadata to be included in the playlist.

extension

No

Allows non-XSPF XML extensions to be included in the playlist.

A trackList element has an unlimited number of track elements to represent each track. track is the only allowed child of trackList. track's child elements give us information about each track. The following table summarizes the children of track:

Track Child Element

Required?

Description

location

No

The URL to the audio file of the track.

identifier

No

The canonical ID for the playlist. Must be a legal URN.

title

No

A human readable title of the track. Usually, the song's name.

creator

No

The name of the track creator. Usually, the song's artist.

annotation

No

Comments on the track.

info

No

A URL to a page containing more information about the track.

image

No

A URL to an image representing the track.

album

No

The name of the album that the track belongs to.

trackNum

No

The ordinal number position of the track in the album.

duration

No

The time to play the track in milliseconds.

link

No

Allows non-XSPF resources to be included in the track.

meta

No

Allows non-XSPF metadata to be included in the track.

extension

No

Allows non-XSPF XML extensions to be included in the track.

Note that XSPF is very simple and track oriented. It was not designed to be a repository or database for songs. There are not a lot of options to manipulate the list. XSPF is merely a shareable playlist format, and nothing more.

RSS

The simplest answer to, "What is RSS?", is that it's an XML file used to publish frequently updated information, like news items, blogs entries, or links to podcast episodes. News sites like Slashdot.org and the New York Times provide their news items in RSS format. As new news items are published, they are added to the RSS feed. Being XML-based, third-party aggregator software makes reading news items easy. With one piece of software, I can tell it to grab feeds from various sources and read the news items in one location. Web applications can also read and parse RSS files. By offering an RSS feed for my blog, another site can grab the feed and keep track of my daily life. This is one way by which a small site can provide rudimentary web services with minimal investment.

The more honest answer is that it is a group of XML standards (used to publish frequently updated information like news items or blogs) that may have little compatibility with each other. Each version release also has a tale of conflict and strife behind it. We won't dwell on the politicking of RSS. We'll just look at the outcomes. The RSS world now has three main flavors:

  • The RSS 1.0 branch includes versions 0.90, 1.0, and 1.1. It's goal is to be extensible and flexible. The downside to the goals is that it is a complex standard.
  • The RSS 2.0 branch includes versions 0.91, 0.92, and 2.0.x. Its goal is to be simple and easy to use. The drawback to this branch is that it may not be powerful enough for complex sites and feeds.

There are some basic skeletal similarities between the two formats. After the XML root element, metadata about the feed itself is provided in a top section. After the metadata, one or more items follow. These items can be news stories, blog entries, or podcasts episodes. These items are the meat of an RSS feed.

The following is an example RSS 1.1 file from XML.com:

<Channel xmlns="http://purl.org/net/rss1.1#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
rdf:about="http://www.xml.com/xml/news.rss">

<title>XML.com</title>
<link>http://xml.com/pub</link>
<description>
XML.com features a rich mix of information and services for the
XML community.
</description>
<image rdf:parseType="Resource">
<title>XML.com</title>
<url>http://xml.com/universal/images/xml_tiny.gif</url>
</image>
<items rdf:parseType="Collection">
<item rdf:about=
"http://www.xml.com/pub/a/2005/01/05/restful.html">
<title>
The Restful Web: Amazon's Simple Queue Service
</title>
<link>
http://www.xml.com/pub/a/2005/01/05/restful.html
</link>
<description>
In Joe Gregorio's latest Restful Web column, he explains
that Amazon's Simple Queue Service, a web service offering a
queue for reliable storage of transient
messages, isn't as RESTful as it claims.
</description>
</item>
<item rdf:about=
"http://www.xml.com/pub/a/2005/01/05/tr-xml.html">
<title>
Transforming XML: Extending XSLT with EXSLT
</title>
<link>
http://www.xml.com/pub/a/2005/01/05/tr-xml.html
</link>
<description>
In this month's Transforming XML column, Bob DuCharme
reports happily that the promise of XSLT extensibility via
EXSLT has become a reality.
</description>
</item>
</items>
</Channel>

The root element of an RSS file is an element named Channel. Immediately, after the root element are elements that describe the publisher and the feed. The title, link, description, and image elements give us more information about the feed.

The actual content is nested in the items element. Even if there are no items in the feed, the items element is required, but will be empty. Usage of these elements can be summarized as follows:

Channel Child Element

Required?

Description

title

Yes

A human readable title of the channel.

link

Yes

A URL to the feed.

description

Yes

A human readable description of the feed.

items

Yes

A parent element to wrap around item elements.

image

No

A section to house information about an official image for the feed.

others

No

Any other elements not in the RSS namespace can be optionally included here. The namespace must have been declared earlier, and the child elements must be prefixed.

If used, the image element needs its own child elements to hold information about the feed image. A title element is required and while optional, a link element to the actual URL of the image would be extremely useful.

Each news blog, or podcast entry is represented by an item element. In this RSS file, each item has a title, link, and a description, each, represented by the respective element. This file has two items in it before the items and Channel elements are closed off.

PHP Web 2.0 Mashup Projects: Practical PHP Mashups with Google Maps, Flickr, Amazon, YouTube, MSN Search, Yahoo! Create practical mashups in PHP grabbing and mixing data from Google Maps, Flickr, Amazon, YouTube, MSN Search, Yahoo!, Last.fm, and 411Sync.com
Published: September 2007
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

Note the use of the rdf namespace. The RSS 1.0 branch uses Rich Description Framework (RDF) extensively. RDF is an XML framework to make documents not only machine-friendly, but also human-friendly by organizing topics together. To consume RSS 1.0, we do not need to know too much about RDF. However, we will be taking a little closer look at it on a future project. For now, the key concept to take away is that individual items are represented by the item element in the RSS 1.0 branch.

The item element's children are summarized as follows:

Item Child Element

Required?

Description

title

Yes

A human readable title of the item.

link

Yes

A URL to the item.

description

Yes

A human readable description of the item.

others

No

Any other elements not in the RSS namespace can be optionally included here. The namespace must have been declared earlier, and the child elements must be prefixed.

Looking at a RSS 2.0 feed from IBM DeveloperWorks, we can see a lot of similarities:

<rss version="2.0">
<channel>
<title>developerWorks : Linux : Technical library</title>
<link>http://www.ibm.com/developerworks/index.html</link>
<description>
The latest content from IBM developerWorks
</description>
<pubDate>Wed, 10 Jan 2007 01:03:11 EST</pubDate>
<language>en-us</language>
<copyright>Copyright 2004 IBM Corporation.</copyright>
<image>
<title>IBM developerWorks</title>
<url>
http://www-106.ibm.com/developerworks/i/dwlogo-small.gif </url>
<link>
http://www.ibm.com/developerworks/index.html </link>
</image>
<item>
<title>
<![CDATA[
Whistle while you work to run commands on your computer
]] >
</title>
<description>
<![CDATA[
Use Linux or Microsoft Windows, the open source sndpeek
program, and a simple Perl script to read specific
sequences of tonal events -- literally whistling,
humming, or singing to your computer -- and run commands
based on those tones. Give your computer a short low
whistle to check your e-mail or unlock your your
screensaver with the opening bars of Beethoven's Fifth
Symphony. Whistle while you work for higher efficiency
]] >
</description>
<link>
<![CDATA[
http://www.ibm.com/developerworks/library/os- whistle/index.html?ca=drs-
]] >
</link>
<category>Articles</category>
</item>
<item>
<title>
<![CDATA[
Programming high-performance applications on the Cell BE
processor, Part 1: An introduction to Linux on the
PLAYSTATION 3
]] >
</title>
<description>
<![CDATA[
The Sony PLAYSTATION 3 (PS3) is the easiest and cheapest
way for programmers to get their hands on the new Cell
Broadband Engine (Cell BE) processor and take it for a
drive. Discover what the fuss is all about, how to
install Linux on the PS3, and how to get started
developing for the Cell BE processor on the PS3.
]] >
</description>
<link>
<![CDATA[
http://www.ibm.com/developerworks/linux/library/pa-linuxps3-1/index.html?ca=drs-
]] >
</link>
<category>Articles</category>
</item>
</channel>
</rss>

Like the 1.1 feed, 2.0 starts with information about the feed and the publisher, followed by news items.

There are some key differences between 1.1 and 2.0:

  • The rss element is the root element for 2.0.
  • A channel element follows rss and encompasses all other elements.
  • Each item is represented by an item element, but items do not have a parent element that groups like all together (like items in 1.1 does).

While structurally the feeds are similar, the tags and nesting are different enough to cause problems, especially when you consider that programs are the primary consumers of RSS feeds.

RSS 2.0 also has many more standard tags available in its namespace. RSS 2.0 recognized there is a lot of common information for channels and items that people would like to include beyond just titles, links, and descriptions. Things like a publication date, languages, and categories are used frequently. In RSS 1.1, one would have to use another standard and pull it into the document through namespacing. This is not only added overhead, but creates many ways of putting something as simple as a publication date into the feed.

Channel Child Element

Required?

Description

title

Yes

A human readable title of the feed.

link

Yes

A URL to the feed.

description

Yes

A human readable description of the feed.

category

No

One or more categories for the feed. There is no set standard for the available values of this element.

cloud

No

A cloud is a centralized server that holds information about a group of RSS feeds. This element will hold information about a remote procedure to call on the cloud server when the feed is updated. Attributes for this are the domain, port, path, procedure, and protocol. The remote procedure can be either XML-RPC or SOAP.

copyright

No

If the content is copyrighted, the copyright is placed into this element.

docs

No

A URL to the documentation for the feed.

generator

No

The name of the program used to generate the feed.

image

No

A section to house information about an official image for the feed.

language

No

The language in which the feed is written.

lastBuildDate

No

The last modification date of the feed.

managingEditor

No

The email address for the person responsible for the content of the feed.

pubDate

No

The last publication date of the feed.

rating

No

The PICS (Platform for Internet Content Selection) rating of the feed. See http://www.w3.org/PICS/ for more information about PICS.

skipDays

No

Days in which RSS aggregators should not read this feed.

skipHours

No

Hours in which RSS aggregators should not read this feed.

textInput

No

The name of a text input field to be displayed with this field.

ttl

No

TTL stands for, "Time To Live". It is the number of minutes the feed should stay in a client's cache.

webMaster

No

An email address of the person responsible for the technical aspects of this feed.

Likewise, RSS 2.0 has many more available child elements for item elements.

Item Child Element Required? Description
title Yes A human readable title of the item.
link Yes A URL to the item.
description Yes A human readable description of the item.
author No The email address of the author of this item.
category No One or more categories for the item. There is no set standard for the available values of this element.
comments No URL to a page of reader comments of the item.
enclosure No Allows a media file to be included for the item. The URL to the media file is included in a required attribute named, the MIME type of the file, are also required. The most common use of this tag is to specify an MP3 in podcasts.
guid No A unique identifier for the item.
pubDate No The last publication date of the item.
source No A third-party source for the item. Used in citation sources for an item.

YouTube Overview

YouTube almost needs no introduction these days. The site is as ubiquitous as many of the previous sites we discussed—Amazon, Google, Yahoo!, and MSN. Links to its videos have been passed around email accounts. In case you have lead a very sheltered existence, we will take a brief look at what YouTube does, some features, and its Web API.

In a nutshell, YouTube is a site that allows users to share homemade videos with the public via the Internet. Users upload any video they wish (with much respect to copyright laws) and other users may view and comment on them. The latter has led YouTube to become a strong social networking site in addition to just sharing videos.

Some of the available features of YouTube include:

  • Video Tagging
    YouTube relies on its user community to describe the videos in its repository. This is done by allowing users to associate descriptive words and short phrases with a particular video. This process is known as tagging. For example, if I am watching a clip of a live performance by the Brooklyn, New York-based band, They Might Be Giants, YouTube allows me to tag the clip with, "They Might Be Giants", "Brooklyn", "alternative".
  • Video Search
    YouTube has a robust search engine that queries the tags placed on videos. By searching on tags, a video's description is democratized. If YouTube's search engine queried only the description given to it by the submitter, that person has a large influence on how that video is returned in search results. By also querying tags, the search engine can find videos the community thinks should be returned. In our example, with "They Might Be Giants", the clip submitter may not have noted that the band is based in Brooklyn. However, because I tagged the clip with "Brooklyn," any Brooklynite looking for local bands may also discover They Might Be Giants.
  • Submitter Subscriptions
    YouTube keeps track of a user's video submissions. It is then natural to let other users subscribe to another user's submissions. Through their antics and jeremiads, a few people have gained quite a following on YouTube, and found their fifteen minutes of fame.
  • Community
    YouTube builds to the social aspect of its site by allowing users to comment on videos.
  • Embedding on Other Sites
    YouTube allows users to embed a video on their own web site. YouTube, then, essentially acts a video hosting service. In addition, an external site can also just display a quick thumbnail of a video with a link to the video and all its information and comments on YouTube.

The social networking aspect of YouTube makes for a good web service subject, and the site has taken advantage of that.

YouTube Developer API

Like some of the other web services we've encountered, YouTube's API requires a developer ID that needs to be passed to the server when calling services. You can sign up for a developer ID at http://www.youtube.com/signup?next=my_profile_dev. After you have an ID, you can dig into the documentation. The documentation can be found at http://www.youtube.com/dev_docs.

The available methods fall into two categories.
A method can either be related to user information, or related to video viewing. YouTube uses a quasi-Java, dot notation to name their web service methods. For example, the method to get a user's profile is named youtube.users.get_profile, and the method to get a list of featured YouTube videos is named youtube.videos.list_featured.

Each method can be called either using REST or XML-RPC. A REST request takes the method name as a parameter along with any other parameters needed. The format used is http://www.youtube.com/api2_rest?method=METHOD_NAME&parameter1=VALUE1&parameter2=VALUE2. For example, let's look at get_profile, which gets a user's profile. The documentation for this method (located at http://youtube.com/dev_api_ref?m=youtube.users.getprofile) says this method requires three parameters:

  • method is the method name itself. For get_profile, the value for method is the method's formal name, youtube.users.getprofile. This parameter is only needed for REST requests, which we are using in this example.
  • dev_id, which is your developer ID.
  • user, the profile name of the user whom you want to retrieve.

The REST request for get_profile would be http://www.youtube.com/api2_rest?method=youtube.users.getprofile&dev_id=YOUR_DEVELOPER_ID&user=PROFILE_NAME.

The same method names are used when interfacing with XML-RPC. To use XML-RPC, the request is a standard XML-RPC call that takes only one param element, a struct, and each YouTube method parameter is sent as a value of the struct. The name of the method is not passed into the struct. Instead, follow XML-RPC standards by putting the name of the method in the methodName element of the call. Under XML-RPC, the call to get_profile would look like this:

<?xml version="1.0" ?>
<methodCall>
<methodName>youtube.users.get_profile</methodName>
<params>
<param>
<value>
<struct>
<member>
<name>dev_id</name>
<value>
<string>YOUR DEVELOPER ID</string>
</value>
</member>
<member>
<name>user</name>
<value>
<string>PROFILE NAME</string>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodCall>

The server will always return an XML response. Each method's response is detailed in the documentation. If REST is used, the response is just the XML in string form. If XML-RPC is used, that same documented XML string is escaped and encased in an XML-RPC response wrapper.

We can see how this works with get_profile. Consult the documentation to see how the response is structured. According to the documentation, this is how the response will be returned if we are using REST:

<user_profile>
<first_name>Shu</first_name>
<last_name>Chow</last_name>
<about_me>I pound on a keyboard for a living</about_me>
...
<friend_count>3</friend_count>
<favorite_video_count>7</favorite_video_count>
<currently_on>false</currently_on>
</user_profile>

If we made the call with XML-RPC, the XML structure would be the same, but this time, the XML is a string within a methodResponse element:

<?xml version='1.0' ?>
<methodResonse>
<params>
<param>
<value><string>
<user_profile>
<first_name>Shu</first_name>
<last_name>Chow</last_name>
<about_me>
I pound on a keyboard for a living
</about_me>
...
<friend_count>3</friend_count>
<favorite_video_count>7</favorite_video_count>
<currently_on>false</currently_on>
</user_profile>
</string></value>
</param>
</params>
</methodResponse>

As you can see, the YouTube API is simple, consistent, and well-documented.

Last.fm Overview

Last.fm (http://www.last.fm) describes itself as a social music networking site. It is an interesting way to find new music based on user contributions. Users register for free at the site. You then download a small client program to run on your machine. This program monitors what music you are playing through software media players such as iTunes and WinAmp. You can also tag the songs through the program. The program uploads the song information, including tags, back to the Last.fm server. Based on the artist, song, genre, and what other people have tagged songs, Last.fm builds a music profile for you. It uses this profile to build a custom streaming radio station for you that you can also listen to through the client program, and recommends other artists and songs that are similar to the music that you like.

Vigilant users may, understandably, bristle at installing a desktop program that sends their music listening habits off to a server somewhere. However, Last.fm does a good job of protecting your privacy. First off, the client program is open source. It does not contain any malware that would compromise your privacy or your computer's security, and the code is opened up for peer review. Second, their privacy policy explicitly states they do not share your personal information to outside parties. Finally, the registration process does not ask for information such as address or names, although you can enter your zip or postal code to see music events in your area. The music profile that it generates is tied to your account and profile you create. Any private information regarding you, the individual, is not required.

As we walk through the project and the examples, I will be demonstrating things using my own personal account. This will guarantee that the examples indeed work. To truly personalize this project as your own, you should consider creating an account and uploading some track data through the Last.fm client program.

Audioscrobbler Web Services

Audioscrobbler Web Services API is the web service that allows you to access data displayed on Last.fm. Audioscrobbler's home is at http://www.audioscrobbler.net/data/webservices. This web service is basically a collection of RSS feeds that fall within several categories:

  • User Profile Data: The largest collection of feeds, this category provides data about a certain user. For example, there are feeds that list the top artists they've listened to, and the top tracks.
  • Artist Data: This category aggregates data about all artists in the Last.fm database. By providing an artist name, you can get things such as their most listened to tracks on Last.fm, their largest fans, the users that have listened to them the most, etc.
  • Album Data: This category holds information about albums. Currently, there is only one feed associated with this category. By providing a title name to the Info feed, you can get a track listing for the album.
  • Track Data: Information about specific tracks is given here.
  • Tag Data: Like YouTube, Last.fm allows users to tag songs with their own descriptions.
  • Group Data: Like other social networking sites, Last.fm provides groups users can create and join. Information about what members are listening to in these groups is available in the Group Data feeds.
  • Forum Data: Last.fm has a community forum. The posts are offered in an RSS feed.
  • LiveJournal Protocol: Audioscrobbler can interact with the LiveJournal web service through this web service.

Looking at the Audioscrobbler Web Services API home page, we see that each feed can be in up to four formats—plain text, generic XML, XSPF, or RSS. Often, the available formats are dictated by their purpose. For example, it would not make sense to output user information in XSPF, a format made just for representing song playlists.

The web service is not documented well, but is very simple, and documentation is by example. On the API home page, the format links to each feed is an example of how to use the feed. Feeds pertaining to member information use the user "RJ", who is one of the original founders of Last.fm. Feeds pertaining to artist information, like top listeners, use an assortment of artists. To use any of the feeds, take the URL of the playlist and format you want, and replace it with the user or artist.

Let's play with some examples. The feeds are easy to use and experiment with, because the Audioscrobbler feeds are just URLs and no developer ID is necessary to use them. The API home page shows the top tracks played by a user are available in plain text, Last.fm's own XML format, and as an XSPF playlist. The URL on that page for RJ's top tracks in XSPF is http://ws.audioscrobbler.com/1.0/user/RJ/toptracks.xspf.

If I want to see my top tracks, I can just replace RJ with my own Last.fm user name: http://ws.audioscrobbler.com/1.0/user/ShuTheMoody/toptracks.xspf.

The artist feeds uses Metallica as the default example. To see a list of top Last.fm fans of Metallica, use the Top Fans feed on the API home page: http://ws.audioscrobbler.com/1.0/artist/Metallica/fans.xml. To see another artist's top fans, just change the Metallica section of the URL: http://ws.audioscrobbler.com/1.0/artist/Donnas/fans.xml.

>> Continue Reading: PHP Web 2.0 Mashup Projects: Your Own Video Jukebox: Part 2

 

If you have read this article you may be interested to view :

PHP Web 2.0 Mashup Projects: Practical PHP Mashups with Google Maps, Flickr, Amazon, YouTube, MSN Search, Yahoo! Create practical mashups in PHP grabbing and mixing data from Google Maps, Flickr, Amazon, YouTube, MSN Search, Yahoo!, Last.fm, and 411Sync.com
Published: September 2007
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

About the Author :


Shu-Wai Chow

Shu-Wai Chow has worked in computer programming and information technology for the past eight years. He started his career in Sacramento, California, spending four years as the webmaster for Educaid, a First Union Company, and another four years at Vision Service Plan as an application developer. Through the years, he has become proficient in Java, JSP, PHP, ColdFusion, ASP, LDAP, XSLT, and XSL-FO. Shu has also been the volunteer webmaster and a feline adoption counselor for several animal welfare organizations in Sacramento.

He is currently a software engineer at Antenna Software in Jersey City, New Jersey, and is finishing his studies in Economics at Rutgers, the State University of New Jersey.

Born in the British Crown Colony of Hong Kong, Shu did most of his alleged growing up in Palo Alto, California. He lives on the Jersey Shore with seven very demanding cats, four birds that are too smart for their own good, a tail-less bearded dragon, a betta who needs her tank cleaned, a dermestid beetle colony, a cherished Fender Stratocaster, and a beloved, saint-like fiancé.

Books From Packt

PHP 5 E-commerce Development
PHP 5 E-commerce Development

Expert PHP 5 Tools
Expert PHP 5 Tools

PHP Team Development
PHP Team Development

JavaFX 1.2 Application Development Cookbook
JavaFX 1.2 Application Development Cookbook

GlassFish Security
GlassFish Security

Learning jQuery 1.3
Learning jQuery 1.3

AJAX and PHP: Building Modern Web Applications 2nd Edition
AJAX and PHP: Building Modern Web Applications 2nd Edition

Drupal 6 Attachment Views
Drupal 6 Attachment Views

No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2
4
k
f
W
2
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software