Home  >  Article  >  Backend Development  >  How to implement communication between PHP and Java_PHP Tutorial

How to implement communication between PHP and Java_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:25:58888browse

Origin:

I recently made a small thing that integrates an e-commerce platform with online banking. The program is open source Ecmall. The online banking interface is also very standardized and the documents provided are very complete. The only small problem is that the signature and verification used by online banking are The signed libs are only Java and C. I am still familiar with Java, so I chose to use Java as the interface for signing and verification.

Method:

There is actually quite a lot of information on the interaction between php and java on the Internet. Generally speaking, there are several methods:

•PHP directly calls the command line through commands such as exec or system, and then runs the java program in a java Hello type. However, the shortcomings are obvious and cannot be well integrated with various functions in the java class. method to interact, and the final running result obtained in this way is also limited in the number of rows, so it is discarded.
•PHP and Java communicate through WebService. You open the relevant WebService on the Java side, and then let PHP call Java's WebService through XML or JSON. This method is more popular, and the functions it can achieve are also customizable. Strong, but the shortcomings are very obvious
•You need to install TomCat server to publish relevant messages on the Java side
•WebService needs to write authentication and make relevant security authentication for the signature and signature verification process
• PHP implements communication between PHP and Java through the PHP-JAVA-BRIDGE module. The configuration and installation of this module are relatively simple, so I chose to use this module for communication between PHP and Java

Step 1

Install the java environment and PHP environment. The PHP environment is abbreviated. Install the JAVA environment using YUM under CentOS

yum install java
yum install yum install java*jdk*devel*

Test java-version. If there is a result similar to the following output, the java environment is successfully installed

Copy code The code is as follows:

java version "1.7.0_25"
OpenJDK Runtime Environment (rhel-2.3. 10.4.el6_4-x86_64)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)

Step 2

Compile and install the php-java-bridge module

Download package:

PHP-JAVA-BRIDGE4.0 This version is 4.0. The latest version should be 6. It can be downloaded from sourceforg, but the usage of 6 and the usage of 4 seems to be a bit different

Compile and install:

Unzip php-java-bridge, enter the directory of php-java-bridge, and compile php-java-bridge into a php extension

Copy code The code is as follows:

tar xzvf php-java-bridge_4.0.1.orig.tar.gz
cd php-java-bridge
phpize
./configure --disable-servlet --with-java= --with-php-config=/usr/local//php/bin/php-config
make
make install

•If the phpize command is invalid after running, it can be solved by yum install php-devel
•In configure, the java parameter is filled in the path of jdk and jre, and in php, the specific directory of the php-config file is filled in
•After compilation is completed, the java.so file and JavaBridge.jar will be added to php's lib/php/extensions/no-debug-non-zts-20060613

Step 3

Configure relevant parameters
Open the php.ini file and add the following parameters at the end:

Copy code The code is as follows:

extension="/usr/local/php/lib/php/extensions/no- debug-non-zts-20060613/java.so"
[java]

java.java_home="/usr/lib/jvm/java-1.7.0-openjdk.x86_64"
java.java="/usr/lib/jvm/java-1.7.0-openjdk.x86_64/ jre/bin/java"
java.log_file="/var/log/php-java-bridge.log"
java.classpath="/usr/local/php/lib/php/extensions/no- debug-non-zts-20060613/JavaBridge.jar"
java.libpath="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613"
java.log_level= "2"

If it is running in mod_php mode, then restart Apache

If it is php running in fast_cgi mode, then restart php-fpm and nginx or apache server

Step 4

Test

Copy code The code is as follows:

phpinfo();


Run result Appears in

How to implement communication between PHP and Java_PHP Tutorial

Prove that the extension is installed correctly and working properly.

demo1 (calling java standard class)

Copy code The code is as follows:

$system=new Java("java.lang. System");
print "Java version=".$system->getProperty("java.version")." ";
$str=new Java("java.lang.String");
echo $str;

demo2 (call custom class)

Create a new test directory under the /data directory, and then create a Hello.java file as a test

Copy code The code is as follows:

public class Hello
{
public static void main(String[] args)
{
System.out.println("php java bridge test");
}

public String getHi(){
return "hi, every sudt linux member";
}
}

Copy code The code is as follows:

javac Hello.java
java Hello
jar cvf hello.jar Hello .class

After packaging our Hello.class into a jar package, we can then directly call the getHi() method by instantiating a Java class in PHP.

Copy code The code is as follows:

ini_set('display_errors', 1);
java_require('/data/ test/hello.jar');
$hello = new Java('Hello');
$hi = $hello->getHi();
echo $hi;
$php_hi = (string) $hi;
var_dump($php_hi);

Run results:

Copy code The code is as follows:

[o(String):"hi, every sudt linux member"]
string(27) "hi, every sudt linux member"

A few little things to note:

 1. Public cannot be omitted for public String getHi() in java, otherwise the default getHi() method is private and cannot be called in PHP.

2. After calling a java class in PHP, the return result is java's o(variable). It is best to do a forced type conversion and convert it into a PHP variable type for use.

3. When we are in java_require(), it is best to use an absolute path, so that we do not have to put the jar package to be called into the libpath we configured in php.ini.

4. When using new Java(), the first letter of the class name must be larger, otherwise an error will be reported and Java will not be able to find the class.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824892.htmlTechArticleOrigin: I recently made a small thing that integrates an e-commerce platform with online banking. The program is open source Ecmall, which is used for online banking. The interface is also very standardized, and the documentation provided is very complete. The only small problem is that the network...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn