PHP communicates with Java,
Origin:
Recently, an e-commerce platform is integrated with online banking. The program is open-source Ecmall, the online banking interface is also very standard, and the documentation is very complete. The only small problem is that, the signature and verification lib used by online banking are only java and c, and are familiar with java. Therefore, java is used as the signature and verification interface.
Method:
There are actually a lot of information on the interaction between php and java on the Internet. In general, there are also several methods:
- PHP directly uses
execOrsystemAndjava HelloThis method is used to run java programs, but its disadvantage is obvious. It cannot interact well with various methods in java classes, in addition, this method also limits the number of rows for the final running result, so it is discarded.
- PHP communicates with Java through WebService, opens relevant WebService services on the Java end, and then enables PHP to call Java WebService through XML or JSON. This method is quite popular, features that can be implemented are customizable, but the disadvantage pages are obvious.
- To install the TomCat server, publish the Java-related packets.
- WebService requires write authentication and security authentication on the signature and signature verification processes
- PHP through the PHP-JAVA-BRIDGE module, PHP and Java communication, this module configuration and installation is relatively simple, so the use of this module for PHP and java Communication
Step 1
Install the java environment and PHP environment. Install the JAVA environment in YUM mode in CentOS.
yum install javayum install yum install java*jdk*devel*
Testjava-versionIf the following result is output, the java environment is successfully installed.
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, there is download on sourceforg, but 6 usage and 4 usage gap seems a little big
Compile and install:
Decompress php-java-bridge, enter the php-java-bridge directory, and compile php-java-bridge into php extension.
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, you can use
yum install php-develSolution
- In configure, the java parameter is filled in the jdk and jre paths. in php, the specific directory of the php-config file is filled in.
- After compilation
lib/php/extensions/no-debug-non-zts-20060613Will addjava.soFile andJavaBridge.jar
Step 3 configure relevant parameters
Openphp.iniFile, add the following parameters at the end:
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 run in mod_php mode, restart Apache
For php running in fast_cgi mode, restart php-fpm and nginx or apache servers.
Step 4 Test
phpinfo();
The running results show that the extension is correctly installed and can be used properly.
Demo1 (calls java standard class)
1
getProperty("java.version")." ";4 $str=new Java("java.lang.String");5 echo $str;
Demo2 (call custom class) creates a test directory under the/data directory, and then creates a Hello. java file for testing.
1 public class Hello 2 { 3 public static void main(String[] args) 4 { 5 System.out.println("php java bridge test"); 6 } 7 8 public String getHi(){ 9 return "hi, every sudt linux member";10 }11 }
javac Hello.javajava Hellojar cvf hello.jar Hello.class
After packaging our Hello. class into a jar package, we can directly call the getHi () method by instantiating a Java class in PHP.
1 ini_set('display_errors', 1);2 java_require('/data/test/hello.jar');3 $hello = new Java('Hello');4 $hi = $hello->getHi();5 echo $hi;6 $php_hi = (string) $hi;7 var_dump($php_hi);
Running result:
[o(String):"hi, every sudt linux member"]string(27) "hi, every sudt linux member"
Notes:
1. The public String getHi () in java cannot be omitted. Otherwise, the default getHi () method is private, which cannot be called in PHP.
2. After the java class is called in PHP, the returned result is java's o (variable). It is best to perform forced type conversion and convert it to PHP's variable type for use.
3. When we use java_require (), we 'd better use the absolute path, so we don't have to put the jar package to be called into the libpath we configured in php. ini.
4. In new Java (), the first letter of the class name must be larger. Otherwise, an error will be reported, and java will not find this class.