Current Location: Home> Latest Articles> Use header() to set the correct posture of the JSON response header

Use header() to set the correct posture of the JSON response header

M66 2025-05-28

When developing web applications, the JSON format is widely used in data exchange between clients and servers. To correctly handle JSON responses, PHP provides the header() function to set the response header information. This article will explain how to use the header() function to correctly set the JSON response header.

What is the header() function?

PHP's header() function is used to send raw HTTP response header information to the client. By setting the correct response header, the server tells the browser or client the type of resource requested so that they correctly handle the response content. Especially when we return JSON data, it is very important to set Content-Type to application/json .

Why set Content-Type to application/json?

By default, PHP does not set the response Content-Type . If we return JSON data, the browser or client may not be able to parse it correctly. Therefore, we need to explicitly tell the client that we are returning JSON data. By setting Content-Type to application/json , we ensure that the client can handle the response correctly and treat it as a JSON format.

How to set JSON response header using the header() function?

In order to correctly return JSON data, we need to perform the following steps:

  1. Send the correct Content-Type response header

  2. Make sure there are no other outputs

  3. Output JSON data

The following is a simple example code showing how to correctly set the JSON response header in PHP and return JSON data.

 <?php
// set up Content-Type for application/json
header('Content-Type: application/json');

// Simulate some data
$data = array(
    'status' => 'success',
    'message' => 'Data has been processed successfully',
    'url' => 'https://m66.net/success'
);

// 将数据转换for JSON Format
echo json_encode($data);
?>

Code parsing

  1. header('Content-Type: application/json');
    This line of code is the most important, it tells the browser or client that the content type of the response is JSON. This allows the client to correctly parse the returned data.

  2. $data = array(...)
    Here we build a simple array as the data we want to return. In actual development, you usually query data from a database or get data from an external API and format it as JSON.

  3. json_encode($data);
    This function converts PHP arrays or objects to JSON format. Note that json_encode() automatically processes special characters in the array, such as quotes or backslashes, to ensure that the generated JSON data complies with the specification.

Things to note

  • Don't output anything before sending the header <br> PHP cannot have any output (for example, spaces, HTML tags, or error messages) before calling the header() function. If there is any output before calling header() , PHP will report an error: Cannot modify header information - headers already sent .

  • Make sure the response headers are in the correct order <br> The response header should be sent before any other output. If HTML output or other content output is performed after sending the JSON response header, the browser will not be able to parse the JSON data correctly.

  • Character encoding problem of JSON data <br> If you need to return JSON data containing non-ASCII characters, make sure to use the JSON_UNESCAPED_UNICODE flag in json_encode() , so that Chinese characters can be avoided being escaped into Unicode encoding.

 echo json_encode($data, JSON_UNESCAPED_UNICODE);

Comprehensive examples

Here is a comprehensive example of all best practices:

 <?php
// Prevent any output
ob_start();

// set up响应头for JSON Format
header('Content-Type: application/json');

// set up其他头部信息(If cross-domain requests are allowed)
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');

// Simulate data obtained from a database or elsewhere
$data = array(
    'status' => 'error',
    'message' => 'An error occurred,Please try again later',
    'url' => 'https://m66.net/error'
);

// 将数据转换for JSON Format并输出
echo json_encode($data, JSON_UNESCAPED_UNICODE);

// End script,Make sure there are no other outputs
ob_end_flush();
?>

Summarize

In PHP, it is very easy to set up JSON response headers using the header() function. By setting Content-Type: application/json , you can ensure that the client correctly processes and parses the returned JSON data. Remember, before setting the response header, make sure there is no other output so that errors can be avoided.

Hopefully this article helps you better understand how to handle JSON responses using PHP. If you have any questions, feel free to ask!