有没有从 PHP 脚本调用 CGI 脚本的好方法?
我看到 PHP 中有一个 virtual() 函数可以调用 CGI 脚本,但这是最好的方法吗? 我也可以将任何参数传递给该脚本吗?
我看到一些使用 file_get_contents()
或 include() 并传入 CGI 脚本 URL 的示例,但这感觉像是黑客攻击。
I saw that there is a virtual() function in PHP that will call a CGI script, but is that the best way? Can I pass any parameters to that scripts as well?
I saw some examples using file_get_contents()
or include() and passing in the URL of a CGI script, but that feels like a hack.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果可以在本地调用,请使用 exec()。 如果它需要作为 CGI 调用(因为脚本被设计为仅在 CGI 环境中工作),那么您需要通过 include() 或 file_get_contents( )。 virtual() 将刷新缓冲区并附加子请求的输出。
您可以通过 include()、file_get_contents() 和 virtual() 传递参数作为 GET 参数:
如果可能,请转到 exec() 路线。 使用其他方法可能需要更改配置。
当使用 exec() 时,您需要传递参数就像任何 CLI 程序一样。
如何传递参数将取决于您稍后希望如何在其他程序/脚本中解析它们。 它们将显示在被调用的程序中,就像从命令行调用它一样。
Use exec() if you can call it locally. If it needs to be invoked as a CGI (as in the script is designed to only work within a CGI environment), then you'll need to call it via include() or file_get_contents(). virtual() will flush your buffers and append the output of the sub-request.
You can pass parameters through include(), file_get_contents(), and virtual() as GET parameters:
If possible, go the exec() route. Using the other methods may require a config change.
When using exec(), you'll need to pass the arguments like you would for any CLI program.
How you pass the parameters in will depend on how you want to parse them out later in the other program/script. They'll show up in the called program like they would if you called it from the command line.
我在从 php 页面调用 CGI 脚本时也遇到了问题。 后来我意识到问题是我的页面文件具有 .html 后缀,而不是 .php 后缀。 解决这个问题使我的脚本可以为我工作。 这可能不是你的问题,但我想我应该提一下以防万一。
I was having trouble with calling a CGI script from my php page too. Later I realized that the issue was that my page file had the .html suffix, rather than the .php suffix. Fixing that allowed my script to work for me. This is probably NOT your problem, but I thought I'd mention it just in case.