Article

Home » Server-side Coding » PHP & MySQL Tutorials » Mash Up Your Music with the Zend Framework

About the Author

Toby Somerville

Toby Somerville Toby is a one-time pilot and a sometime web applications architect and developer. He's done time with Honda, Orange, Savoy Hotel Group and Tennis Australia, and spends "me" time kite buggying and rock climbing.

View all articles by Toby Somerville...

Mash Up Your Music with the Zend Framework

By Toby Somerville

January 25th, 2008

Reader Rating: 8.5

Page: 1 2 3 Next

In a previous article, Akash Mehta detailed how to use the Yahoo! Maps API to create your own mashup. In this article, we'll look at using some other Yahoo! APIs to create our very own music mashup. Along the way, I'll introduce you to the Zend Framework, a time-saving, well-organised, open source framework for developing web applications in PHP 5.

To build the demo, we'll be using the Audioscrobbler API (from last.fm), the Flickr API, and Yahoo News.

The demo application we'll build will prompt users to enter their favorite band or artist. The mashup will:

  • Find links to that artist's top-rated albums and tracks.
  • Show related artists.
  • Display pictures of the artist from Flickr.
  • Display recent news items about the artist from Yahoo! News.

A screenshot of the final application is below. You can try it out yourself in this live demo, and if you'd like to play along at home, feel free to download the code.

The finished mashup

Objectives

This article is not going to give you a total in-depth review of every class and method of the Zend Framework, nor are you likely to make millions from the demo mashup that we'll build (sorry about that!). Rather, it's intended to whet your appetite for what the Zend Framework can do. Pulling data from external sources is extremely easy with the framework, and it's my hope that you'll use this article as a jumping point for exploring other aspects of the framework that you might be able to integrate into your everyday PHP coding.

I won't explain every part of the demo application -- just those parts of the code that demonstrate various features of the Zend Framework. However, the code is well commented if you're interested in delving deeper into its workings.

What Do You Need?

This article assumes a working knowledge of PHP5. To play along at home, you'll need a web server with PHP installed (version 5.1.4, at least). For the demo, I'll assume you're using Apache. You'll also need:

The Zend Framework

The Zend Framework is an open-source component library from Zend, (the company involved in the creation of PHP). The idea behind the framework is to provide developers with a large proportion of the functionality that they use every day. To quote the web site:

Extending the art & spirit of PHP, Zend Framework is based on simplicity, object-oriented best practices, corporate-friendly licensing, and a rigorously tested agile code base ... a lightweight, loosely-coupled component library simplified to provide 4/5s of the functionality everyone needs.

Zend Framework Highlights

Here's a snapshot of what the framework can do (I'm really only skimming the surface):

  • Model-View-Controller components for separating design from development
  • database abstraction to allow unified access to multiple databases such as MySQL, Oracle, Microsoft SQL Server, IBM DB2, PostgreSQL and SQLite
  • internationalization and localization of currency, dates, language, time, calendars and measurements
  • authentication, authorization, and session management
  • built-in support for web services such as Akismet, Amazon, Audioscrobbler, Delicious, Flickr, Simpy, StrikeIron, and Yahoo!
  • SOAP, Feeds, Google Data
  • mail, search, validation, caching, and much more

Rails, eat your heart out!

Who's Using the Framework?

The Zend framework is powering some large PHP applications, such as IBM's QEDWiki project, Magento (an open-source ecommerce engine), and Right Media for its Right Media Exchange, to name three. This should give you an indication of the framework's take-up -- if you decide to invest time learning the Zend Framework, it's a skill that will likely hold you in good stead in the future.

Getting Started

Extract the folders and files of the Zend Framework to your local web server. The folder you want is: Zend Framework-<version number>/library/Zend. This holds all the classes.

Setting the Application Path

Before we can use the framework, we need to specify the include path of the Zend folder (the one with all the classes). There are a few ways we can do this -- either in the .htaccess file, in the php.ini file, or from the page itself:

1. To add the include path via your .htaccess file, add the following line at the bottom of the .htaccess file in your server's web root folder (note that the value you use will depend on where you place the files):

php_value include_path "/home/sitepoint/public_html/"

2. To make the change in your php.ini file, add the following line:

include_path "/home/sitepoint/public_html/"

3. Finally, to add the include path from the page itself, use the set_include_path() function. Here's an example:

<?php
if(set_include_path('/home/sitepoint/public_html/') === false){
 die('Include path failed');
}
?>

Note that you can't just use include() or required() to implement the framework -- doing so will result in a fatal error.

The Loader Class

Now that we're all set to use the framework, let's load some classes. The loader class is a great shortcut for dynamically adding other files and classes to your application. It's not strictly necessary to use it, as once you've set your include_path, you can use the require_once() or include_once() functions to achieve the same objective.

However, the loader class gives you some added flexibility in terms of the way you load your classes. There are two main ways to use the class -- you can use it to load files, or to load classes.

In this example, we'll use the class loader as follows:

<?php
// include the loader  
require_once ('Zend/loader.php');

// Load 'Zend_Filter_Input' class i.e. Zend/Filter/Input.php
Zend_Loader::loadClass('Zend_Filter_Input');
?>

Filtering and Validating

The Zend_Filter_Input component is effectively a holding area for data -- an area that will only allow data to be released on the basis of the requirements of the requestor. It's a bit like airport security -- only people with valid credentials and luggage are allowed to board the aircraft. The people are the data, the luggage screening is the filter, and passport control is the validator. Therefore, any dangerous objects are removed (filtered) at luggage security screening, and only people with correct passports and tickets can board the aircraft through passport and ticket control (validation).

The filter and validation class Zend_Filter_Input is a powerful component that can not only simplify the code you write, but also save you a lot of time and effort on the onerous (but necessary) tasks of filtering and validating data. Helpfully, the data is also escaped for HTML output when it's returned, which should immediately translate to fewer XSS worries.

Available Filters and Validators

There are 12 standard filter/validation classes, each of which takes a string as an argument:

  • Alnum - removes all non-alphabetical or numeric characters
  • Alpha - removes all non-alphabetical characters
  • BaseName - returns the base name of a file
  • Digits - removes all non-digit values
  • Dir - returns the directory name of a path
  • HtmlEntities - converts characters to their HTML equivalents (if they exist)
  • Int - returns an integer
  • RealPath - converts relative paths, i.e. ../../somefile.php, to absolute path name, i.e. /root/home/somefile.php
  • StringToLower - converts alphabetical characters to lowercase
  • StringToUpper - converts alphabetical characters to uppercase
  • StringTrim - strips characters from the beginning and end of the string
  • StripTags - strips HTML and PHP tags other than those that are specifically allowed, as well as the attributes of those allowed tags; it can also remove or allow comments, i.e. <!-- some HTML comment -->

For our demo app, we'll be using StripTags and StringTrim. For more information on the other filters, refer to the Zend Framework's manual.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links