PHP exec doesn't return output of external program

I've the following program:

function execute_query($ip, $username, $password, $query){
    $runCMD ="python run_query.py " . escapeshellarg($ip). " ". escapeshellarg($username) . " " . escapeshellarg($password)
        .  ' ' . escapeshellarg($query);
    $output = [];
    exec ($runCMD, $output);
    print_r ($output);
    //return (implode("\n", $output));
}

However the array $output is always empty, the program is returning the result correctly, when executed manually from CMD. I've no idea what to do. I tried so many things that I even don't recall all of them. Here are a few:

1.I disable UAC
2.I tried using shell_exec
3.I tried appending powershell in front of the command 'powershell' . $runCmd
4.I tried appending '2>&1' at the end of the command
5.I tried using proc_open, to manually open cmd.exe and execute the command

Any other ideas what I may do, or at least how to continue troubleshooting. Using Linux is not an option It seems like stderr is working. Also i tried printing 'asd' from the python script and it's working. The output from the python script is WMI query, it may contain \n, \r and \t

Here is the python script:

from subprocess import check_output
import sys

def run_query(ip, username, password, query):
    runCMD = "query.exe " + username + " " + password + ' "' + query + '" ' + ip + ''
    results = check_output(runCMD).decode("utf-8")
    results = results.split("\n")
    for result in results:  
        if not result.startswith("__"):
            print(result)
if __name__ == "__main__":
    run_query(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])

UPDATE 1
query.exe is a program, which returns filtered result from WMI query, passed as argument. No idea what's inside but it's working as expected. I also tried running query.exe from the php script, the same thing no output.
UPDATE 2
I'm using php 7.1, tried php 5.6 without luck.
UPDATE 3
When I try to change the string into byte array in python I get this output when the script is manually runned:

bytearray(b'The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)\r')
bytearray(b'')

, but the php script returns this:

Array
(
    [0] => bytearray(b'')
)


UPDATE 4
f*** it I'm switching the whole project to python