How to properly "passthrough" a CLI program to a PHP script *while executing your own function*? (My old method has broken.)

127 views Asked by At

Up until a few days ago, I had this working. It had been working for a long time. I'm not sure if it was because of upgrading PHP 8.0.12 to 8.0.13, or because yt-dlp updated and has changed something since. I have not changed my code.

This works:

passthru($yt_dlp_command);

However, it doesn't let me run my own function while it's executing, which is the whole point.

This does no longer work, but worked up until recently:

ob_start();
$a = popen($yt_dlp_command, 'r');

if ($a)
{
    $response = fgets($a, 10); // I've tried numerous other values than 10 as well.

    if ($response)
    {
        while ($row_data = $response)
        {
            ob_flush();
            flush();
            my_function();
            echo $row_data;
        }
    }

    pclose($a);
}

ob_end_clean();

No matter what options I try for yt-dlp, such as --no-progress or --newline (or combinations of these, or none), it just won't show anything but the first line, or the beginning of the first line repeated endlessly in a broken manner.

If I run the $yt_dlp_command in a cmd.exe, it works perfectly with full animated output and all that.

I'm completely stuck now. No idea what else to try. It's probably yt-dlp that has changed something in how it outputs stuff, but even if that's the case, it's weird to me how certain programs can just break this code. It should work regardless of what specific program is being run.

1

There are 1 answers

2
Josiah Hudson On

After playing with your script a bit, I realized that the condition on your while-loop is never updated, so it loops endlessly with the first bit of data returned. At the very least I would recommend adding another fgets() call at the bottom of your while-loop to grab the next bit of data from the forked process call.