如何从 PHP 5 使用 Java [英] How to use Java from PHP 5

查看:24
本文介绍了如何从 PHP 5 使用 Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多关于从 PHP 使用 Java 的教程,但它们似乎相互冲突.他们中的许多人说要更改 php.ini 以使用 dll 文件,但显然现在 PHP 5 中有内置"功能.但这需要Tomcat?我是否必须摆脱 Apache 才能运行它?http://php-java-bridge.sourceforge.net/pjb/installation.php 说将 .war 文件放入 Tomcat 的 autodeploy 目录,但我没有看到任何名为autodeploy"的目录.有人会一步一步解释如何让 Java 的东西在带有 PHP 5 和 Apache 的 Windows 机器上运行(我使用的是 Windows 7)?

I've looked at quite a few tutorials on using Java from PHP, but they seem to conflict with eachother. Many of them say to change php.ini to use a dll file, but apparently there are "built in" capabilities in PHP 5 now. But that requires Tomcat? Do I have to get rid of Apache to run that? http://php-java-bridge.sourceforge.net/pjb/installation.php says to put the .war file into Tomcat's autodeploy directory, but I don't see any directory called "autodeploy". Would someone explain step-by-step how to get get Java stuff working on a Windows machine (I'm using Windows 7) with PHP 5 and Apache?

推荐答案

您可能会发现大量关于如何使用 PHP Web 应用程序中的 Java 类的无用和过时的信息,这些信息根本没有帮助.大约一年前,我遇到了一个类似的问题,我发现在生产中使用足够成熟的唯一方法是 PHP/Java Bridge —— 你在 Sourceforge 上找到的那个.它比人们预期的要好,并且不需要 Java 知识(但是,如果您确实了解 Java,则应该考虑为此目的编写 Web 服务并在 PHP 端使用它).

You will probably find tons of useless and outdated information regarding how to use Java classes from your PHP webapp which doesn't help at all. About a year ago I had faced a similar problem and I found out that the only way that is mature enough to use in production is PHP/Java Bridge -- the one you found on Sourceforge. It works better than one might expect and doesn't require knowledge of Java (however if you do know Java, you should consider writing a Web-service for this purpose and consume it on the PHP-side).

Bridge 的文档不是很明显,但是请记住您应该坚持使用它而不是阅读其他描述不同方法的资源 - 不要混淆,即您不必安装任何 PHP 扩展或smth -- 所有这些都与从 PHP 调用 Java 的另一种(通常是过时且不受支持的)方法有关.

The documentation of the Bridge is not very obvious, however remember you should stick with it and not read other resources that describe different methods - don't mix things up, i.e. you don't have to install any PHP-extensions or smth -- all of them relates to another (usually outdated and unsupported) ways to call Java from PHP.

PHP/Java Bridge 是一种网络协议,因此您在 PHP 端唯一需要的是确保在 php.ini 中启用allow_url_include"

PHP/Java Bridge is a network protocol, so the only thing you need on your PHP-side is to ensure "allow_url_include" is enabled in your php.ini

从 Java 端,您必须将 PHP/Java Bridge 绑定到一个空闲端口并侦听连接.然后您的 PHP 应用程序将能够对 Bridge 的代理文件执行require_once",该文件将在该端口上可用.就是这样,您可以使用该代理 Bridge 对象从 PHP 脚本透明地实例化和调用 Java 类(它将在幕后序列化和传输调用和结果).

From the Java side you have to get PHP/Java Bridge bound to a free port and listen to connections. Then your PHP-app will able to do "require_once" of the Bridge's proxy file which will be available on that port. That's it and you can instantiate and call Java classes transparently from your PHP script using that proxy Bridge object (it will serialize and transfer calls and results behind the scenes).

有两种方法可以让 Bridge 建立起来并将其绑定到某个本地端口.

There are two options how get the Bridge up and bind it to some local port.

1) 复杂的一个:设置一个Tomcat,用桥本身和你想使用的库创建一个war文件.这种方式在这里描述:

1) complex one: setup a Tomcat, create a war file with the bridge itself and the library you'd like to use. This way is described here:

http://php-java-bridge.sourceforge.net/pjb/webapp.php

要将 war 部署到 Tomcat 安装中,只需将 war 放入 Tomcat 的 webapp 文件夹并重新启动它.

To deploy the war into your Tomcat installation simply put the war into Tomcat's webapp folder and restart it.

2) 更简单的一个:打包一个 jar 而不是 war 并运行它(它将使用嵌入式 Web 服务器,因此您不必安装 Tomcat 并部署任何东西).要走这条路,请参阅此页面:

2) easier one: package a jar instead of war and run it (it will use embedded web-server, so you won't have to install Tomcat and deploy anything). To go this way refer this page:

http://php-java-bridge.sourceforge.net/pjb/desktop-apps.php

两种方式都适合您(不要看第二个选项中的桌面"字样,这种方式也适用于您的 PHP 网络应用程序).

Both ways will work for you (don't look at the "desktop" word at the second option, this way will work fine with your PHP webapp as well).

请参阅示例"部分中有关如何使用它的示例(显然).您也可以参考我的小型 PHP 客户端,该客户端使用 PHP/Java Bridge 与 Java 中的 Neo4J 嵌入式数据库进行对话.但是,此示例可能更复杂,并且打算将其部署到 Tomcat,无论如何您都可以了解如何从中实例化和使用类:

Refer to examples on how to use it at the "Examples" section (obvously). Also you can refer to my small PHP client that talks to Neo4J embedded database in Java using PHP/Java Bridge. However this sample might be more complex and it was intended to be deployed to the Tomcat, anyway you can have an idea how to instantiate and use classes from it:

https://github.com/coffeesnake/neo4j-php-wrapper

这篇关于如何从 PHP 5 使用 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆