run command line program and enter password from php

I have a command line program which requires for the user to type a password in order to run. I want to run it and enter the password through a php script.

I have tried using proc_open, using the following code, but it does not work. It reads the input, but fwrite does not seem to do anything.

$cmd = "cmd /c command_line_program";

$descriptorspec = array(
    0 => array('pipe', 'r'),  //STDIN
    1 => array('pipe', 'w'),  //STDOUT
    2 => array('pipe', 'r'),  //STDERR
);
$process = proc_open(
    $cmd,
    $descriptorspec, $pipes, null, null);
if (is_resource($process)) {
    $buffer = "";
    // Read from the command's STDOUT until it's closed.
    while (!feof($pipes[1])) {
        $input = fread($pipes[1], 8192);
        $buffer .= $input;

        if (strpos($buffer, 'Password:') !== false){
            fwrite($pipes[0], "userpassn");
        }
    }
    proc_close($process);
} else {
    echo 'Not a resource';
}

What am I doing wrong? Is there some other solution except proc_open? I am using windows and php 7.2.