This can be done—sudo's requiretty
option is relatively easy to defeat, so it doesn't really add much security by itself. If you are really sure that you want to leave it on (maybe you need to support running your script on users' systems which have requiretty
on by default?) then making a tty may really be the right solution.
This would probably be an enormous pain in straight PHP, though. I've searched the reference docs at php.net, and I don't see any indication that it has any builtin utility functions for allocating or using ptys. If you really want to do it in pure PHP, you'll probably need to refer to the glibc source code for the forkpty() call (including the other dependencies it has, like openpty(), getpt(), grantpt(), unlockpt(), etc). At its core, this will involve open()'ing the pseudoterminal master device at /dev/ptmx to get a master PT file descriptor, doing a chown and a TIOCSPTLCK ioctl, then determining which device in /dev/pts is the corresponding slave fd and opening that.
But I'd highly recommend doing the tty faking part outside of PHP, probably by popen-ing some sudo wrapper script in Python (which has some nice convenient functions in the pty module) or even bash + something like socat.