简体   繁体   English

从PHP脚本调用Java(或python或perl)

[英]Calling Java (or python or perl) from a PHP script

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). 我一直在尝试在Django中构建一个简单的原型应用程序,并且很遗憾,因为它太复杂了(我知道从长远来看它是值得的,但我真的只是不喜欢)有足够的时间 - 我需要一些东西,并在几天内运行。 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. 所以,我现在正在考虑使用PHP,因为它是创建我最熟悉的动态Web内容的方法,而且我知道我可以快速完成某些工作。

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. 我的应用程序虽然很简单,但可能会做一些相当复杂的AI工作,而且可能是因为我不需要在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. 所以我想知道PHP脚本“调用”Java程序或Python脚本或者用另一种语言编写程序或脚本是多么容易/可能。 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. 我并不完全清楚我在这种情况下的“调用”究竟是什么意思,但我想我可能意味着理想情况下我想定义一个函数,比方说Java,然后能够从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. 如果那是不可能的,那么我想我最好的选择(假设我使用PHP)将通过POST或GET直接将控制权直接传递给外部程序到CGI程序或类似程序。

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... 随意说服我应该坚持使用Django,虽然我真的不知道我需要什么模型来生成我想要的HTML表单,这似乎是一件基本的事情,我担心我的做任何更复杂的事情的机会......

Alternatively, anyone who can offer any advice on linking PHP and other languages, that'll be grateful received. 或者,任何能够提供关于PHP和其他语言链接的建议的人都会感激不尽。

"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" “我无法弄清楚我需要什么样的模型来生成我想要的HTML表单,这似乎是一件基本的事情,我担心我有机会做更复杂的事情”

Common problem. 常见问题。

Root cause: Too much programming. 根本原因:编程太多。

Solution. 解。 Do less programming. 做少编程。 Seriously. 认真。

Define the Django model. 定义Django模型。 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. 计算在模型中是mmethods。 Queries work. 查询工作。 Now you can start working on other presentations of the data. 现在,您可以开始处理其他数据表示。

Django generally starts (and ends) with the model. Django通常以模型开始(并结束)。 The forms, view and templates are derived from the model. 表单,视图和模板都是从模型中派生出来的。

For an easy access of Java classes from PHP scripts you can use a php-java bridge. 为了从PHP脚本轻松访问Java类,您可以使用php-java桥。

There is a open source solution: http://php-java-bridge.sourceforge.net/pjb/ 有一个开源解决方案: http//php-java-bridge.sourceforge.net/pjb/
or a solution from Zend ( http://www.zend.com/en/products/platform/product-comparison/java-bridge ). 或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. 我对后者比较熟悉,而且使用起来非常简单直观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM