简体   繁体   English

如何确保我的服务器响应具有PHP的Content-Length标头?

[英]How to ensure that my response from server has `Content-Length` header with PHP?

This is how I response from my server: 这是我从服务器响应的方式:

include('../connection.php');
connection();
mysql_query('SET NAMES utf8');

$response = array();
$words = array();


$query = "SELECT * FROM `$table` ORDER BY `created_at` DESC";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {

    $row['id'] = intval($row['id']);
    array_push($words, $row);
}

$response['words'] = $words;

echo json_encode($response);

But I really need here Content-Length header. 但是我这里确实需要Content-Length标头。 How can I check or add/create such header? 如何检查或添加/创建此类标题?

PHP handles this for you if you enable output buffeting. 如果您启用输出自助餐,PHP会为您处理。 When you write to the output stream (via the buffer) PHP knows how much you've written and sets the header value before sending the response body. 当您通过缓冲区写入输出流时,PHP知道您已写入多少内容,并在发送响应正文之前设置标头值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM