7

I try to print a big JSON block (100k) to the browser, but the server fails without an error.

For example:

echo 'var config = ' . json_encode( $config ) . ';' . PHP_EOL;

I Have found that if i send a small piece, it's OK. I have found that if I put line breaks in the JSON string, it's OK even if the string is 400k.

For example:

$config_json = json_encode( $config ); $config_json = str_replace( '},', '},' . PHP_EOL, $config_json ); echo 'var config = ' . $config_json . ';' . PHP_EOL;

But the breaklines breaks my JSON.

So, if it's a buffer setting, why the PHP_EOL helps?

I have tried also to split the JSON to pieces like here: https://stackoverflow.com/a/19156563/1009525, But without success, Only the breaklines helps me.

Community
  • 1
  • 1
Mati Horovitz
  • 131
  • 1
  • 5
  • is your json valid? have you validated using a online validator tool perhaps? – coderodour Apr 05 '17 at 16:23
  • Yes. But it's not a JSON problem, it's fails for each large string. – Mati Horovitz Apr 06 '17 at 10:35
  • Have you tried disabling buffering? http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_buffering ? If that works, you can do it just for this one response: "Buffering can also be enabled or disabled by passing “yes” or “no” in the “X-Accel-Buffering” response header field." – Robbie Apr 06 '17 at 23:59
  • Is the string displayed in a
     html tag ? If yes, i know there can be some errors when printing large strings
    – Unex Apr 07 '17 at 15:16
  • Any error you get when you try to print the json like **503 server capacity problem** or anything like that. If nothing is showing then enable `ini_set('error_reporting', 32767);` and check again – Haridarshan Apr 09 '17 at 07:31
  • What error do you see? Is there anything in the nginx error log? – Rich Apr 09 '17 at 11:57
  • Check this link maybe helps [http://stackoverflow.com/questions/2392766/multiline-strings-in-json](http://stackoverflow.com/questions/2392766/multiline-strings-in-json) – Marios Nikolaou Apr 12 '17 at 07:58
  • have you tried looping through your `$config` variable and building json string like this `$str = '{'; $str .= '{ id:'. $value['id'].'}'; ... $str .= '}';` – MakeLoveNotWar Apr 12 '17 at 08:16

3 Answers3

1

As you write

the server fails without an error

I presume you mean that the server sends a response to the client (status code: 200 - no error), but the response body (the content) is empty (this is the failure).

You should check this because if actually the server sends a response with content then the issue is not with php, nginx or buffering.

Otherwise (as suggested in comments) maybe the JSON instead of inside a <script> - </script> block may be wrapped between <pre> tags and this could be the problem (but I can't help unless you post more of your code).


From now on I assume the response sent from the server is empty

The code you posted is valid and is supposed to handle correctly the output string you're building up (that's far below PHP limits).

Said that it seems a weird buffering issue. I write "weird" because as far as I know (and I took time to do some research too) buffering should not be influenced by line breaks.


I have found that if I put line breaks in the JSON string, it's OK even if the string is 400k.

A quick workaround to solve your problem is to output a valid JSON with line breaks. You just need to specify an option to json_encode:

echo 'var config = ' . json_encode( $config, JSON_PRETTY_PRINT ) . ';' . PHP_EOL;

JSON_PRETTY_PRINT tells json_encode to format the json to be more readable and doing so will add line breaks.

(Note that this option is available for PHP 5.4.0 and above)


I hope the above solution works for you.

Anyway I strongly suggest you to investigate further the issue in order to let the original code too to work.

First you should ensure you're running a recent and stable version of both nginx and php.

Then I would check nginx configuration file, php-fpm configuration (if you're using php-fpm) and finally php configuration.

Also check php, nginx, and php-fpm error logs.

Paolo
  • 15,233
  • 27
  • 70
  • 91
  • JSON_PRETTY_PRINT is available on PHP versions > 5.4.0, please add a small comment on your answer on that if you want. –  Apr 12 '17 at 13:34
0

try using php heredoc for echoing http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Sandeep Kothari
  • 405
  • 3
  • 6
0

In case you don't have PHP version > 5.4.0 installed on your server a quick workaround could be something like this. The below snippet works for a test array. Initial test was with an array of 250Kb. Since i can't post the actual test array here is a test link with a smaller example. It is as the result of JSON_PRETTY_PRINT though.

$out = json_encode($arr,JSON_FORCE_OBJECT);
$out = str_replace( ':{', ':' . PHP_EOL . '    ' . '{', $out );
$out = str_replace( '},', PHP_EOL . '    },', $out );
$out = str_replace( ',', ',' . PHP_EOL  . '     ', $out );
$out = str_replace( '},' . PHP_EOL . '     ', '},' . PHP_EOL . ' ', $out );
$out = str_replace( '}}', PHP_EOL . '    }' . PHP_EOL . '}', $out );

echo $out;