Current Location: Home> Latest Articles> How to use header() to simulate RESTful API response header

How to use header() to simulate RESTful API response header

M66 2025-05-28

When developing web applications, the RESTful API has become a widely used architectural style, following the standard HTTP protocol to provide flexible and extensible interfaces. By using HTTP response headers correctly, developers can control the behavior and response content of the interface, enhancing the interactivity and security of the interface.

PHP provides the header() function to set the response header, allowing developers to flexibly manage the API's response content, status code, cache control, etc. This article will introduce how to use the header() function to simulate the response header of the RESTful API to achieve more flexible interface interaction.

1. Use PHP's header() function to set the basic response header

In PHP, the header() function can be used to send raw HTTP header information. The basic syntax is as follows:

 header('Header-Name: Header-Value');

For example, to return a standard Content-Type header information, you can set it like this:

 header('Content-Type: application/json');

When simulating a RESTful API response, it is usually necessary to return data in JSON format and the corresponding HTTP status code. Therefore, the following response header can be set:

 header('Content-Type: application/json');
header('HTTP/1.1 200 OK');

2. Common types of simulating RESTful API response headers

2.1 Return data in JSON format

In the RESTful API, the response data is usually in JSON format. To inform the client of the data type returned, you can set the Content-Type header:

 header('Content-Type: application/json');

If you want to return a response data in JSON format, you can usually use the following code:

 $data = ['message' => 'success', 'code' => 200];
echo json_encode($data);

2.2 Set the response status code

The response status code of the RESTful API is crucial, indicating the processing result of the request. For example, 200 OK means the request is successful, 404 Not Found means the requested resource does not exist, and 500 Internal Server Error means an error occurred on the server.

Use the header() function to set the response status code:

 header('HTTP/1.1 404 Not Found');

If you need to simulate other common status codes, you can set them according to the situation:

 header('HTTP/1.1 201 Created');

2.3 Setting cache control

In some cases, the response of the API requires a cache header to control the client or proxy to cache the response. Common cache control headers include Cache-Control and Expires :

 header('Cache-Control: no-cache, no-store, must-revalidate');
header('Expires: 0');

These headers will tell the client not to cache the response data and re-request the data every time.

2.4 Set up CORS (cross-domain resource sharing) response header

If your API needs to be called by clients with different domain names, you must configure Cross-Domain Resource Sharing (CORS) response headers. You can use the Access-Control-Allow-Origin header to allow the specified domain to access the API:

 header('Access-Control-Allow-Origin: https://m66.net');

If you want to allow any domain name to access the API, you can use the wildcard character * :

 header('Access-Control-Allow-Origin: *');

2.5 Define custom response headers

The RESTful API can also set custom response headers as needed. Through the header() function, you can send any custom header information:

 header('X-Request-Id: 12345');
header('X-Rate-Limit-Limit: 100');

These custom headers can be used to provide additional information such as the requested ID, rate limit, etc.

3. Complete example: Simulating the response of the RESTful API

Here is a complete PHP example that demonstrates how to simulate a simple RESTful API response using the header() function: