Zend PHP 7 Certification – Data Formats and Types – REST

This post covers the REST section of the Data Formats and Types chapter when studying for the Zend PHP 7 Certification.

REST is an acronym for Representational State Transfer. It is a design standard (not an extension) and consists of a set of architectural principles for designing web pages and services.

REST applications are supposed to be stateless, which means that the server does not store any state about the client session on the server side.

The applications expose URIs, designed to expose certain aspects of an application’s business logic on a server.

In addition, REST calls can be cached which means it can be reused by the browser later without having to initiate another request back to the server. This saves time and resources.

REST applications can transfer any data formats, for example XML, JSON or both.

The following data types supported include:

  • ASCII strings
  • Integers
  • Booleans
  • Scalars

REST uses HTTP verbs.

  • GET – List (without identifier)
  • GET – Resource (with identifier)
  • POST – Create
  • PUT – Update (with identifier)
  • DELETE – Delete (with identifier)

Using REST headers provide information about the entity or other resource that you are requesting.

There are main two concepts:

  • Content-type: What is being provided.
  • Accept: What is expected in the response.

Status codes used in REST applications include the following:

  • 201 = Created
  • 400 = Bad request/Failed validation
  • 401 = Unauthorised
  • 204 = No content (Useful with DELETE)
  • 500 = Application error

Using ext or cURL is a common way of sending more complex header requests from a PHP script.

Context switching refers to the act of providing different output based on criteria from the request.

The process inspects the HTTP request headers and/or the request URI, and varies the response appropriately. Context switching is commonly used for:

  • Providing different output for requests originated via XMLHttpRequest
  • Providing different output based on accept HTTP headers (for example, REST endpoints)
  • Providing alternate layouts/content based on browser detection

View the other sections:

Note: This article is based on PHP version 7.0.