Codeigniter Rest 服务器无法输出更长的时间 JSON

Codeigniter Rest Server fail to output longer JSON

有谁知道为什么 $this-response($somearray, 200); 不能输出长JSON? (或长数组) 例如,如果我将查询限制为最后 10 行,但如果我将其限制为 100,我可以输出数据库结果 JSON 得到修剪,在我的客户端应用程序中我得到 'unexpected end of input'。如果我简单地更改上面的语句 echo json_encode($somearray); 它运行完美。

对不起。我需要澄清我的问题。我在用 https://github.com/chriskacerguis/codeigniter-restserver

function transits_get()
{
    $sessionId = $this->input->get_request_header('Sessionid', TRUE);
    $transits = $this->Transit_model->get_transits( $sessionId );

    if($transits)
    {
        //$this->output->set_content_type('application/json');
        //$this->output->set_output(json_encode($transits)); //works
        //echo json_encode($transits); //works
        $this->response($transits, 200); // 200 being the HTTP response code //Does not work

    }

    else
    {
        $this->response(array('error' => 'There is no transit records in the database!'), 404);
    }
}

这对我有用,我有很多 JSON 数据(+- 250 条记录):

$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($data));

this article 中所述,您可以执行以下操作:

  1. 打开REST_Controller.php

  2. 找到并删除以下代码(在响应函数的末尾):

    if ( ! $this->_zlib_oc && ! $CFG->item('compress_output')) {
        header('Content-Length: ' . strlen($output));
    }
    

好的,我找到问题所在了。 我只是把REST_controller中设置内容长度的代码注释掉了。 现在它可以工作了,但我仍然想知道为什么 strlen($output) 对于较大的 JSON 文件与 output JSON 的长度不同,而对于较短的文件则没问题。

// If zlib.output_compression is enabled it will compress the output,
    // but it will not modify the content-length header to compensate for
    // the reduction, causing the browser to hang waiting for more data.
    // We'll just skip content-length in those cases.
    if ( ! $this->_zlib_oc && ! $this->config->item('compress_output')) {
        //header('Content-Length: ' . strlen($output));
    }

在库文件夹中,行号。 REST_Controller.php 文件的 267,注释以下代码。它正在工作。

header('Content-Length: ' . strlen($output));