0

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 ?

sam
  • 9,486
  • 36
  • 109
  • 160
  • You haven't said what you're using the Python library for. Is it possible that while you're trying to mix languages you could just do what you need in PHP? –  Dec 13 '13 at 23:29

3 Answers3

2

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.

abarnert
  • 354,177
  • 51
  • 601
  • 671
1

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.

Jite
  • 5,761
  • 2
  • 23
  • 37
0

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

James Mills
  • 18,669
  • 3
  • 49
  • 62
  • It looks very old though ~2002 -- So it may not be very well supported. The general concept of a one-way or two-way bridge between langauges is fairly common though in a lot of Dynamically Typed Langauges such as Python, Lua, Ruby, PHP, etc – James Mills Dec 13 '13 at 23:40
  • Probably best not to recommend things that are A) a decade+ old, and B) you haven't tried personally ;) – brandonscript Dec 14 '13 at 05:18
  • I agree, but it's worth mentioning nonetheless :) Perhaps someone could take it up and bring it back up-to-date :) – James Mills Dec 14 '13 at 07:35