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 her开发者_开发技巧e)
//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 ?
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.
精彩评论