php 设置响应状态代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6163970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:32:21  来源:igfitidea点击:

Set Response Status Code

phpcakephpcakephp-1.3http-status-codes

提问by Rob Wilkerson

I have an API call for which I need to be able to run some checks and potentially return various status codes. I don't need custom views or anything, I just need to return the proper code. If the user hasn't passed proper credentials, I need to return a 401 status. If they haven't sent a supported request format, I need to return a 400 status.

我有一个 API 调用,我需要能够运行一些检查并可能返回各种状态代码。我不需要自定义视图或任何东西,我只需要返回正确的代码。如果用户没有通过正确的凭据,我需要返回 401 状态。如果他们没有发送支持的请求格式,我需要返回 400 状态。

Because it's an API, all I really want to do is set the response status and exit with a simple, stupid message about why the request failed (probably using a exit). Just enough to get the job done, but I haven't been able to get this to work right. I've tried using PHP's header()and Cake's $this->header()(this is all in the controller), but although I get the exit message, the header shows a 200 OKstatus.

因为它是一个 API,所以我真正想要做的就是设置响应状态并用一条简单而愚蠢的消息退出,说明请求失败的原因(可能使用exit)。刚好足以完成工作,但我一直无法让它正常工作。我已经尝试使用 PHPheader()和 Cake 的$this->header()(这些都在控制器中),但是虽然我收到了退出消息,但标题显示了200 OK状态。

Using the code below, I get the message, but the header isn't set. What am I missing?

使用下面的代码,我收到消息,但未设置标题。我错过了什么?

  if( !$this->auth_api() ) {
    header( '401 Not Authorized' );
    exit( 'Not authorized' );
  }

回答by Brad

PHP <=5.3

PHP <=5.3

The header()function has a parameter for status code. If you specify it, the server will take care of it from there.

header()函数有一个状态代码参数。如果您指定它,服务器将从那里处理它。

header('HTTP/1.1 401 Unauthorized', true, 401);

PHP >=5.4

PHP >=5.4

See Gajus' answer: https://stackoverflow.com/a/14223222/362536

见 Gajus 的回答:https://stackoverflow.com/a/14223222/362536

回答by Gajus

Since PHP 5.4 you can use http_response_code.

从 PHP 5.4 开始,您可以使用http_response_code

http_response_code(404);

This will take care of setting the proper HTTP headers.

这将负责设置正确的 HTTP 标头。

If you are running PHP < 5.4 then you have two options:

如果您运行的是 PHP < 5.4,那么您有两个选择:

  1. Upgrade.
  2. Use this http_response_codefunctionimplemented in PHP.
  1. 升级。
  2. 使用在 PHP 中实现的这个http_response_code函数

回答by Wesley Murch

I don't think you're setting the headercorrectly, try this:

我认为你没有正确设置标题,试试这个:

header('HTTP/1.0 401 Unauthorized');

回答by nahri

Why not using Cakes Response Class?You can set the status code of the response simply by this:

为什么不使用Cakes 响应类?您可以简单地通过以下方式设置响应的状态代码:

$this->response->statusCode(200);

Then just render a file with the error message, which suits best with JSON.

然后只渲染一个带有错误消息的文件,它最适合 JSON。

回答by Michael Temple

I had the same issue with CakePHP 2.0.1

我在 CakePHP 2.0.1 上遇到了同样的问题

I tried using

我尝试使用

header( 'HTTP/1.1 400 BAD REQUEST' );

and

$this->header( 'HTTP/1.1 400 BAD REQUEST' );

However, neither of these solved my issue.

但是,这些都没有解决我的问题。

I did eventually resolve it by using

我最终通过使用解决了它

$this->header( 'HTTP/1.1 400: BAD REQUEST' );

After that, no errors or warning from php / CakePHP.

之后,没有来自 php / CakePHP 的错误或警告。

*edit: In the last $this->headerfunction call, I put a colon (:) between the 400 and the description text of the error.

*edit:在最后一个$this->header函数调用中,我:在 400 和错误描述文本之间放置了一个冒号 ( )。

回答by coder

As written before, but for beginner like me don't forget to include the return.

如前所述,但对于像我这样的初学者不要忘记包括返回。

$this->response->statusCode(200);
return $this->response;