Home >Backend Development >PHP Tutorial >How to implement communication between PHP and Java_PHP Tutorial
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
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
•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:
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
Prove that the extension is installed correctly and working properly.
demo1 (calling java standard class)
demo2 (call custom class)
Create a new test directory under the /data directory, and then create a Hello.java file as a test
public String getHi(){
return "hi, every sudt linux member";
}
}
After packaging our Hello.class into a jar package, we can then directly call the getHi() method by instantiating a Java class in PHP.
Run results:
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.