O'Reilly logo

Programming PHP, 3rd Edition by Rasmus Lerdorf, Peter MacIntyre, Kevin Tatroe

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required