Implementation of communication between PHP and Java

Source: Internet
Author: User

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 calls the command line through commands such as exec or system, and then runs java programs in the form of java Hello, but the disadvantage is obvious, it cannot interact well with various methods in the java class, and the running result obtained in this way also has a limit on the number of rows, 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.
• Install the TomCat server to publish the relevant Java 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 java
Yum install java * jdk * devel *

Test java-version. If the following result is output, the java environment is successfully installed.

Copy codeThe 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, 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.

Copy codeThe 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, use yum install php-devel.
• 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, the java. so file and JavaBridge. jar will be added to the php lib/php/extensions/no-debug-non-zts-20060613

Step 3

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

Copy codeThe 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 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

Copy codeThe Code is as follows:
Phpinfo ();


Running result appears

Verify that the extension is correctly installed and can be used properly.

Demo1 (calls java standard class)

Copy codeThe Code is 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 (call custom class)

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

Copy codeThe 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 codeThe 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 directly call the getHi () method by instantiating a Java class in PHP.

Copy codeThe 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 );

Running result:

Copy codeThe Code is as follows:
[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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.