Skip to content

Chapter 34 of 38

REST API Basics

Model resources, methods, status codes, representations, authentication, and errors.

46 minutes 10 quick checksBy Subha Prasad
Lesson 34 of 38Course navigation

Lesson content

Read, practise, then check your understanding

REST-style HTTP APIs expose resources through URIs and standard method semantics. GET reads, POST creates/actions, PUT replaces, PATCH modifies, and DELETE removes when those meanings fit.

Practical example

<?php
header('Content-Type: application/json; charset=utf-8');
http_response_code(201);
echo json_encode(['data' => ['id' => $id]], JSON_THROW_ON_ERROR);

Use accurate statuses, consistent error objects, authentication and authorization, content negotiation, pagination, rate limits, idempotency keys for retried creation, and versioning policy. Never expose stack traces or database errors. Validate media type and payload schema.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes resource?
Which PHP term matches this description: A domain concept identified by a URI.
Which statement best describes HTTP method?
Which PHP term matches this description: An operation semantic such as GET, POST, PUT/PATCH, or DELETE.
Which statement best describes status code?
Which PHP term matches this description: A machine-readable HTTP outcome category.
Which statement best describes idempotency?
Which PHP term matches this description: The property that repeating an operation has the same intended effect.
Which statement best describes content negotiation?
Which PHP term matches this description: Selection of representation through Accept and Content-Type headers.

0 of 10 checks passed

Your progress is saved on this device.