gpt4 book ai didi

php - 捕获 PHP fatal error

转载 作者:IT王子 更新时间:2023-10-28 23:57:53 25 4
gpt4 key购买 nike

我有一个启用了 restful 的 Web 服务站点,因此其他网站/ajax 脚本可以调用该站点以获取/设置数据。但是,每当 Web 服务站点以某种方式返回 PHP fatal error 时,返回的 HTTP 状态是 200 而不是 500。有没有办法修复它,以便在发生 fatal error 时返回 500 而不是 200?或者,如果不可能,我该如何更改我的客户端以识别网络服务返回的 fatal error ?

最佳答案

PHP 确实在发生 fatal error 时发送 HTTP 500。
让我在 this PHP bug-report 中引用一条评论(一个 xdebug 错误阻止了这种行为):

It does happen. It requires that:

1) display_errors = off
2) No headers have been sent yet by the time the error happens
3) Status is 200 at that time.

<?
ini_set('display_errors', 0);
foobar();
// This will return a http 500
?>

您可以使用 shutdown function 捕获 fatal error 和 error_get_last :

register_shutdown_function(function() {
$lastError = error_get_last();

if (!empty($lastError) && $lastError['type'] == E_ERROR) {
header('Status: 500 Internal Server Error');
header('HTTP/1.0 500 Internal Server Error');
}
});

关于php - 捕获 PHP fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2331582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com