views:

112

answers:

2

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 sepeated 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/compresed 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 ?

A: 

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.

silent
hello silent,thanks for your prompt response. This is almost what I am looking for - the only problem is that I will already have the data in memory, it would have been read from a database already, and I dont want to have to save it to a file first (disk I/O being a major bottleneck). So:How may I compress the data directly from the string data variable that I have in memory?Also:1. (I know this is optional), but how may I encrypt the $data variable (before compression?I think I know how to set the status code using the header() function - so that answers questions 3 and 5
skyeagle
Last but not the least, is there an explicit function/command to SEND the data I would have created?I am thinking the code would look something like this:$comp_data = some_compression_func($data)// do HTTP header stuff here ...echo $comp_data; // Hmm, not sure you can do this with zipped data?!return;
skyeagle
Last but not the least, it is not a browser that will be consuming the sent data, I am writing this as part of a RESTful service - and I will be using Curl and another language (C++ binding) on the client side.
skyeagle
You can use addFromString method (http://www.php.net/manual/en/function.ziparchive-addfromstring.php)
silent
A: 

A quick Google turned up this library for creating a zipped stream of data: http://pablotron.org/software/zipstream-php/

Hope that helps.

Matt Ellen