views:

287

answers:

1

I am working with a client on getting a gzip from their webservice. I am able to get a response with my following call:

$response = $client->call('branchzipdata', $param);
$filename = "test.gzip";
if (!$handle = fopen($filename, 'a')) {
     echo "Cannot open file ($filename)";
     exit;
}

if (fwrite($handle, $response) === FALSE) {
    echo "Cannot write to file ($filename)";
    exit;
}

Now when I attempt to write that a file, such as 'test.gzip', I am unable to open it afterwards... most likely because I am doing something horrible wrong. Any insight would be appreciated.

EDIT:

For some reason I was saving the file as '.gzip' instead of '.gz'... So in order to have it work I now have:

$response = $client->call('call', $param);
$content = base64_decode($response);
$filename = "output_zip.gz";
if (!$handle = fopen($filename, 'w')) {
    echo "Cannot open file ($filename)";
    exit;
}

if (fwrite($handle, $content) === FALSE) {
  echo "Cannot write to file ($filename)";
  exit;
}
fclose($handle);
echo system("gzip -d $filename");
+1  A: 

(Edited based on the comments)

If the return value is base64-encoded, you need to base64-decode it before you write it to the file. Alternatively you could write it out to a file which you then base64-decode to another file before trying to open it, but that seems a bit pointless compared with just decoding it when you first get it.

Jon Skeet
Sorry, forgot to include that piece of code, but I am actually closing the handle. From the more I read I am thinking I am just approaching this in the wrong way. I have yet to deal with soap.
Thomas
What happens when you try to read the file? Can you actually open it, but it's not a gzip file? Is it base64-encoded by any chance?
Jon Skeet
I am no longer at work, but yes, it is base64-encoded. The part that confused me was do I decode it before I write the contents to the test.gzip or decoded the contents afterwards? I guess I am just confused when it comes to handling the response I get from the soap request. Thanks for your help!
Thomas
Base64-decode it before you write it to test.gzip.
Jon Skeet