-1

So I recently installed an apache2 web server with php5, and when trying to output plain JSON, a page is generated already containing <html>, <head> and <body> tags, like so:

<html>
  <head></head>
  <body>
    { "text": "some text" }
  </body>
</html>

instead of giving me this:

{ "text": "some text" }

with my php code being:

<?php printf('{ "text": "some text" }'); ?>

I have tried looking online to find how to disable this with no result.
Any help is welcome.

LazyShpee
  • 82
  • 10
  • 1
    Where did you get that html code? I mean: Is that html what you get when you ctrl+U to check your code? Because maybe your browser is actually receiving the bare json code, and it's adding the html by himself. – Amarnasan Sep 28 '15 at 07:54
  • Yes it is automatically put there, and I have no idea where it come from. I have already tried disabling auto_prepend_file and auto_append_file in both the .htaccess and configuration. – LazyShpee Sep 28 '15 at 07:57
  • 2
    Check this out: http://stackoverflow.com/questions/4064444/returning-json-from-a-php-script – Amarnasan Sep 28 '15 at 07:59
  • Wow, I can't believe I didn't check with ctrl+U before... Because I'm actually trying to make a slack.com integration, and I wouldn't work properly, even when putting the example JSON response. Mind putting this into an answer so I can validate it ? – LazyShpee Sep 28 '15 at 08:01
  • You got it as an answer :-) – Amarnasan Sep 28 '15 at 08:04

2 Answers2

3

If you don't output some html code, there will be no html code. PHP or Apache don't render html on their own. Probably your browser add the html Tags, if you view your source in the dev tools.

But if you output some json data, you should also set the correct mime-type header like so:

header('Content-Type: application/json');

Doing this, your browser (or other clients) know, you send them some json strings.

Philipp
  • 14,913
  • 3
  • 26
  • 46
1

Where did you get that html code? I mean: Is that html what you get when you ctrl+U to check your code? Because maybe your browser is actually receiving the bare json code, and it's adding the html by himself.

Amarnasan
  • 12,454
  • 3
  • 30
  • 37