PHP json_encode 大小限制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6194393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:37:53  来源:igfitidea点击:

PHP json_encode size limit?

phpsizelimitjson

提问by patrick

I'm using a PHP proxy to get the contents of a file. I want to search through that file using the powerfull jQuery options, without having to write all kinds of queries in PHP. Here is my PHP code:

我正在使用 PHP 代理来获取文件的内容。我想使用强大的 jQuery 选项搜索该文件,而不必在 PHP 中编写各种查询。这是我的 PHP 代码:

$page = file_get_contents( filter_var( $_POST[url], FILTER_SANITIZE_URL ) );
die( json_encode( $page ) );

If the page loaded gets too big PHP will read the entire document, but json_encoding it will only give the first part of the file, not the entire file. I can't find anything about a size limit on json passed data, but apparently there is one.

如果加载的页面太大,PHP 将读取整个文档,但 json_encoding 只会给出文件的第一部分,而不是整个文件。我找不到任何关于 json 传递数据的大小限制的信息,但显然有一个。

the question: is there a workaround to prevent only part of the file being transfered?

问题:是否有一种解决方法可以防止仅传输部分文件?

I need to grab files from other domains, so reading the contents of a file in jQuery is not really an option.

我需要从其他域中获取文件,因此在 jQuery 中读取文件的内容并不是一个真正的选择。

回答by Cody Wikman

To help others who may be running into problems that they can't explain with json_encode. I've found it helps to know about the json error msg function.

帮助可能遇到无法用 json_encode 解释的问题的其他人。我发现了解 json error msg 函数会有所帮助。

json_last_error_msg();

I was having a similar problem but it wasn't related to file size. I had malformed utf-8 in the database. You can check your json like this

我遇到了类似的问题,但与文件大小无关。我在数据库中的 utf-8 格式错误。你可以像这样检查你的json

$json = json_encode($data);

if ($json)
    echo $json;
else
    echo json_last_error_msg();

PHP docs here json_last_error_msg

PHP 文档在这里json_last_error_msg

回答by mario

PHP 5.3: ext/json/json.c
PHP 7 (current): ext/json/json.c

PHP 5.3:ext/json/json.c
PHP 7(当前):ext/json/json.c

There is no built-in restriction to the size of JSON serialized data. Not for strings anyway. I would therefore assume you've run into PHPs memory limit or something.

JSON 序列化数据的大小没有内置限制。无论如何不适用于字符串。因此,我假设您遇到了 PHP 内存限制或其他问题。

json_encodeing a string consistently just adds some escaping and the outer double quotes. Internally that means a bit memory doubling (temporary string concatenation and utf8_to_utf16 conversion/check), so that I ran into my 32MB php memory limit with an 8MB string already. But other than that, there seem to be no arbitrary limits in json.c

json_encode始终使用字符串只会添加一些转义和外部双引号。在内部,这意味着内存加倍(临时字符串连接和 utf8_to_utf16 转换/检查),所以我已经用 8MB 字符串遇到了 32MB php 内存限制。但除此之外,似乎没有任意限制json.c

回答by yutaka

I resolved similar case. So, I write here.

我解决了类似的情况。所以,我写在这里。

My website also responded broken jsondata. It is about 8MB.

我的网站也响应了损坏的 jsondata。它大约是 8MB。

The cause was the storage has not free. The result of command of df was displayed "used 100%".

原因是存储空间不可用。df 命令的结果显示为“used 100%”。

I deleted some files. Then, resolved.

我删除了一些文件。然后,解决了。