REST API and PHP

Representational State Transfer (REST) is a style of developing an API, rather than a protocol such as HTTP or Simple Object Access Protocol (SOAP).

RESTful services are stateless. This means that each request to the server happens in isolation. In other words, the server should not need any knowledge of previous requests by the client in order to process the current request. Each request should contain all the information necessary to process that request. This provides the benefits of simplicity, scalability, and flexibility in the API.

Each URL represents a single resource or collection of resources, and the HTTP method (GET, POST, PUT, PATCH, or DELETE) you use to make the request determines whether you are retrieving the resource, creating it, updating it, or deleting it, respectively. The URL will be the same for all those operations; only the HTTP verb changes. The URL for a specific resource will also contain a unique identifier for that resource. 

Request Formats

There are two main formats to format the data you send to servers in your requests: XML and JSON. Both of these provide hierarchical structures for formatting data so that it may be easily read by both computers and humans.

HTTP Headers

Every HTTP request and response is sent with a number of headers that facilitate communication between the client and server or provide meta-information about itself. Some headers will be automatically generated for you as part of the client making the request, such as Host, User-Agent, or Content-Length.  

The Accept header allows you to specify a comma-separated list of content types expressed as a MIME type, such as text/html or application/json, which will be used to negotiate with the server to determine a mutual response body so that the client can correctly parse the request.

If the client is sending a POST request with a body, the Content-Type header should be provided to assist the server in parsing the data being sent. Most commonly, you will see this passed as application/json or application/xml.

The Cache-Control header in a server response will give information as to whether the response can be cached for later use by the client. This is typically only done for responses to GET requests but is also useful for decreasing the total number of requests made to a service if you are using data that is of a cacheable nature, thereby improving the performance of the application. If a response is cacheable, it will have a max-age header that specifies the number of seconds in which a request should be kept before it can be considered invalid and a new request generated.

If a request needs to be authenticated, the client may be required to pass an Authorization header. 

Making a Request with PHP

There are several approaches you can use to make requests, and ultimately all of them end up using the cURL extension to make the request.

If you had a simple GET request to make, then you could use the built-in file_get_contents function.