Home  >  Article  >  Backend Development  >  A simple example of PHP calling JAVA's WebService_PHP tutorial

A simple example of PHP calling JAVA's WebService_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:36:12755browse

Use PHP to call WebService developed in JAVA language.
The client submits two String type parameters, and the server returns an object type.
The server uses AXIS-1.4 as the SOAP engine. The client is PHP5.2.9 and uses NuSOAP as the SOAP engine.

Server

Object class

Copy code The code is as follows:

import java.io.Serializable;

public class Person implements Serializable {
/**
     *
    */
private static final long serialVersionUID = -410186774891162281L;
private String username;
private int age;
private boolean sex;// true:male;false:female

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public boolean getSex() {
return sex;
}

public void setSex(boolean sex) {
this.sex = sex;
}
}


Service class
Copy code The code is as follows:

public class UserLogin {

public Person login(String loginName, String loginPasswd) {
Person aPerson = new Person();
if (loginName.equals("laoli") && loginPasswd.equals("111111")) {
aPerson.setUsername("老李");
aPerson.setAge(55);
aPerson.setSex(true);
} else if (loginName.equals("xiaoli") && loginP asswd. equals("123456")) {
aPerson.setUsername("Xiaoli");
aPerson.setAge(23);
aPerson.setSex(false);
} else {
aPerson = null;
}
return aPerson;
}

}


Client
Copy code The code is as follows:

/*
 * Created on 2011-10-12
 * Author wanghao
 *
 * package_name/userLoginClient.php
 */
header("Content-Type: text/html;charset=utf-8");
// Pull in the NuSOAP code
require_once ("libs/nusoap.php");
// Create the client instance
$client = new nusoapclient('http://localhost:8080/axis/services/UserLoginWS?wsdl', true);
$client->soap_defencoding = 'utf-8';
$client->decode_utf8 = false;
$client->xml_encoding = 'utf-8';
// Check for an error
$err = $client->getError();
if ($err) {
    // Display the error
    echo '

Constructor error

' . $err . '
';
    // At this point, you know the call that follows will fail
}
// Call the SOAP method
$param=array('loginName'=>'laoli', 'loginPasswd'=>'111111');
$result = $client->call('login', $param);
// Check for a fault
if ($client->fault) {
    echo '

Fault

';<br>    print_r($result);<br>    echo '
';
} else {
    // Check for errors
    $err = $client->getError();
    if ($err) {
        // Display the error
        echo '

Error

' . $err . '
';
    } else {
        // Display the result
        echo '

Result

';<br>        print_r($result);<br>        echo '
';
    }
}
echo '
';
$param=array('loginName'=>'xiaoli', 'loginPasswd'=>'123456');
$result = $client->call('login', $param);
// Check for a fault
if ($client->fault) {
    echo '

Fault

';<br>    print_r($result);<br>    echo '
';
} else {
    // Check for errors
    $err = $client->getError();
    if ($err) {
        // Display the error
        echo '

Error

' . $err . '
';
    } else {
        // Display the result
        echo '

Result

';<br>        print_r($result);<br>        echo '
';
    }
}
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/740216.htmlTechArticle使用PHP调用JAVA语言开发的WebService。 客户端提交两个String类型的参数,服务端返回一个对象类型。 服务端使用AXIS-1.4作为SOAP引擎。客户端为...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:3.12php_PHP tutorialNext article:3.12php_PHP tutorial