我的前端有一个按钮,这个按钮与后端对话。 后端正在启动一个远程脚本:
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$connection = ssh2_connect('192.168.56.180', 22);
ssh2_auth_password($connection, 'root', 'password');
$stream = ssh2_exec($connection, 'python /WATSON/APP/test/testlistrbk.py');
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo $stream_out_contente;
fwrite($myfile, $stream_out);
fclose($myfile);
?>
我有2个问题,第一个,php应该等待python脚本完成,因为它说here但它没有。
第二个,它给了我以下内容:
PHP警告:fwrite()要求参数2为字符串,资源在第41行的/var/www/html/WEBAPP/wa_start.php中给出
答案 0 :(得分:1)
使用stream_get_contents(stream_out);
在您的代码$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
中,这将返回resource
而不是字符串输出。试试这段代码。
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$connection = ssh2_connect('192.168.56.180', 22);
ssh2_auth_password($connection, 'root', 'password');
$stream = ssh2_exec($connection, 'python /WATSON/APP/test/testlistrbk.py');
stream_set_blocking($stream, true);
$outputStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
echo "Output: " . stream_get_contents($outputStream);
echo "Error: " . stream_get_contents($errorStream);
fwrite($myfile, $outputStream.$errorStream);
fclose($myfile);