Origin:
Recently made an e-commerce platform and net silver integration of Small things, the program is open source Ecmall, the interface is also very standardized, the document is very full, the only small problem is that the use of network silver signature and verification of the Lib is only Java and C, Java is also familiar with some, So we chose to use Java as the interface for signing and checking.
Method:
There's actually a lot of data on the web about PHP interacting with Java. In general, there are several ways to do this:
PHP calls the command line directly from commands such as exec or system, and then runs Java programs in the same way as Java Hello, but the downside is obvious and does not interact well with the various methods inside the Java class. And the result of this approach is also the number of rows limit, so discard.
PHP and Java through the WebService way to communicate, their own on the Java side Open related WebService services, and then through XML or JSON to let PHP call Java WebService, this way compared to the public, Features that can be customized are also strong, but the disadvantage page is very obvious
• To install the Tomcat server, publish the relevant messages on the Java side
WebService to write the certification, the signing and verification process to make the relevant security certification
PHP through the Php-java-bridge This module, the implementation of PHP and Java communication, this module configuration is relatively simple installation, so chose to use this module for PHP and Java communication
The first step
Install Java Environment and PHP environment PHP environment slightly centos under the Yum Way install Java environment
Yum Install Java
Yum Install yum install java*jdk*devel*
Test Java-version If there is output similar to the following results, the Java environment installation succeeds
Copy the 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)
Step Two
Compiling and installing the Php-java-bridge module
Download the package:
php-java-bridge4.0 This version is 4.0, the latest version should be 6, there is a download on sourceforg, but 6 usage and 4 usage gap seems to be a bit big
Compile and install:
Unzip the Php-java-bridge, enter the Php-java-bridge directory, and compile the Php-java-bridge into PHP extensions
Copy the 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 it is run, you can resolve it via yum install Php-devel
In configure, the Java parameter is populated with the JDK and JRE path, which is the specific directory of the Php-config file in PHP.
• java.so files and Javabridge.jar will be added to PHP lib/php/extensions/no-debug-non-zts-20060613 after the compilation is complete
Step Three
Configuring related parameters
Open the php.ini file and add the following parameters at the end:
Copy the 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 is mod_php mode, restart Apache
If PHP is running in fast_cgi mode, restart PHP-FPM and Nginx or Apache server
Fourth Step
Test
Copy the Code code as follows:
Phpinfo ();
Appears in the run result
Prove that the extension is properly installed and can be used normally.
DEMO1 (call Java Standard Class)
Copy the Code code as follows:
$system =new Java ("Java.lang.System");
Print "Java version=". $system->getproperty ("Java.version"). " ";
$str =new Java ("java.lang.String");
Echo $str;
Demo2 (calls to custom class)
Create a new test directory under the/data directory, and then make a Hello.java file for testing
Copy the 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 the 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 the 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);
Operation Result:
Copy the Code code as follows:
[O (String): "Hi, every Sudt Linux member"]
String "Hi, every Sudt Linux member"
A little bit of a problem to note:
1. The public String Gethi () in Java cannot omit public, otherwise the default Gethi () method is private, so it cannot be called in PHP.
2. After invoking the Java class in PHP, the returned result is the O (variable) of Java, it is better to do a forced type conversion, converted to PHP variable type to use.
3. When we're in Java_require (), it's best to use absolute paths, so we don't have to put the jar packages we want to call into the LIBPATH we've configured in php.ini.
4. In new Java (), the first letter of the class name must be larger, otherwise it will error, Java will not find this class.
http://www.bkjia.com/PHPjc/824892.html www.bkjia.com true http://www.bkjia.com/PHPjc/824892.html techarticle Origin: Recently made an e-commerce platform and net silver integration of Small things, the program is open source Ecmall, the interface is also very standardized, the document is very full, the only small problem is that the net ...