Calling Java (or python or perl) from a PHP script
Asked Answered
F

2

3

I've been trying to build a simple prototype application in Django, and am reaching the point of giving up, sadly, as it's just too complicated (I know it would be worth it in the long-run, but I really just don't have enough time available -- I need something up and running in a few days). So, I'm now thinking of going with PHP instead, as it's the method for creating dynamic web content I'm most familiar with, and I know I can get something working quickly.

My application, while simple, is probably going to be doing some reasonably complex AI stuff, and it may be that libraries don't exist for what I need in PHP. So I'm wondering how easy / possible it is for a PHP script to "call" a Java program or Python script or a program or script in another language. It's not entirely clear to me what exactly I mean by "call" in this context, but I guess I probably mean that ideally I'd like to define a function in, let's say Java, and then be able to call it from PHP. If that's not possible, then I guess my best bet (assuming I do go with PHP) will be to pass control directly to the external program explicitly through a POST or GET to a CGI program or similar.

Feel free to convince me I should stick with Django, although I'm really at the point where I just can't figure out what model I need to produce the HTML form I want, which seems such a basic thing that I fear for my chances of doing anything more complex...

Alternatively, anyone who can offer any advice on linking PHP and other languages, that'll be grateful received.

Freiman answered 18/11, 2008 at 19:51 Comment(2)
Let me understand this. You're under time pressure to get a prototype up in a few days, and you've elected to learn new technology to do it? Is that correct?Lamaism
Yep. Not a good idea, I guess. But I've done it successfully before with PHP and with Ruby, and this time Python looked like the right way to go. Maybe I like living on the edge, or maybe I'm just not very smart! :-)Freiman
L
4

"where I just can't figure out what model I need to produce the HTML form I want, which seems such a basic thing that I fear for my chances of doing anything more complex"

Common problem.

Root cause: Too much programming.

Solution. Do less programming. Seriously.

Define the Django model. Use the default admin pages to see if it's right. Fix the model. Regenerate the database. Look at the default admin pages. Repeat until the default admin pages work correctly and simply.

Once it's right in the default admin pages, you have a model that works. It's testable. And the automatic stuff is hooked up correctly. Choices are defined correctly. Computations are in the model mmethods. Queries work. Now you can start working on other presentations of the data.

Django generally starts (and ends) with the model. The forms, view and templates are derived from the model.

Lamaism answered 18/11, 2008 at 20:31 Comment(2)
Thank you -- that's a really helpful perspective. I'll give it another go, I think! And thanks for all your other really helpful responses to my dumb questions today! BenFreiman
Sorry. I never know when to accept an answer. For a question like this one I feel like if I accept an answer then I won't get any more responses, which in this case might have been helpful.Freiman
C
2

For an easy access of Java classes from PHP scripts you can use a php-java bridge.

There is a open source solution: http://php-java-bridge.sourceforge.net/pjb/
or a solution from Zend (http://www.zend.com/en/products/platform/product-comparison/java-bridge).

I'm more familiar with the later, and it's very easy and intuitive to use.

Carney answered 18/11, 2008 at 20:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.