从虚拟/自定义" Web服务器"调用PHP. [英] Call PHP from virtual/custom "web server"

查看:154
本文介绍了从虚拟/自定义" Web服务器"调用PHP.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我试图弄清楚如何从"Web服务器"中调用PHP.

Basically, I'm trying to figure out how PHP can be called from a "web server".

我已经阅读了文档,但是并没有太大帮助.

I've read the documentation, but it didn't help much.

据我所知,有三种方法可以调用PHP:

As far as I can tell, there are three ways to invoke PHP:

  • 通过命令行(例如:php -f "/path/to/script.php")
  • 通过CGI(??)/通过FastCGI(???)
  • 通过网络服务器(例如Apache)模块

因此,让我们从 CGI 开始.也许我只是瞎子,但规范中并未提及网络服务器在地球上如何将数据(标头和回调)传递给实现CGI的事物.使用 FastCGI 的情况甚至更糟.

So let's start with CGI. Maybe I'm just blind, but the spec doesn't mention how on Earth the web server passes data (headers & callbacks) to the thing implementing CGI. The situation is even worse with FastCGI.

接下来,我们有特定于服务器的模块,由于所有潜在客户都无处可寻,我什至都不知道要搜索什么.

Next up, we have server-specific modules, which, I don't even know what to search for since all leads end up nowhere.

推荐答案

调用CGI脚本非常简单. PHP具有一些特性,但是您基本上只需要设置环境变量列表,然后调用PHP-CGI二进制文件即可:

Invoking a CGI script is pretty simple. PHP has a few peculiarities, but you basically only need to setup a list of environment variables, then call the PHP-CGI binary:

setenv GATEWAY_INTERFACE="CGI/1.1"
setenv SCRIPT_FILENAME=/path/to/script.php
setenv QUERY_STRING="id=123&name=title&parm=333"
setenv REQUEST_METHOD="GET"
...

exec /usr/bin/php-cgi

其中大多数是样板. SCRIPT_FILENAME是如何将实际的php文件名传递给PHP解释器,而不是作为exec参数.对于PHP来说,至关重要的也是非标准变量REDIRECT_STATUS=200.

Most of them are boilerplate. SCRIPT_FILENAME is how you pass the actual php filename to the PHP interpreter, not as exec parameter. Crucial for PHP is also the non-standard variable REDIRECT_STATUS=200.

对于GET请求,您仅需要环境变量.对于POST请求,您只需将HTTP请求正文作为stdin管道传输到已执行的php-cgi二进制文件.返回的stdout是CGI响应,由不完整的HTTP标头\ r \ n \ r \ n和页面正文组成.

For a GET request you only need the environment variables. For a POST request, you simply pipe the HTTP request body as stdin to the executed php-cgi binary. The returned stdout is the CGI response consisting of an incomplete HTTP header, \r\n\r\n, and the page body.

(只是从内存中.也许还有更多陷阱.)

(Just from memory. There maybe a few more gotchas.)

这篇关于从虚拟/自定义" Web服务器"调用PHP.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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