Add ob_start(); function from where you want to put output to a file
Use following function to get output of code execution :
$contents = ob_get_contents();
Put the content to a file:
file_put_contents(<FILE PATH WITH NAME>,$contents);
Clean the output with using ob_end_clean();
Example:
…..
ob_start();
print_r($data);
$contents = ob_get_contents();
file_put_contents(‘var/www/html/dumprequest.txt’,$contents);
ob_end_clean();