如何从 PHP 发送 HTTP 响应中的压缩 (ASCII) 数据
我正在编写一项将数据作为响应发送给客户端的服务。
我正在发送文本(PLAIN ASCII)数据,但我想首先压缩数据(使用任何标准压缩例程),然后使用 header() 函数等将压缩数据发送回服务器
。 PHP 新手,所以收集了数据后,我知道需要知道如何在 HTTP 响应中将(压缩的)数据发送给用户
这是我到目前为止所拥有的伪代码:
<?php
function createResponse($args)
{
if ( ($data = fetch_data($args)) !== NULL)
{
/* Assumption is that $data is a string consisting of comma separated
values (for the columns) and each "row" is separated by a CRLF
so $data looks like this:
18-Oct-2009, 100.2, -10.5, 15.66, 34.2, 99.2, 88.2, 'c', 'it1', 1000342\r\n
19-Oct-2009, -33.72, 47.5, 24.76, 8.2, 89.2, 88.2, 'c', 'it2', 304342\r\n
etc ..
*/
//1. OPTIONAL - need to encrypt $data here (how?)
//2. Need to compress (encrypted?) $data here (how?)
//3. Need to send HTTP Status 200 OK and mime/type (zip/compressed here)
//4. Need to send data
}
else
{
//5. Need to send HTTP error code (say 404) here
}
}
?>
任何有经验的 PHP 编码人员都可以告诉我吗上述 1 -5 使用哪些语句?
I am writing a service that sends data as a response, to a client.
I am sending text (PLAIN ASCII) data, but I want to compress the data first (uzing any of the standard compression routines), and then send the compressed data back to the server, using the header() function etc.
I am relatively new to PHP so having gathered the data, I know need to know how I can send the (compressed) data in an HTTP response to the user
This is the pseudocode of what I have so far:
<?php
function createResponse($args)
{
if ( ($data = fetch_data($args)) !== NULL)
{
/* Assumption is that $data is a string consisting of comma separated
values (for the columns) and each "row" is separated by a CRLF
so $data looks like this:
18-Oct-2009, 100.2, -10.5, 15.66, 34.2, 99.2, 88.2, 'c', 'it1', 1000342\r\n
19-Oct-2009, -33.72, 47.5, 24.76, 8.2, 89.2, 88.2, 'c', 'it2', 304342\r\n
etc ..
*/
//1. OPTIONAL - need to encrypt $data here (how?)
//2. Need to compress (encrypted?) $data here (how?)
//3. Need to send HTTP Status 200 OK and mime/type (zip/compressed here)
//4. Need to send data
}
else
{
//5. Need to send HTTP error code (say 404) here
}
}
?>
Can any experienced PHP coders out there tell me which statements to use for 1 -5 above ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 ob_gzhandler 对其进行压缩(这样您的浏览器就会解压缩或者您可以使用 ZipArchive 归档您的数据并将其像真正的 .zip 一样传输给用户-file - 你需要类似的东西:
小心,必须在任何输出之前发送标头。
You can compress it with ob_gzhandler (so your browser will decompress it by himself) or you can archive your data with ZipArchive and transfer it to user like a real .zip-file - you need something like:
Be careful, headers must be sent before any output.