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 使用哪些语句吗?

4

1 回答 1

0

您可以使用ob_gzhandler对其进行压缩(这样您的浏览器将自行解压缩),或者您可以使用ZipArchive归档您的数据并将其像真正的 .zip 文件一样传输给用户 - 您需要以下内容:

// 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');

请注意,标头必须在任何输出之前发送。

于 2009-11-06T15:59:39.373 回答