Call PHP from Virtual/Custom "Web Server"

Call PHP from virtual/custom web server

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

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.

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.)

How can i integrate php with my Http-server?

I think the easiest way would be to run it as a system call.

You can get documentation here.

You will have to set the $_SERVER variables as environment variables for the call from values gleaned from the request header but then it should just be a matter of piping in the request body and returning STDOUT.

you should be able to do something similar to support other server side languages as well (python, ruby etc..)

How does a webserver interface with PHP

It's pretty simple actually. The Webserver communicates with PHP through the CGI interface. This entails setting up environment variables, invoking the php interpreter, piping a POST body through stdin, and then reading the PHP response from stdout.

  • Call PHP from virtual/custom "web server"
  • How to pass POST data to the PHP-CGI?
  • http://pear.php.net/package/HTTP_Server
  • What is Common Gateway Interface (CGI)?

As to PHP postprocessing the $_SERVER variables: That's fairly minimal, it only constructs PHP_SELF and PHP_AUTH_USER etc. as documented in the manual. The rest is provided by the webserver (e.g. all HTTP headers converted into HTTP_* env variables).

PHP server on local machine?

Install and run XAMPP: http://www.apachefriends.org/en/xampp.html

I cannot reach php built-in server running on a VM

You are binding your server to localhost. It is only listening on the localhost network interface. It won't be accessible outside of that machine.

Tell it to listen on your externally facing IP address instead.

Alternatively, tell it to listen on all network interfaces:

php -S 0.0.0.0:9889


Related Topics



Leave a reply



Submit