REST Clients
A RESTful web service is a loose term describing web APIs implemented using HTTP and the principles of REST. A RESTful web service describes a collection of resources, along with basic operations a client can perform on those resources through the API.
For example, an API might describe a collection of authors and the books those authors have contributed to. The data within each object type is arbitrary. In this case, a “resource” is each individual author, each individual book, and the collections of all authors, all books, and the books each author has contributed to. Each resource must have a unique identifier so calls into the API know what resource is being retrieved or acted upon.
You might represent a simple set of classes to represent the book and author resources, as here in Example 15-1.
Example 15-1. Book and Author classes
classBook{public$id;public$name;public$edition;publicfunction__construct($id){$this->id=$id;}}classAuthor{public$id;public$name;public$books=array();publicfunction__construct($id){$this->id=$id;}}
Because HTTP was built using the REST architecture in mind, it provides a set of “verbs” that you use to interact with the API. We’ve already seen GET and POST verbs, which websites often use to represent “retrieve data” and “perform an action.” RESTful web services introduce two additional verbs, as you’ll see in the following list:
GETRetrieve information about a resource or collection of resources.
POSTCreate ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access