2

I am trying to get the contents from a gzipped file that is returned to me after using Mailchimp API doing a batch operation request. I expect to get only a JSON string as response, but also receive a bunch of numbers and random (?) strings.

This is what I do.

$gz = gzopen($response->response_body_url, "r");
$contents = trim(gzread($gz, 10000));
print_r($contents); //see output below
gzclose($gz);

This is what is returned to me.

0000777000000000000000000000000012705141572007721 5ustar rootroot./05fa27ceab.json0000666000000000000000000000121212705141572012327 0ustar rootroot[{"status_code":400,"operation_id":null,"response":"{\"type\":\"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/\",\"title\":\"Member Exists\",\"status\":400,\"detail\":\"xxxx.xxxx@xxxx.xx is already a list member. Use PUT to insert or update list members.\",\"instance\":\"\"}"},{"status_code":400,"operation_id":null,"response":"{\"type\":\"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/\",\"title\":\"Member Exists\",\"status\":400,\"detail\":\"xxxx2.xxxx2@xxxx2.xx is already a list member. Use PUT to insert or update list members.\",\"instance\":\"\"}"}]

What am I missing here? Why won't it work?

faerin
  • 1,915
  • 17
  • 31
  • is it really a gzipped file or is it merely the http response being deflated? – e4c5 Apr 18 '16 at 13:58
  • Well, this is actually pretty much all I do. The `$response->response_body_url` contains the URL to where the zip is located. Downloading and opening it from my browser works fine. I tried a bunch of different approaches. This one seems to be the closest as the actual JSON string is in there. Unfortunately I am not able to tell you if it's deflated or not...? – faerin Apr 18 '16 at 14:03
  • Can you post a link to the file if possible? – e4c5 Apr 18 '16 at 14:06
  • Trust me, I would if I could. But it contains some info I'm not sure I'm supposed to share here... :/ Sorry to be so unhelpful. I can't seem to find anything about anyone having a similar problem either :/ – faerin Apr 18 '16 at 14:16
  • Would a somewhat edited content of the downloaded file do? – faerin Apr 18 '16 at 14:21
  • probably not, since the problem appears to be with the gzip decoding. – e4c5 Apr 18 '16 at 14:26
  • Can you share sample gzipped content for inspect the issue? I guess you can download file_put_contents(somewhere,$response->response_body_url) – merdincz Apr 18 '16 at 14:36

1 Answers1

0

It looks like you may be dealing with a .tar.gz file instead of just gzip. The easiest way to do that is either with the PharData extension or by just saving it to disk and using a shell tool to unzip.

Here's an answer to a question on how to deal with .tar.gz files in php

Community
  • 1
  • 1
TooMuchPete
  • 4,583
  • 2
  • 17
  • 21