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?

1

1 Answer 1

2

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 .