How does php's proc_open perform interactive operations

124 Views Asked by At

I try to operate "php xxx.php" with proc_open, it works fine, but I have a problem when I try to interact with mysql, I can't get the response data of mysql, it is output directly to the shell

code:

<?php
$handle = proc_open('mysql -uroot -p', [
    ["pipe", "r"],
    ["pipe", "w"],
    ["pipe", "w"]
], $pipes);

$world = stream_get_contents($pipes[1]);
var_dump($world);

Even if I just execute "mysql", I can't get its output, it seems that its output pipe is redirected somewhere else, how can I make it work as I expected.

I tried "bash -c 'mysql -uroot -p'" but still didn't get the expected behavior

For ease of understanding, I have appended the output:

[root@myrokcy code]# php myfile.php
Enter password: (Handle blocking here manually, otherwise the script won't continue)
string(0) ""
[root@myrokcy code]# 

If I adjust the output to stream_get_contents($pipes[2]);

[root@myrokcy code]# php myfile.php
Enter password: (Handle blocking here manually, otherwise the script won't continue)
string(83) "ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
"
[root@myrokcy code]# 

I still can't catch "Enter password: ", it is output first and blocks my reading and writing. I want to try to read this content and automate the interaction. Obviously, I fail. It shouldn't be a permission issue, my test account is "root", system: rocky8.6, it's just that I haven't modified the default "php.ini".

I think I still need to emphasize that the current problem is that the output from "$pipes[1]" cannot be obtained correctly, ""Enter password: "" is forced to output to the shell, and it will block until you operate, "$ world = stream_get_contents($pipes[1])" will not be executed unless you do something to stop the blocking process

If I'm doing it right, is this a bug? if it is i will report it.

0

There are 0 best solutions below