Run command as sudo with php exec with provided password

I have a small php website and want to run a script via button press. On the website I have an input for a password and I want to use that password for sudo authentification. (I don’t want to change permissions somewhere to make this work. I just want to use plain sudo auth.)

I have this php script:

exec('echo "' . $request['password'] . '" | sudo -S /usr/local/bin/mycommand  2>&1', $output, $retval);

But this doesn’t seem to work. I can run this script without problems via console but just not via php. I also tried to hardcode the password rule out errors within the request but that also doesn’t work.

Those are the errors I get via php:

0   "[sudo] password for www-data: Sorry, try again."
1   "[sudo] password for www-data:"
2   "sudo: no password was provided"
3   "sudo: 1 incorrect password attempt"

What’s the error here?