O'Reilly logo

Ajax with PHP 5 by Andrew G. Curioso

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required