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:
If you paste the
https://api.stackexchange.com/2.2/answers?site=stackoverflow&sort=activity&
in the browser, it returns a valid JSON.