Book Image

RESTful PHP Web Services

By : Samisa Abeysinghe
Book Image

RESTful PHP Web Services

By: Samisa Abeysinghe

Overview of this book

<p>Representational State Transfer (REST) is a form of software architecture; systems that follow it are often called RESTful. It is the architectural style that drives the World Wide Web (the motivation for REST was to capture elements that made the Web successful), and RESTful web services use simple protocols already familiar to web developers. RESTful web services are found in lots of places with big name Web APIs such as Flickr, and Yahoo search following a REST-based approach.<br /><br />With any architectural style or approach, struggling with its basic concepts is just one problem – implementation in your favorite language is another, and that's where this book comes in. If you're a PHP developer, this book will show you how to create and consume RESTful web services in PHP, and make your services work well in the context of the Web.<br /><br />You will learn about the basic concepts of REST, and then look at PHP tools, libraries and techniques for working with RESTful web services. You will see how to use other RESTful services from your PHP applications, and also how to engineer your PHP applications to make them RESTful.<br /><br />This book is about implementing RESTful web services in PHP, and so the book is packed with example code and careful explanations.</p>
Table of Contents (15 chapters)
RESTful PHP Web Services
Credits
About the Author
About the Reviewers
Preface
Index

Preface

This book discusses the use of PHP to implement web applications based on REST architectural principles. Web services are a popular breed of web application technologies in today's programmable Web, and REST is the most popular style used in there. This book uses real-world examples as well as step-by-step guidelines to explain how to design REST-style services and clients from the ground up and how to use PHP programming constructs and frameworks to implement those services and clients.

What This Book Covers

Chapter 1 introduces the concepts related to the programmable Web, shows how HTTP and web services are related to each other, introduces the principles behind REST, explains how HTTP verbs are used in REST applications, explains the need for RESTFul web services while building PHP web applications, and introduces some frameworks and tools that can be used to work with REST in PHP.

Chapter 2 takes a first look at REST with PHP. While providing and consuming REST-style web services, the primary pre-requisites are an HTTP server or an HTTP client library and an XML parser library. In this chapter, we will see how to use the PHP CURL API to consume web services using various HTTP verbs such as HTTP GET, POST, PUT, and DELETE. The DOM API and SimpleXML API for building XML object structures and parsing XML streams are also discussed. We will discuss in detail how to build XML request payloads and also how to parse XML response payloads. The final section of this chapter demonstrates how to use the HTTP client features and XML parser features to invoke the Flickr REST API.

Chapter 3 looks into some real-world applications and discusses how to combine multiple service interfaces to build value-added custom applications. In this chapter, we will see how to use RSS or ATOM feeds, Yahoo search API, and Yahoo maps API. With the know-how you gain in this chapter and the previous chapters, you could build very powerful value-added applications like mashups using publicly available REST-style services.

Chapter 4 covers the steps that you would have to follow in designing and implementing a resource-oriented service in detail. Identifying resources and business operations for a given problem statement, designing the URI patterns, selecting the correct HTTP verbs, mapping URI and HTTP verbs to business operations are covered using the library example. Implementing the services and business operations using PHP is explained in detail, step by step.

Chapter 5 covers the steps that you would have to follow in designing and implementing resource-oriented clients in detail. The design of the clients is governed by the design of the service. And the client programmer needs to understand the semantics of the service, which is usually communicated through service API documentation. In the examples of this chapter, we will use the library service API designed in Chapter 4 to explain how we could use an existing API while designing PHP applications.

Chapter 6 uses the REST classes provided with the Zend Framework to implement the sample library system. The design of the service and client are covered, along with the MVC concepts supported by the Zend Framework. We will discuss how resources map to the model in MVC, and how HTTP verbs when combined with resource URIs map to the controller in MVC. We will explore how to combine Zend_Rest_Server with Zend_Controller to implement the business operations of the service and how to use Zend_Rest_Client class to consume the services.

Chapter 7 looks into the use of tools to trace and look into the messages to figure out possible problems with request and response pairs passed between clients and services. That helps with debugging and troubleshooting of services and clients. We will also look into how we could look at the XML documents to figure out possible problems in building XML in this chapter, and discuss how we can locate problems in parsing incoming XML messages.

Appendix A introduces the WSO2 Web Services Framework for PHP (WSO2 WSF/PHP) and discusses how to use the WSF/PHP service API to implement the sample Library system as a REST service and implement a REST client to consume it. We will also look into using the SOAP features provided in the frameworks to implement a SOAP client to consume the same service using SOAP-style messages. This chapter also discusses the differences between REST and SOAP message styles, in brief.

Appendix B introduces a PHP class named RESTClient that can be used to consume REST-style services. This class supports all key HTTP verbs, GET, POST, PUT, and DELETE. The advantage of using such a class is that it minimizes the complexity of your client code. At the same time, you can re-use this class for all your REST-style client implementations. This PHP class is sufficient for most simple REST-style client programs, and requires no third-party libraries. However, if you want to implement services and also want advanced clients, it is advised to use a more established framework such as Zend Framework or WSO2 WSF/PHP introduced in Chapter 6 and Appendix A of this book.

What You Need for This Book

You need PHP5 installed with Apache httpd server to try out the samples of this book. You would require a MySQL installation to try out the library sample discussed in the book.

You also need to install Zend Framework and WSO2 WSF/PHP to try out the samples based on those frameworks.

Who This Book is For

This book is for PHP programmers who are interested in using Web Services in their applications. Sometimes, you would be interested in using the publicly available REST-style services in your own applications, in which case, the REST client concepts discussed in this book would be very useful. You might also be involved with the implementation of PHP applications where you want to expose some aspects of the application as services to the outside world, in which case, you can benefit from the REST service concepts covered in this book. In addition, if you are a software developer looking for a hands-on text that will help you understand REST principles, from the ground up, this book would be a very good guide for you.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: ISBN0001

A block of code will be set as follows:

<books>
   <book>
      <id>1</id>
      <name>Book1</name>
      <author>Auth1</author>
      <isbn>ISBN0001</isbn>
   </book>
</books>

New terms and important words are introduced in a bold-type font. Words that you see on the screen, in menus or dialog boxes for example, appear in our text like this: "clicking the Next button moves you to the next screen".

Note

Important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader Feedback

Feedback from our readers is always welcome. Let us know what you think about this book, what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply drop an email to , making sure to mention the book title in the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or email .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer Support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the Example Code for the Book

Visit http://www.packtpub.com/files/code/5524_Code.zip to directly download the example code.

The downloadable files contain instructions on how to use them.

Errata

Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing this you can save other readers from frustration, and help to improve subsequent versions of this book. If you find any errata, report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata added to the list of existing errata. The existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide the location address or website name immediately so we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with some aspect of the book, and we will do our best to address it.