macos 使用 Mac 终端运行 PHP 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10012226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 09:01:53  来源:igfitidea点击:

Run PHP script using Mac Terminal

phphtmlmacosterminal

提问by 125369

Right now I can run a PHP script from my Mac terminal like echoing some output, creating folders etc. That is just PHP which does not take any input parameters.

现在我可以从我的 Mac 终端运行一个 PHP 脚本,比如回显一些输出,创建文件夹等。这只是不接受任何输入参数的 PHP。

Is it possible to run a PHP script with an HTML part which consists of a form for entering a username which is again posted in the PHP part so that I can create a folder name with the entered username. I wanted to test it using Terminal, not a browser on the Mac.

是否可以运行带有 HTML 部分的 PHP 脚本,该部分包含用于输入用户名的表单,该表单再次发布在 PHP 部分中,以便我可以使用输入的用户名创建文件夹名称。我想使用终端测试它,而不是 Mac 上的浏览​​器。

I just tried running the script with html part and what I get in the terminal output is that the whole html part is just reflected as output in the terminal output including the tags. Is what I want possible?

我只是尝试用 html 部分运行脚本,我在终端输出中得到的是整个 html 部分只是在终端输出中反映为输出,包括标签。我想要的可能吗?

回答by Elias Van Ootegem

PHP does take parameters from terminal, just execute your script ./myScript.php nameand in your script $argv[1] will hold name. $argv[0] is the absolute path to the current script.

PHP 确实从终端获取参数,只需执行您的脚本,./myScript.php name并且在您的脚本中 $argv[1] 将保留name. $argv[0] 是当前脚本的绝对路径。

Though I'm not quite sure what your script is for, you might want to read up on the php-cli... it's far more elaborate than you seem to believe

虽然我不太确定你的脚本是做什么的,但你可能想阅读 php-cli ......它比你似乎相信的要复杂得多

回答by Purpletoucan

I don't think what you are asking is possible. The terminal doesn't 'know' what HTML is, it is really just a bunch of text strings. The reason HTML forms display in a browser is because the HTML is rendered.

我不认为你问的是可能的。终端不“知道”HTML 是什么,它实际上只是一堆文本字符串。HTML 表单在浏览器中显示的原因是因为 HTML 已呈现。

PHP does support command line scripts, but instead of putting out HTML strings, you should just put out plain text strings. Terminal windows are no as interactive as a browser window so the best you could do is put out a question and wait for a text response. I've never done this with PHP and I don't know if it is possible. If you are familiar with PHP, you might find it relatively easy to migrate to Perl or Python, both of which would allow this style of interactive session.

PHP 确实支持命令行脚本,但是您应该只输出纯文本字符串而不是输出 HTML 字符串。终端窗口不像浏览器窗口那样具有交互性,因此您能做的最好的事情就是提出问题并等待文本响应。我从来没有用 PHP 做过这个,我不知道这是否可能。如果您熟悉 PHP,您可能会发现迁移到 Perl 或 Python 相对容易,这两者都允许这种交互会话风格。

回答by Pierre-Olivier

To interpret HTML content, you will need a browser; a terminal window will only display the output in plain text format.

要解释 HTML 内容,您需要一个浏览器;终端窗口将仅以纯文本格式显示输出。

However, if you want to read input from a user in a terminal window, you might want to look at I/O streams: http://www.php.net/manual/en/wrappers.php.php

但是,如果您想在终端窗口中读取用户的输入,您可能需要查看 I/O 流:http: //www.php.net/manual/en/wrappers.php.php

回答by gosukiwi

Apache is in charge of web request, you can't send a post to a php file without a web server. If you run the file in console all you will get is a bunch of HTML, but the console has no way to interpret the HTML, that's what the browser does, you can, however, make a PHP script to run in the console and request an username if that's what you need, that's the only way.

Apache 负责 Web 请求,没有 Web 服务器,您无法将帖子发送到 php 文件。如果您在控制台中运行该文件,您将得到的只是一堆 HTML,但控制台无法解释 HTML,这就是浏览器所做的,但是,您可以制作一个 PHP 脚本以在控制台中运行并请求如果这是您需要的用户名,那是唯一的方法。

PHP has a built-in webserverwith the latest version though, you might be able to use the browser just with PHP if you don't want a webserver like apache.

PHP 有一个内置的最新版本的网络服务器,如果你不想要像 apache 这样的网络服务器,你可以只用 PHP 使用浏览器。