Front-end time on the internet to see the example of Erlang connection php-fpm, I followed to get a bit, the following to my debugging code after the post
I fixed 2 bugs in the original code:
1, parameter more than 128 characters error
2. Error when PHP returns more than 1024 bytes
File name Fastcgi.erl
-Module (fastcgi). -Compile (Export_all). Do_request (ARGV), Env = [{"Script_filename", "E:/phpgame/game/gateway.php"}, {"WebSocket", Argv}], Io:format ("Argv msg: ~t S~n ", [ARGV]), {OK, Socket} = Gen_tcp:connect (" 127.0.0.1 ", 9000, [binary, {p Acket, 0}, {active,true}], 10000), Fcgi_send_record (Socket, 1, 1, << 1: 0:8, 0:40 >>), Fcgi_send_record (Socket, 4, 1, ENV), Fcgi_send_rec Ord (socket, 4, 1, []), Bin = Phprespone (socket,[]), gen_tcp:close (socket), percent of the original message << Version:8, Type:8, Requestid:16, Contentlength:16, Paddinglength: 8, Reserved:8, str/binary >> = Bin, << _: Contentlength:16, _: Str/binary >> = Bin, percent Calculate output Content Length Dlen = ContentLength-52, percent gain content << _h:52/ Binary,data:dlen/binary,_other/binary >> = Str, Io:fwrite ("rs:~p~ N ", [Data]), {ok,data}.recv_msg (Sock), receive {TCP, Sock, Bin}, Io:format (" Bin msg: ~p~n ", [bin]), percent of the original message << Version:8, Type:8, RequestID: Contentlength:16, Paddinglength:8, Reserved:8, Str/binary >> = Bin, << _: +, Contentlength:16, _: Str/binary >> = Bin, percent count output content length Dlen = ContentLength-52, percent gain content << _h:52/binary,data:dlen/binary,_other/binary >> = Str, io:f Write ("Rs:~p~n", [Data]), {ok,data}; {Tcp_closed,sock}-Io:format ("Socket Close: ~p~n", [Sock]); _other-Io:format ("Other msg: ~p~n", [_other]), recv_msg (Sock) after 3000 -Io:format ("Time Out.~n") end. Phprespone (Sock,sofar), receive {TCP, Sock, Bin}, Phprespone (Sock,[bin | SOFAR]); {Tcp_closed,sock}, List_to_binary (Lists:reverse (Sofar)); _other-Io:format ("Other msg: ~p~n", [_other]), Phprespone (Sock,sofar) after 30 Io:format ("Time Out.~n") end. Percent Send option Fcgi_send_record (Socket, Type, RequestID, namevaluelist), Encodedrecord = Fcgi_encod E_record (Type, RequestiD,namevaluelist), Gen_tcp:send (Socket, Encodedrecord). Percent Pack Fcgi_encode_record (Type, RequestID, Namevaluelist) when Is_list (namevaluelist), Fcgi_encode_reco RD (Type, requestid,fcgi_encode_name_value_list (namevaluelist)); Percent Contentdata whether or not the full 8 bytes are filled, otherwise the fill Fcgi_encode_record (Type, RequestID, Contentdata) when Is_binary (Contentdata), ContentLength = Size (contentdata), paddinglength = if contentlength rem 8 = = 0-0; True-8-(contentlength REM 8) end, percent-filled data, 0 padding per 8-byte pack Paddi Ngdata = << 0: (paddinglength * 8) >>, Version = 1 , Reserved = 0, << version:8, Type:8, Requestid:16, Contentle Ngth:16, Paddinglength:8, Reserved:8, Contentdata/binary, Paddingdata/binary >>. Percent of the environment variables are composed of binary fcgi_encode_name_value_list (_namevaluelist = []) << & gt;>; Fcgi_encode_name_value_list (_namevaluelist = [{name, value} | Tail]) << (Fcgi_encode_name_value (name,value))/binary, (Fcgi_encode_name_value_li St (Tail))/binary >>. Fcgi_encode_name_value (name, _value = undefined), Fcgi_encode_name_value (name, ""); Fcgi_encode_name_value (name, value) when Is_list (name) and is_list (value), namesize = Length (Name), Namesizedata = << namesize:8 >>, valuesize = Lengt H (Value), if valuesize < Valuesizedata = << Valuesize:8 >>; Valuesize > 127 -Valuesizedata = << (valuesize bor # 80000000):32>> End, << namesizedata/binary,valuesizedata/binary, (List_to_binary (Name))/ Binary, (List_to_binary (Value))/binary >>.
Starting PHP CGI under Windows can do this
Php-cgi.exe-b 127.0.0.1:9000
Called in the Erlang Shell:
2> C (fastcgi).
{ok,fastcgi}
3> fastcgi:do_request ("{' UID ': 1}").
The writing is not good, please include it all