views:

575

answers:

5

Hi,

I have a website built with PHP. I have an Erlang application running as a daemon on the same server. I need to call functions on the Erlang application from PHP and get back the result.

I've found PHP/Erlang and over PHP modules but I can't install a PHP module on this server, I can only use PHP code.

The only way I know to solve it is run an Erlang web server locally that the PHP will be able to talk to.

Is there a better way to solve it? If using a httpd server is the best way, what Erlang server should I use? It should be as light as possible and doesn't need features like SSL and doesn't need to handle large load.

Thanks

A: 

I don't think there is better solution. I need Erlang webserver to run it on web. here is some info PHP+Erlang related

http://yaws.hyber.org/cgi.yaws

Sorantis
I don't need to serve php using erlang server(yaws).I serve my php using apache but wants to be able to call erlang functions from php scripts.
+1  A: 

Erlang is excellent at socket I/O: maybe you could use a pipe of some sort?

This would be more direct than through another WEB server layer for sure.

Use the functions erlang:open_port and erlang:port_command functions to get data in/out of Erlang through a system port.

jldupont
Can you explain what do you mean by "a pipe of some sort"?What do I need to do on the erlang side and how do I call it from php?
You write yourself a gen_server on the Erlang side that opens a socket and on the PHP side you use a socket too (http://ca.php.net/sockets). In terms of data interchange format, you can use either XML or JSON.
jldupont
Will the socket be thread safe, meaning that two erlang process can send messages to the same socket the gen_server is listening to?Is the google protocol buffer related http://code.google.com/apis/protocolbuffers/docs/overview.htmlor is it just a more efficient way to pass the data.In my case simplicity is much more important than efficiency.thanks
(have you considered entering separate questions?)- Erlang is very much thread-safe; in fact it excels at threading.- Google Protocols Buffers: I do not know enough to say something intelligent about these.
jldupont
A: 

$ cat erl.erl

#!/usr/bin/env escript

main(Args) ->
    io:format("~p\n", [Args]),
    io:format("~p\n", [(catch test(Args))]).

test([N1,N2|_]) ->
    lists:seq(list_to_integer(N1),list_to_integer(N2)).

$ chmod +x erl.erl

$ cat php.php

?php
var_dump(exec("./erl.erl 1 5"));
?>

$ php php.php

string(11) "[1,2,3,4,5]"

JLarky
you should use http://php.net/proc_open in php and most likely communication with other erlang node in erl.erl but i think you can do it by yourself :)
JLarky
This is much slower as the escript must be compiled each time. Also, a shell command must be spawned each time.
jldupont
you can replace escript by compiled erlang application, just "erl -run module function args". And program will be spawned once per php script, not so much.
JLarky
A: 

I'd run a webserver such as mochiweb hosting the erlang code. The PHP code would use curl to send http queries encoded in JSON to mochiweb. Mochiweb has a JSON encoder/decoder and PHP has native JSON support.

Even if every thing is on the same server, just use HTTP. Handles all the low level stuff and if you need to scale, it will be easier, as scaling with HTTP is a solved problem. Mochiweb is light and has high performance.

cstar
A: 

Have a look at erl_call. http://www.erlang.org/doc/man/erl_call.html

It is a unix program which is used to call a function in erlang. It will start a dummy erl node, execute the command(s) and return the result. You could use PHP to call erl_call and then use the results it returns.

ErJab