Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP json_encode size limit?

Tags:

json

php

size

limit

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:

$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.

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.

like image 622
patrick Avatar asked May 31 '11 22:05

patrick


People also ask

What does json_encode return?

Syntax. The json_encode() function can return a string containing the JSON representation of supplied value. The encoding is affected by supplied options, and additionally, the encoding of float values depends on the value of serialize_precision.

What does the PHP function json_encode () do?

The json_encode() function is used to encode a value to JSON format.

How can I get JSON encoded data in PHP?

To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.

What is json_encode and Json_decode in PHP?

JSON data structures are very similar to PHP arrays. PHP has built-in functions to encode and decode JSON data. These functions are json_encode() and json_decode() , respectively. Both functions only works with UTF-8 encoded string data.


2 Answers

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_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

$json = json_encode($data);

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

PHP docs here json_last_error_msg

like image 186
Cody Wikman Avatar answered Oct 23 '22 01:10

Cody Wikman


PHP 5.3: ext/json/json.c
PHP 7 (current): 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_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

like image 12
mario Avatar answered Oct 23 '22 03:10

mario