Home > Article > Backend Development > How to set response information in php
In web development, PHP language is a very commonly used back-end language. In PHP programming, the operation of setting response information is often involved. This article will briefly introduce how to set response information in PHP.
1. Set the HTTP response status code
Before the server returns the response, you can use the http_response_code()
function to set the response status code. For example, the following code can return a 404 status code:
http_response_code(404);
2. Set the response header
In the HTTP response, the response header part contains some information about the response. In PHP, you can use the header()
function to set response header information. For example, the following code sets the response header Content-Type
to application/json
:
header('Content-Type: application/json');
You can also set multiple response header information at once. For example, the following code sets the response headers Content-Type
and Content-Encoding
:
header('Content-Type: application/json'); header('Content-Encoding: gzip');
Note that all header()
functions must Called before anything is output. If the content has already been output, calling the header()
function will cause an error.
3. Set the response body
The response body refers to the actual content returned in the response. In PHP, you can use the echo
function to output the response body. For example, the following code outputs a piece of text:
echo 'Hello, world!';
Typically, the response body needs to be formatted into a specific format, such as JSON or XML. In PHP, you can use the json_encode()
function to format an array or object into JSON format. For example, the following code converts an associative array into JSON:
$data = array('name' => '张三', 'age' => 20); echo json_encode($data);
You can also use XMLWriter
class formats an array or object into XML format. For example, the following code converts an associative array into XML:
$data = array('name' => '张三', 'age' => 20); $xml = new XMLWriter(); $xml->openMemory(); $xml->startDocument('1.0', 'UTF-8'); $xml->startElement('person'); foreach ($data as $key => $value) { $xml->startElement($key); $xml->text($value); $xml->endElement(); } $xml->endElement(); echo $xml->outputMemory();
4. Setting cookies
In PHP, you can Use the setcookie()
function to set cookies. For example, the following code sets a cookie named name
with a value of 张三
:
setcookie('name', '张三');
setcookie()
The function supports multiple parameters, For example, you can set expiration time, scope, security and other information. Please refer to the PHP manual for details.
To sum up, setting response information is an important part of PHP development. Through the introduction of this article, I believe readers have mastered how to set the response status code, response header and response body in PHP, as well as how to set cookies.
The above is the detailed content of How to set response information in php. For more information, please follow other related articles on the PHP Chinese website!