在PHP RESTful API中,HTTP狀態(tài)碼用于表示請(qǐng)求的處理結(jié)果。以下是一些常用的HTTP狀態(tài)碼及其用法:
header('HTTP/1.1 200 OK');
echo json_encode(array('message' => 'Request successful'));
header('HTTP/1.1 201 Created');
echo json_encode(array('message' => 'Resource created'));
header('HTTP/1.1 400 Bad Request');
echo json_encode(array('message' => 'Bad request'));
header('HTTP/1.1 401 Unauthorized');
echo json_encode(array('message' => 'Unauthorized'));
header('HTTP/1.1 404 Not Found');
echo json_encode(array('message' => 'Resource not found'));
header('HTTP/1.1 500 Internal Server Error');
echo json_encode(array('message' => 'Internal server error'));
根據(jù)具體的業(yè)務(wù)需求和錯(cuò)誤情況,可以使用不同的HTTP狀態(tài)碼來(lái)表示請(qǐng)求的處理結(jié)果,從而提高API的可讀性和可維護(hù)性。