使用PHP的交互式shell [英] Interactive shell using PHP

查看:198
本文介绍了使用PHP的交互式shell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道,是否可以仅使用PHP创建一个交互式shell.我的意思是您在数据库,python等方面拥有类似的东西.

Just wondering, is it possible to create an interactive shell, using PHP alone. I mean something like you have with databases, python, etc.

如果是,怎么办?

推荐答案

是的,有可能.为了进行交互,程序必须能够等待并从 stdin 中读取用户输入.在PHP中,您可以通过打开 'php://stdin' 的文件描述符来读取 stdin .取自一个不同问题的答案,这是一个PHP中的交互式用户提示的示例(当然是从命令行运行时):

Yes, it's possible. In order to be interactive, the program must be able to wait for and read in user input from stdin. In PHP, you can read from stdin by opening a file descriptor to 'php://stdin'. Taken from an answer to different question, here's an example of an interactive user prompt in PHP (when run from the command line, of course):

echo "Continue? (Y/N) - ";

$stdin = fopen('php://stdin', 'r');
$response = fgetc($stdin);
if ($response != 'Y') {
   echo "Aborted.\n";
   exit;
}

当然,要获得完整的输入行而不是单个字符,您需要 fgets() ,而不是 fgetc() .根据您的程序/外壳将执行的操作,整个程序可能被构造为一个大的连续循环.希望这能给您一个入门的思路.如果您想真正使用 (CLI伪GUI),则可以使用ncurses .

Of course, to get a full line of input rather than a single character, you'd need fgets() instead of fgetc(). Depending what your program/shell will do, the whole program might be structured as one big continuous loop. Hopefully that gives you an idea how to get started. If you wanted to get really fancy (CLI pseudo-GUI), you could use ncurses.

这篇关于使用PHP的交互式shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆