Calling a function in another lagnauge

63 views Asked by At

Im writing a program in php which does almost everything it needs to do, but at one point i need to use a function that is written in python using the mechanize library, both these scripts will be hosted on the same server.

My initial thoughts are to make a CURL call containing any arguments from the php script to the python script, and then return the results of the function again back to the php script using CURL.

Im quite new to programming and not sure of the best conventions when doing something like this, is my setout workflow using CURL the usual way this would be done or is there another way ?

3

There are 3 answers

0
Jite On

You could always use the exec function in php (http://php.net/function.exec) if the script is on the same server.

But please, be sure that you know what you are doing, cause the function is powerful and could be quite nasty if used the wrong way.

0
abarnert On

If you need to be able to distribute the PHP code and the Python code independently, deploy them on separate servers, etc., then this is reasonable—you make the Python code a web service, make the PHP code call that web service, and you're done.


But if the Python script is always going to be running locally, it's usually easier just to run it as a program—pass it command-line arguments and/or standard input, and retrieve its standard output. PHP has a few different ways to do this—system, popen, exec, passthru—which all have different upsides and downsides.

For example, with exec, you just call escapeshellarg on each argument, put them together into a space-separated string with the path to the script, call exec, and you get the result back.

3
James Mills On

I don't know how good this is (I haven't tried it personally) but you could always ghive this a try: