从 Java 调用 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/614995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling PHP from Java
提问by
Possible Duplicate:
invoking a php method from java
可能的重复:
从 java 调用 php 方法
I am writing a Java class. And I have some logic in test.php. One of the methods of that Java class should invoke test.php, so that test.php performs some computation and returns the result to the invoking Java method. Can I do that with something like the following?
我正在编写一个 Java 类。我在 test.php 中有一些逻辑。该 Java 类的方法之一应该调用 test.php,以便 test.php 执行一些计算并将结果返回给调用 Java 方法。我可以用下面的东西来做到这一点吗?
Runtime rt = runtime.getRuntime();
process exec = rt.exec(test.php);
回答by Itay Moav -Malimovka
If you have a php cli installed, then you can do what you wrote here. Just run the php file with the CLI.
如果您安装了 php cli,那么您可以执行您在此处编写的操作。只需使用 CLI 运行 php 文件。
回答by david a.
You may be able to execute the php script that way, but will it produce expected result?
您也许能够以这种方式执行 php 脚本,但是它会产生预期的结果吗?
Call to PHP done this way may work, but it will miss any context that it may need to work (parameters from a request, user's session scope, etc.)
以这种方式调用 PHP 可能会起作用,但它会错过它可能需要工作的任何上下文(来自请求的参数、用户的会话范围等)
回答by spligak
This works in the opposite direction you are describing but it may be able to help as it is an actual bridge between the two languages.
这与您描述的方向相反,但它可能会有所帮助,因为它是两种语言之间的实际桥梁。
http://www.php.net/manual/en/java.examples-basic.php
http://www.php.net/manual/en/java.examples-basic.php
PHP has had a Java bridge for awhile now, but keep in mind it is experimental at best.
PHP 使用 Java 桥已经有一段时间了,但请记住,它充其量只是实验性的。
回答by codefinger
You should use the JSR-223 scripting API. There is a PHP implementation that uses a Java/PHP bridge:
您应该使用 JSR-223 脚本 API。有一个使用 Java/PHP 桥接的 PHP 实现:
http://php-java-bridge.sourceforge.net/pjb/
http://php-java-bridge.sourceforge.net/pjb/
Another is pure Java: http://www.caucho.com/resin-3.0/quercus/
另一个是纯Java:http: //www.caucho.com/resin-3.0/quercus/
I have not used this myself. But Caucho's Quercus product uses it, so I would suspect it is mature enough to do whatever you need.
我自己没有使用过这个。但是 Caucho 的 Quercus 产品使用它,所以我怀疑它已经足够成熟,可以做任何你需要的事情。
Here are some other links that may help you:
以下是一些可能对您有所帮助的其他链接:
https://scripting.dev.java.net/
https://scripting.dev.java.net/