codeigniter 中的 PHP 和 Java 集成

发布于 2024-12-03 22:45:50 字数 212 浏览 0 评论 0 原文

我有一个基于 PHP 5 的网站,我需要将其与我创建的一系列 Java 类集成。基本上,我创建了一个 Web 服务来将 xml 发布到文件中,一旦进入该文件,我就会解析 xml 并想要调用 java 文件中的方法。

我不知道如何从 PHP 调用 java 函数。我研究过 PHP/Java 桥接器,但没有任何内容清楚地描述如何设置它。

任何帮助将不胜感激,

谢谢

I have a PHP 5 based site which I need ti integrate with a range of Java classes I have created. Basically I have created a webservice to post xml to a file, once in that file I parse the xml and want to call methods within a java file.

I have no idea how to call java functions from PHP. I have looked into PHP/Java bridge but there is nothing out there which describes clearly how to set this up.

Any help would be greatly appreciated,

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别忘他 2024-12-10 22:45:50

您可以使用 ThriftGoogle Protocol BuffersAvro 到生成接口并在之间传递消息两种语言。这类似于 Facebook 在 PHP 和其他代码之间进行通信的做法。

以 Google proto 缓冲区为例,您将创建一个定义某些结构的 .proto 文件。然后,您在该 .proto 文件上运行 proto 编译器,它将生成相应的 PHP 和 Java 类,这些类可以通过某种中间件桥在两种语言之间传递。

Avro 和 Thrift 都提供了创建可以相互通信的客户端和服务器的方法,而我相信您只能依靠自己的原始缓冲区。

以下是在 PHP 中设置 Thrift 客户端的教程: http ://chanian.com/2010/05/13/thrift-tutorial-a-php-client/

当然,最好的想法是使用运行于ON JVM 然后您将可以直接访问所有 J​​ava 库。您将能够调用Java对象,就像它们是普通的旧PHP对象一样。 JVM 托管的动态语言也变得非常快,尤其是使用 JDK 7。 Quercus(运行的 PHP 实现)在 JVM 上)实际上已被证明更快 (快 4 倍!) 比主流 PHP 解释器快。

另外,如果您在 JVM 上运行 PHP,那么您可以生成线程并排队后台工作...以及您将能够访问的所有其他新库...切换到 Quercus 是一个巨大的胜利。

You can use something like Thrift, Google Protocol Buffers or Avro to generate an interface and pass messages between the two languages. This is similar to what Facebook does to communicate between their PHP and other code.

Taking Google proto buffers as an example, you'll create a .proto file that defines some structure. You then run the proto compiler on that .proto file and it will generate corresponding PHP and Java classes which can be passed between the two languages over some middleware bridge.

Avro and Thrift both come with ways to create clients and servers that can talk to one another, where as I believe you are on your own with proto buffers.

Here is a tutorial on setting up a Thrift client in PHP: http://chanian.com/2010/05/13/thrift-tutorial-a-php-client/

Of coures the best idea is to go with a PHP implementation that runs ON the JVM and then you will have direct access to all Java libraries. You will be able to call Java objects like they are plain old PHP objects. JVM hosted dynamic languages are also getting very fast, especially w/ JDK 7. Quercus (an implementation of PHP that runs on the JVM) has actually been shown to be faster (4x faster!) than the mainstream PHP interpreter.

Also, if you are running PHP on the JVM then you can spawn threads and queue up background work... and all the other new libraries you'll be able to access... it is a huge win to switch to Quercus.

温柔一刀 2024-12-10 22:45:50

您应该考虑 php-java-bridge。我过去在一个带有 php 前端和 java 后端的项目中使用过它。

引用他们的网站:

PHP/Java Bridge 是基于 XML 的流式网络协议的实现,可用于将本机脚本引擎(例如 PHP、Scheme 或 Python)与 Java 虚拟机连接起来。它比通过 SOAP 的本地 RPC 快 50 倍,并且需要更少的 Web 服务器端资源。它比通过 Java 本机接口直接通信更快、更可靠,并且不需要额外的组件即可从 PHP 调用 Java 过程或从 Java 调用 PHP 过程。

编辑:我在另一个SO回答缺点一个>

You should consider the php-java-bridge. I have used it in the past on a project with a php front end and a java backend.

Quoting from their website:

The PHP/Java Bridge is an implementation of a streaming, XML-based network protocol, which can be used to connect a native script engine, for example PHP, Scheme or Python, with a Java virtual machine. It is up to 50 times faster than local RPC via SOAP, requires less resources on the web-server side. It is faster and more reliable than direct communication via the Java Native Interface, and it requires no additional components to invoke Java procedures from PHP or PHP procedures from Java.

Edit: I explained the disadvantages of using Quercus in another SO answer

猥琐帝 2024-12-10 22:45:50

我不确定代码点火器的结构如何执行其代码,但我刚刚发现了与此问题类似的其他问题: 从网站上的 PHP 脚本运行 Java 类文件

I'm not sure how code igniter is structured executes its code, but i just found this other question similar to this question: Run Java class file from PHP script on a website

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文