Capture json from file using ob_start in php

199 Views Asked by At

I have a file that outputs JSON. I would like to capture this output in a second file. However, when I include the output file, nothing is getting captured and I'm wondering if ob_start does not work with JSON.

Output.php

The JSON output file outputs:

{"book":{"title":"Peter Pan","author":"JM Barrie"}}

The code that outputs it is:

echo json_encode(array('book'=>$book));

My code to capture this and print it from a second file is:

Bigpage.php

ob_start();
echo "got here"
include 'output.php';
$output = ob_get_clean();
echo $output; //prints "got here". Does not print out anything from output.php

What am I doing wrong? Alternatively, how should can I capture and print this output?

1

There are 1 best solutions below

1
Karol Oracz On

You have missing ; after echo "got here", idk if that's the case. Im doing everything as you and the result is :

got here{"book":{"book":{"title":"Peter Pan","author":"JM Barrie"}}}

Is it what you expect?