# 響應頭
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