douzhuan1432 2013-04-08 09:50
浏览 123
已采纳

PHP:长字符串,json_encode,var_dump和echo

I noticed that the application I was developing was breaking, I managed to narrow it down to this problem (I have set up a test case for it):

ini_set("display_errors", "1");
error_reporting(E_ALL);

error_log("[test] Memory limit: " . ini_get('memory_limit'));
error_log("[test] Max Execution Time: " . ini_get('max_execution_time'));

$testArray = array("abcdefghijklmnotuv..... 786998 characters later...ijklmEND");
$json = json_encode($testArray);

var_dump($json);
// echo($json);

error_log("[test] Memory: ".memory_get_usage()."B");

When I var_dump the variable $json, I get the correct output:

string(786998) "["abcdefghijklm....ijklmEND"]"

When I echo the variable $json, you can very briefly see something appear on the screen but then it disappears, the end response seems to be NULL.

If I repeat the above with a string instead of an array the same thing happens.

If I repeat the above with a string AND omit the json_encode step everything behaves as expected, the results of var_dump and echo are correct.

During the entire process there are no errors output in the error log, my memory limits and max execution times are also okay:

[test] Memory limit: 256M
[test] Max Execution Time: 30
[test] Memory: 2134296B

Any ideas?

A little information on my application:

In a nutshell there are two servers. Server A sends a HTTP request to server B, server B processes the request and sends the response back to server A. The response is always a JSON encoded array. If one of the values of the array in the response is too long server A receives a NULL response.

  • 写回答

1条回答 默认 最新

  • douluxia0606 2013-04-09 02:52
    关注

    It turns out the problem wasn't to do with server B, it was server A.

    Server A is implemented in Java using the Netty API.

    The HTTP response was too long and it was being chunked and I didn't have a HttpChunkAggregator in my pipeline, once I added it in everything was fine.

    ClientBootstrap cb = new ClientBootstrap(cf);
    
    cb.getPipeline().addLast("codec", new HttpClientCodec());
    cb.getPipeline().addLast("chunkaggregator", new HttpChunkAggregator(1048576));
    cb.getPipeline().addLast("inflater", new HttpContentDecompressor());
    cb.getPipeline().addLast("handler", new OutboundHandler());
    

    I did check the browser source externally and the code was there, thank you for your comments they did lead me in the right direction!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么