php的http响应,在PHP中发送HTTP响应代码的最佳方式

为了回答你关于有什么区别的问题,我在PHP文档中找到了

this comment(感谢Steven):

http_response_code is basically a shorthand way of writing a http

status header, with the added bonus that PHP will work out a suitable

Reason Phrase to provide by matching your response code to one of the

values in an enumeration it maintains within

php-src/main/http_status_codes.h. Note that this means your response

code must match a response code that PHP knows about. You can’t create

your own response codes using this method, however you can using the

header method.

In summary – The differences between http_response_code and header

for setting response codes:

Using http_response_code will cause PHP to match and apply a Reason Phrase from a list of Reason Phrases that are hard-coded into

the PHP source code.

Because of point 1 above, if you use http_response_code you must set a code that PHP knows about. You can’t set your own custom code,

however you can set a custom code (and Reason Phrase) if you use the

header method.

我很好奇一些流行的框架如何在标准响应中发送标头:

// status

header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText), true, $this->statusCode);

public function renderStatusLine()

{

$status = sprintf(

'HTTP/%s %d %s',

$this->getVersion(),

$this->getStatusCode(),

$this->getReasonPhrase()

);

return trim($status);

}

Yii也是如此

protected function sendHeaders()

{

if (headers_sent()) {

return;

}

$statusCode = $this->getStatusCode();

header("HTTP/{$this->version} $statusCode {$this->statusText}");

// ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值