在标题内使用函数(“HTTP / 1.1”) [英] Using a function inside header("HTTP/1.1")

查看:52
本文介绍了在标题内使用函数(“HTTP / 1.1”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现一个PHP函数,该函数返回一个HTTP状态代码以及

中的自定义消息,该消息是对客户端的响应的标题和正文。所有

后续部分都需要此功能,并应在发生错误时使用(无效输入,错误请求

类型等)。



该函数必须设置相应的HTTP标头并以JSON格式在

响应体中提供消息。



一个好的函数定义看起来像:send_error($ error_code,$ error_message)

该函数应该有2个输入参数:

(整数)$ error_code将是要返回的HTTP状态代码。

(字符串)$ error_message将是一个自定义消息文本



代码变量包含一个值这也是提供的$ responses数组中的关键。

这个数组使用适当的原因文本映射整数代码。一定要使用这个

数组!您可以从

Implement a PHP function that returns a HTTP status code along with a custom message in
the header and body of the response to the client. This function will be needed for all
subsequent sections, and should be used when an error occurs (invalid input, wrong request
type etc.).

The function must set the appropriate HTTP header AND provide the message in the
response body in JSON format.

A good function definition would look like: send_error($error_code, $error_message)
The function should take 2 input parameters:
(Integer) $error_code will be the HTTP status code to return.
(String) $error_message will be a custom message text

The code variable contains a value which is also key in the $responses array provided.
This array maps integer codes with their appropriate reason text. Make sure to use this
array! You can get the server protocol from the

$_SERVER

超全局获取服务器协议。

标题应为以下格式:

superglobal.
The header should be of the form:

$PROTOCOL $CODE - $REASON



响应正文应包含以下形式的有效JSON对象:


The response body should contain a valid JSON object of the form:

{
"error": "$CODE - $REASON: $MESSAGE"
}



其中:


Where:

$PROTOCOL = HTTP Protocol used (e.g. HTTP/1.1)
$CODE = HTTP Status Code (e.g. 400)
$REASON = HTTP Status Code Reason (e.g. "Bad Request")
$MESSAGE = Custom message (e.g. "Age must be between 13-130")

示例标题:

HTTP/1.1 400 - Bad Request: Age must be between 13-130



示例正文:


Example Body:

{
"error": 400 - Bad Request: Age must be between 13-130"
}



注意:您不能在PHP中使用


NOTE: You cannot use the

http_response_code()

函数,因为它没有

允许自定义原因消息。



我尝试了什么:



我'尝试创建一个函数



function in PHP for this as it does not
allow for custom reason messages.

What I have tried:

I've tried creating a function

function send_error($error_code, $error_message){
        header("HTTP/1.1". $error_code . $error_message);
}



我认为这是错误的方法,因为我不断收到内部服务器错误。我只是不知道该去哪里/从这里做什么


I think this is the wrong approach though as i keep getting an internal server error. I just don't know where to go/what to do from here

推荐答案

error_code,
error_code,


error_message)

该函数应该有2个输入参数:

(整数)
error_message)
The function should take 2 input parameters:
(Integer)


error_code将是要返回的HTTP状态代码。

(字符串)
error_code will be the HTTP status code to return.
(String)


这篇关于在标题内使用函数(“HTTP / 1.1”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆