# 响应头
Every HTTP response has headers. These are metadata that describe the HTTP response but are not visible in the response’s body. The PSR-7 Response object provides several methods to inspect and manipulate its headers.
> 每个HTTP响应都有标头。这些元数据描述了HTTP响应,但在响应体中不可见。PSR-7响应对象提供了几个方法来检查和操作它的标头。
## 获取所有响应头 getHeaders
You can fetch all HTTP response headers as an associative array with the PSR-7 Response object’s`getHeaders()`method. The resultant associative array’s keys are the header names and its values are themselves a numeric array of string values for their respective header name.
> 您可以使用PSR-7响应对象的`getHeaders()`方法以关联数组的形式获取所有的HTTP响应报头。由此产生的关联数组的键是标题名,其值本身是各自标题名的字符串值的数字数组。
~~~php
$headers = $response->getHeaders();
foreach ($headers as $name => $values) {
echo $name . ": " . implode(", ", $values);
}
~~~
Figure 5: Fetch and iterate all HTTP response headers as an associative array.
## 获取其中一个响应头 getHeader
You can get a single header’s value(s) with the PSR-7 Response object’s`getHeader($name)`method. This returns an array of values for the given header name. Remember,*a single HTTP header may have more than one value!*
> 您可以使用PSR-7响应对象的`getHeader($name)`方法获得单个标题的值。这将返回给定标题名称的值数组。记住,*一个HTTP报头可能有多个值!*
~~~php
$headerValueArray = $response->getHeader('Vary');
~~~
Figure 6: Get values for a specific HTTP header.
You may also fetch a comma-separated string with all values for a given header with the PSR-7 Response object’s`getHeaderLine($name)`method. Unlike the`getHeader($name)`method, this method returns a comma-separated string.
> 您还可以使用PSR-7响应对象的`getHeaderLine($name)`方法获取一个以逗号分隔的字符串,其中包含给定标题的所有值。与`getHeader($name)`方法不同,该方法返回一个逗号分隔的字符串。
~~~php
$headerValueString = $response->getHeaderLine('Vary');
~~~
Figure 7: Get single header's values as comma-separated string.
## 检测响应头 hasHeader
You can test for the presence of a header with the PSR-7 Response object’s`hasHeader($name)`method.
> 您可以使用PSR-7响应对象的`hasHeader($name)`方法测试报头是否存在。
~~~php
if ($response->hasHeader('Vary')) {
// Do something
}
~~~
Figure 8: Detect presence of a specific HTTP header.
## 设置响应头 withHeader
You can set a header value with the PSR-7 Response object’s`withHeader($name, $value)`method.
> 您可以使用PSR-7响应对象的`withHeader($name, $value)`方法设置标题值。
~~~php
$newResponse = $oldResponse->withHeader('Content-type', 'application/json');
~~~
Figure 9: Set HTTP header
**Reminder**
The Response object is immutable. This method returns a*copy*of the Response object that has the new header value.**This method is destructive**, and it*replaces*existing header values already associated with the same header name.
> 提醒
> Response对象是不可变的。此方法返回具有新标头值的响应对象的**副本**。**这个方法是破坏性的**,它*替换*已经关联到相同标题名的现有标题值。
## 附加响应头 withAddedHeader
You can append a header value with the PSR-7 Response object’s`withAddedHeader($name, $value)`method.
> 您可以使用PSR-7响应对象的`withAddedHeader($name, $value)`方法附加一个标题值。
~~~php
$newResponse = $oldResponse->withAddedHeader('Allow', 'PUT');
~~~
Figure 10: Append HTTP header
**Reminder**
Unlike the`withHeader()`method, this method*appends*the new value to the set of values that already exist for the same header name. The Response object is immutable. This method returns a*copy*of the Response object that has the appended header value.
> 提醒
>
> 与` withHeader()`方法不同,此方法*将*新值附加到已经存在的同一标题名称的值集。Response对象是不可变的。此方法返回具有附加头值的响应对象的副本。
## 移除响应头 withoutHeader
You can remove a header with the Response object’s`withoutHeader($name)`method.
> 您可以使用响应对象的`withoutHeader($name)`方法删除标题。
~~~php
$newResponse = $oldResponse->withoutHeader('Allow');
~~~
Figure 11: Remove HTTP header
**Reminder**
The Response object is immutable. This method returns a*copy*of the Response object that has the appended header value.
> 提醒
>
> Response对象是不可变的。此方法返回具有附加头值的响应对象的副本。
- 开始
- 安装
- 升级指南
- Web服务器
- 概念
- 生命周期
- PSR 7
- 中间件
- 依赖容器
- 实例 及通知和警告处理
- Request
- 请求方法
- 请求头信息
- 请求主体
- 上传的文件
- 请求帮助
- 路由对象
- Response
- 响应状态
- 响应标头
- 响应体
- 返回JSON
- 视图模板
- 路由
- 创建路由
- 路由回调
- 路由策略
- 路线占位符
- 路由名
- 路由组
- 路由中间件
- 路由表达式缓存
- 容器识别解析
- 封装中间件
- 路由的中间件
- 错误处理中间件
- 方法重写的中间件
- 输出缓冲中间件
- 内容长度中间件
- 扩展功能
- 以 / 结尾的路由模式
- 获取当前路由
- 设置CORS
- 使用POST表单上传文件
- 第三方组件
- slim-session
- auth
- slim-api-skeleton
- dir