Origin:
Recently made an electric business platform and net silver integration of Small things, the program is open source Ecmall, the network Silver interface is also very standard, give the document is very complete, the only small problem is, net silver use of the signature and verification of the Lib is only Java and C, Java is also familiar with some, So you chose to use Java as your signature and verification interface.
Method:
There are actually a lot of data about PHP interacting with Java on the web. In general, there are several ways to do this:
PHP invokes the command line directly with commands such as exec or system, and then runs the Java program in Java Hello, but the drawbacks are obvious and do not interact well with the various methods in the Java class. And the result of this way of getting the results of the operation also has the line number limit, so discard.
PHP communicates with Java in a WebService way, opens its own WebService services in the Java side, and then lets PHP invoke Java WebService via XML or JSON, a way that compares to the public, The ability to achieve customization is also strong, but the disadvantage of the page is very obvious
• To install a Tomcat server to publish related messages from the Java side
WebService to write certification, the signature and verification process to make the relevant security certification
PHP through Php-java-bridge This module, the implementation of PHP and Java communication, this module configuration installation relatively simple, so chose to use this module for PHP and Java communication
First step
Install Java Environment and PHP environment PHP environment slightly centos under Yum installation Java environment
Yum Install Java
Yum Install yum install java*jdk*devel*
Test Java-version The Java environment is installed successfully if there is output similar to the following result
Copy Code code 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)
Second Step
Compiling and installing the Php-java-bridge module
Download package:
php-java-bridge4.0 This version is 4.0, the latest version should be 6, on the sourceforg there are downloads, but 6 of usage and 4 of the usage gap seems to be a bit big
Compile Installation:
Unzip the Php-java-bridge, enter the Php-java-bridge directory, and compile the Php-java-bridge into a php extension
Copy Code code 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 not valid after running, it can be resolved through yum install Php-devel
In configure, the Java parameter is filled in with the JDK and JRE path. PHP fills in the directory of Php-config files
• After compiling, java.so files and Javabridge.jar will be added to the PHP lib/php/extensions/no-debug-non-zts-20060613
Third Step
Configure related parameters
Open the php.ini file and add the following parameters at the end:
Copy Code code 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's mod_php, then restart Apache
If PHP is running fast_cgi, restart PHP-FPM and Nginx or Apache servers
Fourth Step
Test
Copy Code code as follows:
The results of the run appear
Proof that the extension is properly installed and can be used normally.
DEMO1 (invoke Java Standard Class)
Copy Code code as follows:
<?php
$system =new Java ("Java.lang.System");
Print "Java version=". $system->getproperty ("Java.version"). " ";
$str =new Java ("java.lang.String");
Echo $str;
Demo2 (Invoke custom Class)
Create a new test directory under the/data directory, and then make a Hello.java file for testing
Copy Code code 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 code as follows:
Javac Hello.java
Java Hello
Jar CVF Hello.jar Hello.class
After packing our hello.class into a jar package, we can then invoke the Gethi () method directly in PHP by instantiating a Java class.
Copy Code code 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 Result:
Copy Code code as follows:
[O (String): "Hi, every Sudt Linux member"]
String "Hi, every Sudt Linux member"
A small problem to note:
1. Public String Gethi () in Java cannot omit public, otherwise the default Gethi () method is private so that it cannot be invoked in PHP.
2. After invoking Java classes in PHP, the return result is Java's O (variable), which is best done by forcing type conversions and converting them into PHP variable types for use.
3. When we Java_require (), we'd better use an absolute path, so we don't have to put the jar package we have to call into the LIBPATH in our php.ini configuration.
4. In the new Java (), the first letter of the class name must be larger, otherwise it will be an error, Java will not find this class.