J
J
jbeaver2014-03-11 13:49:28
PHP
jbeaver, 2014-03-11 13:49:28

How to solve json_encode output problem?

Hello.
I output JSON in PHP in this way:
echo json_encode($data, JSON_UNESCAPED_UNICODE);
Nothing extra before and after json_encode is output, without this line the server response is empty.
On the home server, everything works as it should, on the hosting, some garbage is added to the response, it looks something like this:

1221d
{"user":{ ... // тут собственно строка с JSON
0

//конец ответа

Moreover, if you output the result of json_encode to a file, then the file contains pure JSON without garbage:
file_put_contents(Yii::app()->basePath.'/logs/test.json', json_encode($data, JSON_UNESCAPED_UNICODE));

What could be the problem?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2014-03-11
Protko @Fesor

BOM header? Close the <?php?

F
frees2, 2014-03-11
@frees2

$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);

M
Max, 2014-03-11
@7workers

I have seen self-written json_encode in some "frameworks", like this:
if( !function_exists("json_encode") ) {
function json_encode() .....
}
perhaps the module is not installed on the hosting and a "self-made" function is used.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question