#73: Building a Web Service

To close out this chapter, let's take a quick look at how to build a REST web service. The idea is similar to almost any other kind of dynamic web page, but rather than worrying about how the page looks, all you need to do is properly construct an object, transform the object to XML, and print the result. After that, everything else is someone else's problem (at least, in theory).

We'll use the data from the example table in the Appendix A. The web service takes a category parameter (as POST or GET) and returns an XML product listing such as this:

<?xml version="1.0" encoding="utf-8"?> <Items> <Item> <Name>Cowboy Boots</Name> <ID>12</ID> <Price>19.99</Price> </Item> <Item> <Name>Slippers</Name> <ID>17</ID> <Price>9.99</Price> ...

Get Wicked Cool PHP 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.