. 87
( 132 .)


// load up the XML
$stories = simplexml_load_string($xml);

// and hey, presto
$last_updated = date(˜r™, $last_time);
if (empty($storycount))
if (empty($_REQUEST[˜storycount™]))
$storycount = 0; // all of ˜em
516 Part IV: Not So Simple Applications

$storycount = $_REQUEST[˜storycount™];

Now, to show off the goods. After opening an HTML table to contain the head-
lines, we create some variables pointing at the topic image directory on the
Slashdot site and a local image directory on our own server. What we™re going to do
is copy the files from their site to ours. Since the images are based on general top-
ics, the same ones are reused frequently. The first time we see each one, we make a
local copy, and thereafter, that™s what gets displayed on our pages. It™s faster for us,
and cheaper for them.

print <<<EOT
<table cellspacing=”0” cellpadding=”0” border=”0”>
<th colspan=”4” align=”left” style=”font-size:large;”>
Slashdot Stories
<th colspan=”4” align=”left” style=”font-size:smaller;”>
last updated {$last_updated}

// we are counting on this not changing..
$slashimage_dir = “http://images.slashdot.org/topics”;
// they get enough traffic - let™s store the Borg
// locally...
$ourimage_dir = “{$this_dir}/slashdot”;
$i = 0;
foreach ($stories->story as $story)
if (!file_exists(“{$ourimage_dir}/{$story->image}”))
, “{$ourimage_dir}/{$story->image}”
Chapter 15: XML Parsing 517

Again, note how PHP™s support for URLs-as-files makes this a snap. We just
use the built-in copy() routine, which copies a file from one place to another,
even though here, the file being copied is on some other server far off in the Net
At this point, we™re inside our main story loop, so we print out the headline, as
well as the name of the person who posted it, the all-important department-of-
strange-department-names line, and the number of comments about the story on
the Slashdot site. In a bid to add a tiny bit of value to what is otherwise wholesale
borrowing, we™ll change the way we display the number of comments. You know a
story is buzzing when the topic count goes through the roof, so the comments line
will go from normal, to bold, to bright red for the big ones:

// just to be fancy, let™s be alarmed
// by the number of comments
$comment_style = ˜™;
$comment_count = (int)$story->comments;
if ($comment_count > 200)
$comment_style .= ˜font-weight:bold;™;
if ($comment_count > 500)
$comment_style .= ˜color:red;™;
print <<<EOT
<td align=”center” valign=”top” rowspan=”2”><img height=”25”
<td align=”left” valign=”top” rowspan=”2”>&nbsp;</td>
<td align=”left” valign=”top” colspan=”2”><a href=”{$story->url}”
<td align=”left” valign=”top”>&nbsp;</td>
<td style=”font-size:smaller;” align=”left” valign=”top”>
from the {$story->department} dept.
by {$story->author} on {$story->time}
<span style=”{$comment_style}”>{$story->comments} comments so

518 Part IV: Not So Simple Applications

And that™s it. Now we just check the story count and break out of the loop if
we™ve reached our limit. Then we close the table, and we™re done.

if ($storycount and $i >= $storycount)
print <<<EOT



This chapter looked at the way in which PHP interacts with XML, which is an
extraordinarily useful markup language for representing data in flat file form so
that™s it™s more or less universally meaningful.
The new capabilities of PHP have made using an XML document feed ” and
there are lots of them out there ” go from what was really something of a pain, to
almost no trouble at all. Plus, obviously, headline feeds aren™t the only kind of serv-
ices available. The ease of using those services inside Web applications is going to
make them a lot more popular.

If you™re interested in getting deeper into XML and Web services, take a look
in the /book/xml-rpc directory on the CD. We™ve put the code for an XML-
RPC server and client there, along with some front-end pages you can use to
see how they work. There, we use the new DOM extension of PHP 5, built
around the libxml2 libraries, and Xpath, as well as the new object-oriented
features and exception-handling capabilities, to make it almost as easy to call
a function on someone else™s server as it is on your own.For more information
about XML-RPC, the best place to start is http://www.xmlrpc.com.

Next up: Simple Object Access Protocol (SOAP), a protocol for formatting mes-
sages with XML.
Chapter 16


— Introducing Simple Object Access Protocol (SOAP)

— Introducing NuSOAP classes

— Examining application architecture built around public Web services and
— Looking at SOAP operations in PHP

WOULDN™T IT MAKE SENSE if software applications could talk to each other via some-
thing like email? You know, have one application send a message to another in
order to invoke some function and have a message containing the result go back?
Well, of course, it would, and a number of proprietary systems have served this pur-
pose for a long time (IBM MQSeries is a long-standing example). Messaging sys-
tems enable applications ” including those written in different languages, running
on different processors, and created at different times ” to work together to meet
organizational-software-system requirements.
Messaging systems can be broken up, conceptually, into at least two parts: the
transport protocol and the message format. Proprietary systems have traditionally
used a closed protocol for at least one of those functions. Even relatively open stan-
dards, like remote procedure calls (RPCs) under Java, have relied on special facili-
ties that can be perceived as security weaknesses by skeptical minds or under
specific conditions.
This is the problem Simple Object Access Protocol (SOAP) is meant to solve.
SOAP defines an XML document type for invoking objects™ properties and func-
tions. XML documents that conform to this specification are messages to be sent,
like the body text of ordinary email messages between humans. SOAP messages,
once written, can be sent via HTTP, or, indeed, by Simple Mail Transport Protocol
(SMTP), the protocol normally used to send email. Hardly any routers or firewalls
block the TCP ports required by those transport protocols, so moving SOAP mes-
sages around is a relatively straightforward process.
This chapter doesn™t concern itself much with transport mechanisms. Instead,
we™re concerned here with the details of the SOAP message specification and the
means by which you can make PHP comply with them.

520 Part IV: Not So Simple Applications

Overview of SOAP
SOAP, at the end of the day, lives up to its first initial: It is indeed simple. SOAP
messages are essentially text passages with some special tags applied, similar in
some ways to HTML documents.
The SOAP specification is neither concerned with matters of transport (that™s the
role of the HTTP and SMTP specifications) nor with rules about what SOAP messages
can carry (not much, anyway; that™s up to the developers who build applications
that employ SOAP). The SOAP definition has mainly to do with the structure of the
documents that carry function calls to objects providing services and results back
to the calling programs.
You can divide SOAP transactions into the request and the response. The client,
via SOAP messages addressed to the server, says something like, “What™s the CEO™s
inseam measurement?” This presumes, of course, that the server has a function that
looks up or otherwise provides the boss™s inseam length.
The server then executes the called function and returns some value or series of
values. It might just return the integer 34, or return the integer 34 as well as the
string inches so that the unit being used is clear.
Now it™s time to have a look at the SOAP message itself. Though the SOAP spec-
ification includes information on a large number of special features (such as
attachments), SOAP messages are essentially envelopes and their contents. The
application we create in this chapter requires no SOAP features other than these
critical ones.

You can view the full SOAP version 1.2 specifications on the Web at
http://www.w3.org/TR/soap12-part0. (By the way, don™t go to
http://www.soap.org expecting to find information about the SOAP
protocol. That site is run by the Society for Obstetric Anesthesia and
Perinatology ” “Celebrating Over 150 Years of Obstetric Anesthesia”).

The SOAP envelope
The envelope surrounds the message payload, or body, and serves to define the
XML namespaces used throughout the SOAP message.
Because it is an XML document, the SOAP text begins with a statement of its
XML encoding and version:

<?xml version=™1.0™ encoding=™UTF-8™?>

Then you have an opening Envelope tag, preceded by the SOAP-ENV namespace
identifier. Indeed, the opening Envelope tag serves mainly to define three of the
four namespaces used in the SOAP message. Here™s the opening Envelope tag:
Chapter 16: SOAP 521


Though the intricacies of XML namespaces are beyond the scope of this book,
understand that the three namespaces defined in that passage of code serve the fol-
lowing roles:

— SOAP-ENV ” Provides the definition of Envelope and Body elements in this
SOAP message. The SOAP 1.2 specification is http://www.w3.org/2001/
09/soap-encoding. That URL will render readably in Microsoft Internet
Explorer; other browsers might require you to download the XML and
look at it in a text viewer (such as vi, emacs, or Windows Notepad).
— xsi ” The xsi schema defines aspects of function calls, such as values
sent as arguments.
— xsd ” The xsd schema deals with data types, such as integers, dates,
strings, and Boolean values.

After the opening Envelope tag comes the whole of the Body element (to be dis-
cussed next). At the conclusion of the SOAP message, after the Body element, you
find a closing Envelope tag, complete with its namespace identifier. It looks like


Now let™s move on to the Body element.

The SOAP body
Inside the SOAP envelope is the SOAP body, the payload that™s the whole point of
having SOAP in the first place. A body begins with an opening Body tag, further
distinguished by a SOAP-ENV namespace identifier:



. 87
( 132 .)