0

I'm trying to use the V2.2 of StackExchange api with PHP. I'm using the Symfony project with Kriswallsmith's buzz library.

The problem comes when I try to print the content of response of HTTP request and its encode. I have already read lots of question related with this problem in StackOverflow but the problem still exists.

This is a portion of code where I show the problem:

echo "The url: ";
var_dump($url);
var_dump($response);
$content = $response->getContent();
echo "Json decode's content: ";
var_dump(json_decode($content, true));
echo "The error is";
switch(json_last_error()) {
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
        break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' - Unexpected control character found';
        break;
    case JSON_ERROR_SYNTAX:
        echo ' - Syntax error, malformed JSON';
        break;
    case JSON_ERROR_STATE_MISMATCH:
        echo ' - Invalid or malformed JSON';
        break;
    case JSON_ERROR_UTF8:
        echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
        break;
}

die();

This returns the following:

enter image description here

If you paste the

https://api.stackexchange.com/2.2/answers?site=stackoverflow&sort=activity&

in the browser, it returns a valid JSON.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262

1 Answers1

1

It occurs to me that for some reason the content is not ungzipped:

$ curl https://api.stackexchange.com/2.2/answers
�VJ-*�/��LQ�210ЁrsS���S����3KR2��R
                                  K3�RS�`J�sA�I�)��E@NIj�R-g��PP

$ curl https://api.stackexchange.com/2.2/answers | gzip -d
{"error_id":400,"error_message":"site is required","error_name":"bad_parameter"}

You should be able to use PHP's gzuncompress function or dig Buzz a bit.

kix
  • 3,290
  • 27
  • 39
  • 1
    This is the reason but with `gzuncompress` function was still falling. This is the correct method/answer for my question. Thanks ;) http://stackoverflow.com/a/10105319/2359967 –  Sep 03 '14 at 15:55
  • @benatespina, well, actually an HTTP client should work out such technical things by itself: it can read the headers, can't it? – kix Sep 03 '14 at 16:23