Cover by Rasmus Lerdorf, Peter MacIntyre, Kevin Tatroe

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

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

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required