1

I'm trying to implement this simple request in Drupal 7 on my local machine:

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

For your information:

From my side, the endpoint seems to work fine, as it sends a correct JSON result with all the users.

But I still can't recover the data because it's like $request->data appears like blank despite the "4468 characters". That's wierd.

enter image description here

While I'm supposed to get something like that, with a very beautiful JSON string:

enter image description here

If I try with another API, like Twitter for example, it works fine (sorry I put an old depreciated endpoint but it's not important, I get the data back anyway) :

enter image description here

Does someone have a clue about that strange issue? Thank you.

  • 1
    The API response is GZIPped. Use gzinflate, or whatever, on the response before trying to JSON decode it. – Brock Adams Nov 13 '14 at 14:48
  • Oh... well. I should have read the whole doc before asking :D Many thanks @BrockAdams – Djouuuuh Nov 13 '14 at 15:08
1

As @BrockAdams said, the string is compressed and need to be uncompress depending on the $request->headers.

$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);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .