Consuming XML

Let's return to the topic of XML. This time, the XML is both served and consumed by PHP. When programming web services, it is common to produce XML output as well as get XML as input. One common example of such an implementation is the SOAP protocol, which isn't covered in this text. Instead of taking a normal query string, we take a full XML document as input. This is a reversal of what we have done thus far. In addition to generating the XML in PHP and reading it in JavaScript, we will do the exact opposite as well.

Another important topic to take into account is handling multiple query terms simultaneously and joining them together. First we need to go back to the design phase and determine how we to format our requests.

Designing the XML Request

We have two search fields to keep in mind when designing our XML request: the title and the author.

As promised, multiple searches can be executed simultaneously. This isn't particularly useful in our application; however, it could be useful in future web application development.

<request>
       <query>
              <title>PHP</title>
              <author>Andrew</author>
       </query>
 </request>

If multiple query nodes are present then the searches will each be executed sequentially. However, now we also need to redesign our XML response because the old response was suitable only for one set of results:

<?xml version="1.0" ?> <catalog> <query title="PHP" author="Andrew"> <book> <Title>Ajax with PHP 5</Title> <Author>Andrew Curioso</Author> <Year>2007</Year> <Publisher>O'Reilly ...

Get Ajax with PHP 5 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.