2

I seem to be confused about PHP output buffering. I have code like this:

function return_json($obj) {
  ob_get_clean();
  ob_start();
  header("Content-Type: application/json");
  echo json_encode($obj);
  exit;
}

But it doesn't seem to like the ob_get_clean(). I do that because some HTML might accidentally get generated before it gets to that point but I thought this was how you were meant to do it.

What am I missing?

3
  • So what exactly is going wrong? What do you mean by "it doesn't seem to like the ob_get_clean()"? Does it say headers already sent? If you do not get the JSON output, you should remove the ob_start() within your function.
    – GrGr
    Commented Mar 18, 2009 at 12:39
  • My JSON calls are erroring. It looks like there is no return payload.
    – Jordie
    Commented Mar 18, 2009 at 12:45
  • If I get it right, you do not know what the output of the script is? Do you use FireBug? Just for testing: replace the Content-Type with some HTMLish stuff and call the script directly.
    – GrGr
    Commented Mar 18, 2009 at 13:01

6 Answers 6

9

To use ob_get_clean (), you have to be sure, that at some point you have ob_start ()'ed earlier. Otherwise, there’s no buffer to clean, everything is already flushed to the user agent.

7

Use the ob_get_level() function to see if an output buffer is active and quit it:

while (ob_get_level()) {
    ob_end_clean();
}
0
3

you have to do an ob_start before all your code to catch any output before that function is called

2
  • I do ob_start() at the start of my script
    – Jordie
    Commented Mar 18, 2009 at 12:28
  • You need to have it before ob_get_clean()
    – Johan
    Commented Mar 18, 2009 at 12:45
1

If you just want to clean the buffer after starting output buffering with

ob_start() 

use

ob_clean()

Also be aware that nothing is already being flushed with functions like echo, print_r, etc. So the first thing in your script should be ob_start(). Be sure your includes do not already send something to the browser.

0

ob_start needs to be called before any content is generated. Normal usage would be something like:

ob_start();
# generated content here

$content = ob_get_contents(); # $content now contains anything that has been output already
ob_end_clean();

# generate any headers you need
echo $content;
0

If the problem you are having is that nothing is going to output, you seem to be missing the flush method? Also, ob_end_clean() can only be called after output buffering has been started, otherwise it returns 'false'. You can't use the ob_ methods to clean up any existing headers that have already been issued, you need to make sure of that yourself.

function return_json($obj) {
  ob_start();
  header("Content-Type: application/json");
  echo json_encode($obj);
  ob_end_flush();
  exit;
}
1
  • This does not seem to be the problem, I tested unknown (yahoo)'s code and PHP flushes on exit just fine. Unfortunately he does not tell us, what his output is.
    – GrGr
    Commented Mar 18, 2009 at 15:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.