Web Services

This five-part article series will introduce you to Web Services. If you’ve ever wanted to incorporate them into your web sites, this is a good place to start. This article is excerpted from chapter 20 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).

This chapter discusses some of the more applicable implementations of Web Services technologies and shows you how to use PHP to start incorporating them into your Web application development strategy right now. To accomplish this goal without actually turning this chapter into a book unto itself, the discussion that follows isn’t intended to offer an in-depth introduction to the general concept, and advantages, of Web Services.

Even if you have no prior experience with or knowledge of Web Services, hopefully you’ll find this chapter quite easy to comprehend. The intention here is to demonstrate the utility of Web Services through numerous practical demonstrations. Specifically, the following topics are discussed:

Why Web Services? For the uninitiated, this section very briefly touches upon the reasons for all of the work behind Web Services and how they change the landscape of application development.

Real Simple Syndication (RSS): The originators of the World Wide Web had little idea that their accomplishments in this area would lead to what is certainly one of the greatest technological leaps in the history of humankind. However, the extraordinary popularity of the medium caused the capabilities of the original mechanisms to be stretched in ways never intended by their creators. As a result, new methods for publishing information over the Web have emerged and are starting to have as great an impact on the way we retrieve and review data as did their predecessors. One such technology is known as Real Simple Syndication, or RSS. This section introduces RSS and demonstrates how you can incorporate RSS feeds into your development acumen using a great tool called Magpie.

SimpleXML: New to PHP 5, the SimpleXML extension offers a new and highly practical methodology for parsing XML. This section introduces this new feature and offers several practical examples demonstrating its powerful and intuitive capabilities.

SOAP: SOAP plays an enormously important role in the implementation of Web Services. This section discusses its advantages and introduces PHP’s SOAP extension, which was made available with the version 5 release.

{mospagebreak title=Why Web Services?} 

Although the typical developer generally adheres to a loosely defined set of practices and tools, much as an artist generally works with a particular medium and style, he tends to create software in the way he sees most fit. As such, it doesn’t come as a surprise that although many programs resemble one another in look and behavior, the similarities largely stop there. Numerous deficiencies arise as a result of this refusal to follow generally accepted programming principles, with software being developed at a cost of maintainability, scalability, extensibility, and interoperability.

This problem of interoperability has become even more pronounced over the past few years, given the incredible opportunities for cooperation that the Internet has opened up to businesses around the world. However, fully exploiting an online business partnership often, if not always, involves some level of system integration. Therein lies the problem: if the system designers never consider the possibility that they might one day need to tightly integrate their application with another, how will they ever really be able to exploit the Internet to its fullest advantage? Indeed, this has been a subject of considerable discussion almost from the onset of this new electronic age.

Web Services technology is today’s most promising solution to the interoperability problem. Rather than offer up yet another interpretation of the definition of Web Services, here’s an excellent interpretation provided in the W3C’s “Web Services Architecture” document ( http://www.w3.org/TR/ws-arch/ ):

A Web Service is a software system designed to support interoperable machine-to-machine interaction over a network. It has an interface described in a machine-processable format (specifically WSDL). Other systems interact with the Web Service in a manner prescribed by its description using SOAP messages, typically conveyed using HTTP with an XML serialization in conjunction with other Web-related standards.

Some of these terms may be alien to the newcomer; not to worry, because they’re introduced later in the chapter. What is important to keep in mind is that Web Services open up endless possibilities to the enterprise, a sampling of which follows:

Software as a service: Imagine building an e-commerce application that requires a means for converting currency among various exchange rates. However, rather than take it upon yourself to devise some means for automatically scraping the Federal Reserve Bank’s Web page ( http:// www.federalreserve.gov/releases/ ) for the daily released rate, you instead take advantage of its (hypothetical) Web Service for retrieving these values. The result is far more readable code, with much less chance for error from presentational changes on the Web page.

Significantly lessened Enterprise Application Integration (EAI) horrors: Developers currently are forced to devote enormous amounts of time to hacking together often complex solutions to integrate disparate applications. Contrast this with connecting two Web Service–enabled applications, in which the process is highly standardized and reusable no matter the language.

Write once, reuse everywhere: Because Web Services offer platform-agnostic interfaces to exposed application methods, they can be simultaneously used by applications running on a variety of operating systems. For example, a Web Service running on an e-commerce server might be used to keep the CEO abreast of inventory numbers via both a Windows-based client application and a Perl script running on a Linux server that generates daily e-mails that are sent to the executive team.

Ubiquitous access: Because Web Services typically travel over HTTP, firewalls can be bypassed because port 80 (and 443 for HTTPS) traffic is almost always allowed. Although debate rages as to whether this is really prudent, for the moment it is indeed an appealing solution to the often difficult affair of firewall penetration.

Such capabilities are tantalizing to the developer. Believe it or not, as is demonstrated throughout this chapter, you can actually begin taking advantage of Web Services right now.

Ultimately, only one metric will determine the success of Web Services: acceptance. Interestingly, several global companies have already made quite a stir by offering Web Services application programming interfaces (APIs) to their treasured data stores. Among the most interesting offers include those provided by the online superstore Amazon.com, Google, and Microsoft, stirring the imagination of the programming industry with their freely available standards-based Web Services. Since their respective releases, all three implementations have sparked the imaginations of programmers worldwide, who have gained valuable experience working with a well-designed Web Services architecture plugged into an enormous amount of data.

Follow these links to learn more about these popular APIs:

  1. http://www.amazon.com/webservices/
  2. http://www.google.com/apis/ 
     
  3. http://msdn.microsoft.com/mappoint/

{mospagebreak title=Real Simple Syndication}

Given that the entire concept of Web Services largely sprung out of the notion that XML- and HTTP-driven applications would be harnessed to power the next generation of business-to-business applications, it’s rather ironic that the first widespread implementation of the Web Services technologies happened on the end-user level. RSS solves a number of problems that both Web developers and Web users have faced for years.

All of us can relate to the considerable amount of time consumed by our daily surfing ritual. Most people have a stable of Web sites that they visit on a regular basis, and in some cases, several times daily. For each site, the process is almost identical: visit the URL, weave around a sea of advertisements, navigate to the section of interest, and finally actually read the news story. Repeat this process numerous times, and the next thing you know, a fair amount of time has passed. Furthermore, given the highly tedious process, it’s easy to miss something of interest. In short, leave the process to a human and something is bound to get screwed up.

Developers face an entirely different set of problems. Once upon a time, attracting users to your Web site involved spending enormous amounts of money on prime-time commercials and magazine layouts, and throwing lavish holiday galas. Then the novelty wore off (and the cash disappeared) and those in charge of the Web sites were forced to actually produce something substantial for their site visitors. Furthermore, they had to do so while working with the constraints of bandwidth limitations, the myriad of Web-enabled devices that sprung up, and an increasingly finicky (and time-pressed) user. Enter RSS.

RSS offers a formalized means for encapsulating a Web site’s content within an XML-based structure, known as a feed. It’s based on the premise that most site information shares a similar format, regardless of topic. For example, although sports, weather, and theater are all vastly dissimilar topics, the news items published under each would share a very similar structure, including a title, an author, a publication date, a URL, and a description. A typical RSS feed embodies all such attributes, and often much more, forcing an adherence to a presentation-agnostic format that can in turn be retrieved, parsed, and formatted in any means acceptable to the end user, without actually having to visit the syndicating Web site. With just the feed’s URL, the user can store it, along with others if he likes, into a tool that is capable of retrieving and parsing the feed, allowing the user to do as he pleases with the information. Working in this fashion, you can use RSS feeds to do the following:

  1. Browse the rendered feeds using a standalone RSS aggregator application. Examples o f popular aggregators include RSS Bandit ( http://www.rssbandit.org/ ), Straw ( http:/ /www.gnome.org/projects/straw/ ), and SharpReader ( http://www.sharpreader.net/ ). A screenshot of RSS Bandit is shown in Figure 20-1.

     
  2. Subscribe to any of the numerous Web-based RSS aggregators and view the feeds via a Web browser. Examples of popular online aggregators include Google Reader ( http:/ / www.google.com/reader/ ), NewsIsFree ( http://www.newsisfree.com/ ), and Bloglines ( http://www.bloglines.com/ ) .
  3. Retrieve and republish the syndicated feed as part of a third-party Web application or service. Later in this section, you’ll learn how this is accomplished using the Magpie RSS class library.

 
Figure 20-1.  The RSS Bandit interface


WHO’S PUBLISHING RSS FEEDS?


Believe it or not, RSS has actually officially been around since early 1999, and in previous incarnations since 1996. However, like many emerging technologies, it remained a niche tool of the "techie" community, at least until recently. The emergence and growing popularity of news aggregation sites and tools has prompted an explosion in terms of the creation and publication of RSS feeds around the Web. These days, you can find RSS feeds just about everywhere, including within these prominent organizations:

Given the adoption of RSS in such circles, it isn’t really a surprise that we’re hearing so much about this great technology these days.


{mospagebreak title=RSS Syntax}

If you’re not familiar with the general syntax of an RSS feed, Listing 20-1 offers an example that will be used as input for the scripts that follow. Although a discussion of RSS syntax specifics is beyond the scope of this book, you’ll nonetheless find the structure and tags to be quite intuitive (after all, that’s why they call it Real Simple Syndication).

Listing 20-1. A Sample RSS Feed (blog.xml)

<?xml version="1.0" encoding="iso-8859-1"?>
   <rss version="2.0">
   <channel>
     
<title>Inside Open Source</title> 
      <link>http://opensource.apress.com/ </link>

   <item>
      <title>Killer Firefox Tip #294</title>
      <link>http://opensource.apress.com/ article/190/</link>
      <author>W. Jason Gilmore</author>
      <description>Like most of you, I spend bunches of time downloading large
         
files from the Web, typically podcasts and PDF documents…</description>
   </item>

   <item>
      <title>Beginning Ubuntu Linux wins Linux Journal Award!</title> 
      <link>http://opensource.apress.com/ article/189/</link>
      <author>Keir Thomas</author>
      <description>Woo hoo! My book, Beginning Ubuntu Linux, has won an award
          in the Linux Journal Editor’s Choice 2006 awards!
          More precisely…</description>
   </item>

   <item>
      <title>Forms Validation with CakePHP</title>
      <link>http://opensource.apress.com/ article/188/</link>
      <author>W. Jason Gilmore</author>
      <description>Neglecting to validate user input is akin to foregoing
          any defensive
          gameplan for containing the NFL’s leading rusher. Chances are
          sooner or later…</description>
   </item>
   </channel>
   </rss>

This example doesn’t take advantage of all available RSS elements. For instance, other feeds might contain elements describing the feed’s update interval, language, and creator. However, for the purposes of the examples found in this chapter, it makes sense to remove those components that have little bearing on instruction.

Now that you’re a bit more familiar with the purpose and advantages of RSS, you’ll next learn how to use PHP to incorporate RSS into your own development strategy. Although there are numerous RSS tools written for the PHP language, one in particular offers an amazingly effective solution for retrieving, parsing, and displaying feeds: MagpieRSS.

Please check back next week for the continuation of this article.

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye