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

class Book
{
  public $id;
  public $name;
  public $edition;

  public function __construct($id)
  {
    $this->id = $id;
  }
}

class Author
{
  public $id;
  public $name;
  public $books = array();

  public function __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:

GET

Retrieve information about a resource or collection of resources.

POST

Create ...

Get Programming PHP, 3rd Edition 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.