如何从 PHP 发送 HTTP 响应中的压缩 (ASCII) 数据

发布于 2024-08-10 21:43:24 字数 1115 浏览 4 评论 0原文

我正在编写一项将数据作为响应发送给客户端的服务。

我正在发送文本(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蹲在坟头点根烟 2024-08-17 21:43:24

您可以使用 ob_gzhandler 对其进行压缩(这样您的浏览器就会解压缩或者您可以使用 ZipArchive 归档您的数据并将其像真正的 .zip 一样传输给用户-file - 你需要类似的东西:

// Headers for an download:
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="yourarchive.zip"');
header('Content-Transfer-Encoding: binary');
// load the file to send:
readfile('yourarchive.zip');

小心,必须在任何输出之前发送标头。

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:

// Headers for an download:
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="yourarchive.zip"');
header('Content-Transfer-Encoding: binary');
// load the file to send:
readfile('yourarchive.zip');

Be careful, headers must be sent before any output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文