具有HTTP状态的Php脚本


Php script with HTTP status

我需要你的帮助来编写PHP脚本。如何使用适当的HTTP状态进行响应:2xx表示成功,4xx表示错误请求(无重试),或者5xx服务器不可用(重试)来自指定的url?

使用http_response_code:

<?php
http_response_code(404);
?>

在发送任何输出之前发送标题:

低于PHP 5.4

header("HTTP/1.0 404 Not Found");    
header("HTTP/1.1 200 OK");    
header("HTTP/1.1 500 Internal Server Error");

PHP 5.4及以上

http_response_code(404);
http_response_code(200);
http_response_code(500);

有关使用PHP发送HTTP标头的详细信息:http://php.net/manual/en/function.header.php

可用收割台响应代码概述:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html