truncate response issue
i'm create webservice using slim3, the issue is when output_buffer enable with def 4096 value or On the json response at the end truncate some character. and make the json invalid.
code: $app->get('/am/', function (ServerRequestInterface $request, ResponseInterface $response) use ($dbh, $app) { $response = $response->withHeader('Content-type', 'application/json'); $obj = new account_manager($dbh, $app); $result = $obj->get(); $response->write(json_encode($result)); return $response; });
thank you jimmy
Please provide more information about what you are trying to do.
You are modifying your php environment, so can you please tell us what you are doing and specifically the code you are using to do it, so that we can test it locally.
Thank you.
im trying to return record from my postgresql (using PDO) . when the php enveiroment still default(ouput_bufffer=4096) i get ouput: {"status":1,"total":23,"limit":2,"page":1,"data":[{"id":"32020b3bed87475793e48925ba001a4f","first_name":"jimbo","last_name":"carter","country_id":"ID","address":"kemang timur","phone":"+628384848","currency":"IDR","active":true},{"id":"37475d5ae45f4e448fd8c62fa260c8ee","first_name":"jimbo","last_name":"carter","country_id":"ID","address":"kemang timur","phone":"+628384848","currency":"IDR","active":tru
the ouput is missing the some character. but when i disable then output_buffer it give normal output.
You will need to flush the buffer manually via ob_flush() or set the php directive implicit_flush to true. http://php.net/manual/en/outcontrol.configuration.php#ini.output-buffering
I hope that helps.
so is it a bug? why the response behave like that?
It is not a bug. This is defined PHP behaviour.
ok , thank you
Hi Geggleto, i have try to flush manual. and is not truncate now. i have another issue when i use flush . i can't set the http response code. thank you
I cannot help you. This is all php behaviour.
What is your database set too, UTF-8, or something else?
You may find that setting the outputBuffering setting to false fixes it:
$config = ['settings' => [
'outputBuffering' => false,
]];
$app = new \Slim\App($config);
I think @rocketpastsix may be onto something too. A possible reason that it's truncating is the the size of the body is incorrect.
Try:
$response = $response->addHeaderLine('Content-Length', $length);
Where $length is the correct length of the json encoded string.
i'm using utf-8.right now solving by forcing flush() before return the response. but for the header response code i must using http_response_code(). @akrabat I have try set output buffer false in php.ini itu solve the truncate but i can't set the response code. i thought the issue also the mismatch count the body length. in function read class stream. i will try to set the content length manual.
i have find the cause of this problem, i have include php class that '<?php' not at the begining file. there is 3 white space before that tag. and it cause the output truncate.
Well done on finding it!