1

After adding PHP header("Content-type: text/plain"); a space is added. I use it for pretty JSON.

I need this:

{
     "code":"200" }
}

But I get this:

 {
     "code":"200" }
}

4
  • 3
    Very, very hard for folks to help you if you don't quote more of your code. Commented Nov 19, 2012 at 18:06
  • @ user: The edit is great, but as the important thing is where your <?php ... ?> tags are, we need to see the full page (as you clearly have a nice, self-contained example there). Commented Nov 19, 2012 at 18:16
  • As far as I can tell, the only difference between those two blocks of code is a single space before the first {... is that what the source of concern is? Also - do you realize that what you've put out here is invalid JSON to begin with? Commented Nov 19, 2012 at 18:25
  • You must add more detail from your code, If you really want people to help out Commented Nov 19, 2012 at 18:26

2 Answers 2

3

Look at your PHP page closely, you probably just have whitespace in it (outside of your PHP tags) that you don't realize. (And you have an extra } in your JSON...)

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

4 Comments

First i have two spaces, when I checked it, I have removed one and now this space, because Content-type is setted, but I need it.
In general, ensure <?php is first thing in your php file and remove ?>.
@user1821941: The point is that every character outside of <?php ... ?> blocks is sent to the client. So you need to make sure there are no characters outside of those blocks. At all.
Thanks, really stupid bug. In fuctions I have missed to remove a space. Thanks!
0

If you're using PHP 5.4 or greater you can use JSON_PRETTY_PRINT flag.

However I also think that you might have a whitespace before the print, if not in your main page, maybe in functions.php.

2 Comments

I have updated php5 yesterday, but this JSON_PRETTY_PRINT flag isn't work and I use function.
You should check via phpinfo() your PHP version. That flag works with PHP 5.4 or greater.

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.