Home >PHP >REST API in PHP

REST API in PHP

How to build a REST API in PHP: JSON, status codes and error responses, HTTP methods and routing, an API on top of a database, API keys and CORS for calls from JavaScript. Together, the examples in the articles form one small API for a task list.

An API is an interface through which programs exchange data – a mobile app with a website, JavaScript on a page with the server, or two servers with each other. What REST means, why the responses are in JSON and how to create and read JSON in PHP correctly.

read more →

The status code tells the client how the request turned out before it reads the response body. Which codes to use in an API, how to send them in PHP and how to return an error that both a program and a person understand.

read more →

In a REST API, the address says what you work with and the HTTP method what should happen. What the individual methods mean, how to send all API requests to one PHP file with .htaccess and how to split them there by address and method.

read more →

We already have the router and the response functions, now comes the main part: an API that reads and writes data in a database. A paginated list, detail, creating, updating and deleting a task – with data validation and prepared statements.

read more →

An API for programs does not log in with a form and a session, but with a key that the client sends with every request. How to create and store keys securely, how to check them in PHP and why Apache sometimes hides them from scripts.

read more →

JavaScript on a page may freely call only an API at the same address as the page. When the API is elsewhere, the server has to allow it with CORS headers. How the browser decides, what a preflight is and how to set it up correctly in PHP.

read more →