I didn't find obvious function that can return the results in stderr. So shell_exec is a general solution.
It's not some special function, but just something standard in shell. The answer is "2>&1". It means redirect message in stderr into stdout.
For example:

shell_exec("dir")
will return the file list normally.

shell_exec("dirrrr")
will return null since all messages are in stderr and nothing in stdout.

shell_exec("dirrrr 2>&1")
will return the error message.

shell_exec("dir 2>&1")
will return the file list normally.