7.4. Creating a News Ticker

Popular on both television news networks and web sites, a news ticker displays information in a scrolling format. Unlike the static nature of television, the Web enables users to interact with these tickers. If something catches their eye, they can click the news item and it takes them to their desired information. Because of this interactivity, news tickers are quite popular on web sites, and as it turns out, easy to implement using Ajax (see Figure 7-1).

Like any other Ajax-enabled application, a web-based news ticker comes in two parts: a server application and a client application. Since RSS feeds can exist on any server, a PHP server-side proxy is used to retrieve requested feeds for the client. The client application is, of course, a mix of HTML and JavaScript.

Figure 7.1. Figure 7-1

7.4.1. The Server-Side Component

The PHP server application is extremely simple. Its only function is to retrieve data from a URL and return it back to the client. To do so, the page expects a url variable in the query string to indicate which data to retrieve. For instance:

newsticker.php?url=http://rss.news.yahoo.com/rss/topstories

This URL tells newsticker.php, the server-side component, to retrieve data from the Yahoo! News Top Stories RSS feed.

Because the server's only job is to retrieve remote information, the code is only a few lines long:

<?php header("Content-Type: ...

Get Professional Ajax, 2nd Edition 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.