【问题标题】:Codeigniter Rest Server fail to output longer JSONCodeigniter Rest Server 无法输出更长的 JSON
【发布时间】:2015-02-15 13:59:23
【问题描述】:

有人知道为什么吗 $this-response($somearray, 200); 无法输出长 JSON? (或长数组) 例如,如果我将查询限制为最后 10 行,但如果我将其限制为 100,我可以输出数据库结果 JSON 被修剪,在我的客户端应用程序中,我得到“输入意外结束”。如果我简单地更改上述语句 回声 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);
    }
}

【问题讨论】:

  • 你的 php 代码在哪里?

标签: php codeigniter rest server


【解决方案1】:

好的,我找到了问题所在。 我只是注释掉了 REST_controller 中设置内容长度的代码。 现在它可以工作了,但我仍然想知道为什么 strlen($output) 对于较大的 JSON 文件的长度与输出 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));
    }

【讨论】:

    【解决方案2】:

    这对我有用,而且我有很多 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));
      }
      

    【讨论】:

    • 这也有效。但我需要的是使用 $this->response 方法,因为我真的很喜欢它的工作方式,除了它修剪更长的 JSON 并使其无效。
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 2013-07-14
      • 1970-01-01
      相关资源
      最近更新 更多