使用erlang连接php的cgi处理方式,构建强大的web开发平台
- -module(test).
- -export([cgi_worker/0]).
- cgi_worker() ->
- Env = [{"SERVER_SOFTWARE","test web server"},
- {"SERVER_NAME","localhost"},
- {"HTTP_HOST","localhost"},
- {"GATEWAY_INTERFACE","CGI/1.1"},
- {"SERVER_PROTOCOL","HTTP/1.1"},
- {"SERVER_PORT","8080"},
- {"REQUEST_METHOD","GET"},
- {"REQUEST_URI","/t.php"},
- {"DOCUMENT_ROOT","d:/www"},
- {"DOCUMENT_ROOT_MOUNT","/"},
- {"SCRIPT_FILENAME","d:/www/t.php"},
- {"PATH_INFO",[]},
- {"PATH_TRANSLATED",[]},
- {"SCRIPT_NAME","/t.php"},
- {"REMOTE_ADDR","127.0.0.1"},
- {"REMOTE_HOST","127.0.0.1"},
- {"SERVER_ADDR","127.0.0.1"},
- {"LOCAL_ADDR","127.0.0.1"},
- {"QUERY_STRING",[]},
- {"HTTP_ACCEPT",
- "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,i
- mage/png,*/*;q=0.5"},
- {"HTTP_USER_AGENT",
- "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.0 (KHTML
- , like Gecko) Chrome/4.0.206.1 Safari/532.0"},
- {"HTTP_ACCEPT_CHARSET","GBK,utf-8;q=0.7,*;q=0.3"},
- {"HTTP_ACCEPT_LANGUAGE","zh-CN,zh;q=0.8"},
- {"HTTP_ACCEPT_ENCODING","gzip,deflate,sdch"},
- {"HTTP_CACHE_CONTROL","max-age=0"}],%%构造http访问相关配置
- CGIPort = open_port({spawn, "f:/PHP/php-cgi.exe"},%%Port方式对外交互
- [{env, Env},
- {cd, "d:/www"},
- exit_status,
- binary]),
- cgi_get_line({start, CGIPort}).
- %%获取结果
- cgi_get_line({start, Port}) ->
- receive
- {Port, {data,Data}} ->
- io:fwrite("DATA:~p~n", [Data]);
- {Port, {exit_status, 0}} ->
- io:fwrite("~p~n", [{exit_status, 0}]);
- {Port, {exit_status, Status}} when Status /=0 ->
- io:fwrite("~p~n", [{exit_status, Status}]);
- _Other ->
- io:fwrite("~p~n", [_Other])
- end.
复制代码
php脚本
- <?php
- echo 'ello php-cgi!';
- ?>
复制代码
以上纯粹直接构造,使用时请根据每个人环境加以修改,编译后运行如下:
67> c(test).
{ok,test}
68> test:cgi_worker().
DATA:<<"X-Powered-By: PHP/5.2.6rnContent-type: text/htmlrnrnhello php-cgi!">>
ok
说明连接并执行成功,erlang以其强悍的网络并发处理(并非只谈性能)能力,加上php的脚本的简便,相信他们可以成为很好的互补。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
楼主,我觉得你真的太厉害了~~~!