5

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.

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

  • via command line (eg: php -f "/path/to/script.php")
  • via CGI(??) / via FastCGI (???)
  • via a web server (eg: Apache) module

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.

Christian
  • 27,509
  • 17
  • 111
  • 155
  • 1
    Have you considered reading the code? All of these things in your question are open source. – Dan Grossman Aug 12 '11 at 23:37
  • @Dan I'm *trying* to do that right now. But it's proving difficult for several reasons: **(1)** I can't find all the code **(2)** the code is in c/c++ **(3)** the code is very old and/or messy. – Christian Aug 12 '11 at 23:44
  • @Dan says who?? I could do it in Assembler or ADA if I wanted to (or FPC, which I'm planning to). – Christian Aug 12 '11 at 23:55
  • Yelling at the wind won't help you. http://tools.ietf.org/html/draft-robinson-www-interface-00 – Dan Grossman Aug 13 '11 at 00:00
  • Here is a CGI module written in PHP, might be easier to follow than the typical guides: http://code.google.com/p/nanoweb-instant/source/browse/work/five_tani/modules/mod_cgi.php - Don't bother with FastCGI, not worth the effort (and PHP doesn't support the simpler SCGI.) – mario Aug 13 '11 at 00:00
  • @Dan - Seen that already, see my 3rd para. – Christian Aug 13 '11 at 00:01
  • @mario - **Thanks, excellent response, as always.** – Christian Aug 13 '11 at 00:02

2 Answers2

7

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

mario
  • 144,265
  • 20
  • 237
  • 291
  • I faced the issue then you need to populate `PATH_INFO` and `SCRIPT_PATH` variables explicitly. Please check [my question](http://stackoverflow.com/questions/18962960) for details – Andrey Sep 24 '13 at 07:21
1

FastCGI is probably the best option since it's so wisely used, it would give you language independence (you could drop in Ruby later, for example) and it's well documented with lots of examples.

You could write your own Server API if you really want, but it's trickier than implementing FastCGI and has several disadvantages.

I wouldn't bother at all with straight CGI, FastCGI exists for a reason.

Long Ears
  • 4,886
  • 1
  • 21
  • 16
  • OK, how does the web server do IPC with the PHP executable? local(loopback) connection? – Christian Aug 13 '11 at 00:09
  • @Christian it could be on a different host (an arbitrary TCP/IP connection) or at the other end of a unix pipe. Do read the spec I linked to, it's quite plainly written. – Long Ears Aug 13 '11 at 00:22
  • I'll give it a good look after a few hours' sleep. It's 2:30am already :/ – Christian Aug 13 '11 at 00:40