0

I'm trying to call the Stack Exchange API but it sends compressed JSON strings and I can't uncompress it.

$request = drupal_http_request('https://api.stackexchange.com/2.2/users?order=desc&sort=reputation&site=stackoverflow');
$uncompressed = gzinflate($request->data);
$json_response = drupal_json_decode($uncompressed);

I'm getting an error like this: Warning: gzinflate(): data error in adventure_profile_profile() (line 104 of /home/justine/www/adventure/sites/all/modules/custom/adventure_profile.module). (where line 104 is $uncompressed = gzinflate($request->data);).

Am I doing something wrong?

1

Ok, I'm sorry I just figure it out. I wasn't looking at the $request->headers to see what method to uncompress I have to use. Here is the solution:

$request = drupal_http_request('https://api.stackexchange.com/2.2/users?order=desc&sort=reputation&site=stackoverflow');

if (isset($request->headers['content-encoding'])) {
  if ($request->headers['content-encoding'] == 'gzip') {
    $request->data = gzinflate(substr($request->data, 10));
  }
  elseif ($request->headers['content-encoding'] == 'deflate') {
    $request->data = gzinflate($request->data);
  }
}

$json_response = drupal_json_decode($request->data);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.