Search code examples
phppraat

PHP Shell_exec isn't capturing the full output/is not waiting for the command to finish


I'm trying to call a Praat script from the command line using PHP (shell_exec command).

However, it appears that it isn't waiting for the script to finish before it returns. For example, when my "hello world" script is called, $output_string only contains "h".

$command_string runs perfectly when copy-pasted into the terminal directly. When var_dump($output_string) is run, it correctly shows the required output, however error_log still only prints "h"

an attempt at a minimally reproducible example:

Praat script:

writeInfoLine:"hello world"

PHP code:

$output_string = shell_exec("\"path_to_praat.exe\" --run \"path_to_script\"");
error_log($output_string);

Solution

  • shell_exec() was returning the correct output. However, the way that Praat was outputting text was incompatible with how PHP was processing it.

    I found out that the text was being weird when I converted it to json, and effectively saw "\u0000h\u0000e\u0000l\u0000l\u00000o ...." printed out.

    Once I cleaned $output string with

    $output_string = str_replace("\0", "", $output_string);
    

    Everything worked perfectly