# can Php - Fast-CGI and STDIN be used?

- Thread: https://externals.io/message/22573
- Original archive: https://news-web.php.net/php.internals/22573
- Started by Matthew on 2006-03-25T01:09:23+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #22573 — Matthew — 2006-03-25T01:09:23+00:00

URL: https://externals.io/message/22573#22573

I'm sorry if this is the wrong place for this post, I just don't know where
else to go. 

I need some help figuring out how to use FCGI_STDIN with a running php
script.

I have written a server in c++, which for a while been running php through
it's plain old cgi interface. I have been using php for 2 things, the
obvious one, web pages, and the not so obvious, safe command/server
scripting. I have just implimented a fast-cgi interface to replace the cgi
interface, however, the scripting stuff i had that worked over regular cgi
just doesn't want to work with fast-cgi.

Basically, the php script outputs a command and arguments, all commands
start with a %, so:
$var = "%print";
printf("%s hello world\n", $var); 
Can be used to print to the user calling the script.

That part still works fine of course :).

It doesn't stop there however, some of the %callbacks will return strings of
data over stdin so they can be $var = trim(fgets($STDIN)) for a very simple
and effective method of communicating with the server.

I've tried doing the same with fast-cgi, but it seems
fopen("php://stdin","r") is not the right place to be reading from.

I'm sending:

(C++)
// loops over ever line of input, test it
std::string response = processALineFromCGI-OrFastCGIScript(oneLine)
if(response.compare("NOCMD") == 0)
buffer_to_print_to_user_who_called_this_when_request_finishs();
else if(response.size() > 0) {
 std::string sendout = FCGI_Headerize(response, FCGI_STDIN, ..);
 non-blocking_send(sendout);
}
oneLine.erase();
I use the same function to build the headers for FCGI_BEGIN_REQUEST,
FCGI_PARAMS, FCGI_STDIN on POST input, it works great there(now)


Also, is it possible to keep the connection open to php-cgi to avoid
connect/accept calls? Even if it's only fifo so only 1 request can go at a
time?

If I must I could create a special c++ fast-cgi server that binds a second
port to pipe requests from php streams to the server, but would be a MAJOR
hack for what I'm trying to do.. :-D

Thanks in advance!

---

## #22595 — Wez Furlong — 2006-03-27T12:47:09+00:00

URL: https://externals.io/message/22573#22595 · In reply to: #22573

IMO, you're better off using `stream_socket_server()` and writing a
"real" daemon for that.  Abusing fastcgi/cgi to work in that way is
only going to bite you in the ass.

--Wez


> I'm sorry if this is the wrong place for this post, I just don't know where
> else to go.
>
> I need some help figuring out how to use FCGI_STDIN with a running php
> script.
>
> I have written a server in c++, which for a while been running php through
> it's plain old cgi interface. I have been using php for 2 things, the
> obvious one, web pages, and the not so obvious, safe command/server
> scripting. I have just implimented a fast-cgi interface to replace the cgi
> interface, however, the scripting stuff i had that worked over regular cgi
> just doesn't want to work with fast-cgi.
>
> Basically, the php script outputs a command and arguments, all commands
> start with a %, so:
> $var = "%print";
> printf("%s hello world\n", $var);
> Can be used to print to the user calling the script.
>
> That part still works fine of course :).
>
> It doesn't stop there however, some of the %callbacks will return strings of
> data over stdin so they can be $var = trim(fgets($STDIN)) for a very simple
> and effective method of communicating with the server.
>
> I've tried doing the same with fast-cgi, but it seems
> fopen("php://stdin","r") is not the right place to be reading from.
>
> I'm sending:
>
> (C++)
> // loops over ever line of input, test it
> std::string response = processALineFromCGI-OrFastCGIScript(oneLine)
> if(response.compare("NOCMD") == 0)
> buffer_to_print_to_user_who_called_this_when_request_finishs();
> else if(response.size() > 0) {
>  std::string sendout = FCGI_Headerize(response, FCGI_STDIN, ..);
>  non-blocking_send(sendout);
> }
> oneLine.erase();
> I use the same function to build the headers for FCGI_BEGIN_REQUEST,
> FCGI_PARAMS, FCGI_STDIN on POST input, it works great there(now)
>
>
> Also, is it possible to keep the connection open to php-cgi to avoid
> connect/accept calls? Even if it's only fifo so only 1 request can go at a
> time?
>
> If I must I could create a special c++ fast-cgi server that binds a second
> port to pipe requests from php streams to the server, but would be a MAJOR
> hack for what I'm trying to do.. :-D
>
> Thanks in advance!
