Slim icon indicating copy to clipboard operation
Slim copied to clipboard

truncate response issue

Open jimsproject opened this issue 10 years ago • 14 comments

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

jimsproject avatar Jan 26 '16 13:01 jimsproject

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.

geggleto avatar Jan 26 '16 13:01 geggleto

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.

jimsproject avatar Jan 26 '16 14:01 jimsproject

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.

geggleto avatar Jan 26 '16 14:01 geggleto

so is it a bug? why the response behave like that?

jimsproject avatar Jan 26 '16 15:01 jimsproject

It is not a bug. This is defined PHP behaviour.

geggleto avatar Jan 26 '16 16:01 geggleto

ok , thank you

jimsproject avatar Jan 27 '16 06:01 jimsproject

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

jimsproject avatar Jan 27 '16 13:01 jimsproject

I cannot help you. This is all php behaviour.

geggleto avatar Jan 27 '16 13:01 geggleto

What is your database set too, UTF-8, or something else?

matthewtrask avatar Jan 27 '16 13:01 matthewtrask

You may find that setting the outputBuffering setting to false fixes it:

$config = ['settings' => [
    'outputBuffering' => false,
]];
$app = new \Slim\App($config);

akrabat avatar Jan 27 '16 19:01 akrabat

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.

akrabat avatar Jan 27 '16 19:01 akrabat

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.

jimsproject avatar Jan 28 '16 05:01 jimsproject

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.

jimsproject avatar Jan 28 '16 12:01 jimsproject

Well done on finding it!

akrabat avatar Jan 28 '16 12:01 akrabat