Strange JSON encoding using json_encode

I am using WordPress with the JSON API plugin (http://wordpress.org/extend/plugins/json-api/) to create responses to our other site.

I came across a really strange problem (we use PHP 5.3.6) when I pass the following array http://pastebin.com/xdfYjrvK to json_encode () it gives me this (with json content-type): http: // pastebin. com / T61XGPP5

So, shit at the beginning, in the above example it is 2609 and 0 at the end, it changes depending on the size of the response, more content → a higher six-digit number. It also appears only when the number of answers is “high enough,” so it works with small answers.

At first I thought it was a plugin, but it works locally (on two different Mac OS X computers), and we updated all the packages on VPS (Debian, Apache, Nginx, PHP) to the latest versions.

It is displayed only when sending a content type, and not when displaying the result of $ with plain text instead of the / json application:

$charset = get_option('blog_charset'); if (!headers_sent()) { header('HTTP/1.1 200 OK', true); header("Content-Type: application/json; charset=$charset", true); } echo $result; 

$ charset installs in utf-8.

The Google chrome console says: "The resource is interpreted as a Document, but is transmitted using an application such as MIME / json."

So, does anyone know what is going on here?

+6
source share
2 answers

It looks like coded coding ( http://en.wikipedia.org/wiki/Chunked_transfer_encoding ). Make sure your headers set Content-Length correctly in the response to make sure that you are not forcing the web server to use CTE.

+4
source

One of the requirements of json is that all the data you provide to it must be encoded in UTF-8 encoding. json_encode () does not do this automatically. So you can try running this array_map("utf8_encode", $array); before json_encode it.

Else ... It looks weird, so I just guess ...

0
source

Source: https://habr.com/ru/post/891552/


All Articles