6

I'm using WordPress together with the JSON API Plugin (http://wordpress.org/extend/plugins/json-api/) to generate responses to our other site.

I've hit a really weird problem (we're using 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 the crap in the beginning, in the above example it's 2609 and 0 in the end, it changes depending on the size of the response, more content -> higher hex number. It also appears only when the amount of response is "high enough", so it works on small responses.

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

It's only displayed when sending the content-type, not when outputting the $result with plain text instead of application/json:

$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 is set to utf-8.

The Google chrome console says: "Resource interpreted as Document but transferred with MIME type application/json."

So, does anyone have a clue whats happening here?

2
  • Where is $result initialized? Is it a json_encode-d value? Commented Jun 28, 2011 at 14:06
  • Yes, it comes from another function that takes care of the json_encoding with the built in. Commented Jun 28, 2011 at 14:09

2 Answers 2

4

This looks like chunked encoding (http://en.wikipedia.org/wiki/Chunked_transfer_encoding). Make sure to check that your headers are setting the Content-Length properly in the response to make sure you aren't forcing the web server to use CTE.

Sign up to request clarification or add additional context in comments.

2 Comments

My headers were: HTTP/1.1 200 OK Server: nginx/0.6.32 Date: Wed, 29 Jun 2011 06:25:53 GMT Content-Type: application/json; charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive X-Powered-By: PHP/5.2.6-1+lenny10 Setting the content-length worked like a charm, thanks!
had the same problem, with nginx aswell.. thanks a lot this saved me a lot of trouble
0

One requirement json have is that all data you give to it must be UTF-8 encoded. json_encode() does not do this automaticly. So you can try to run this array_map("utf8_encode", $array); before you json_encode it.

Else... It looks weird, so Im just guessing...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.