PHP calls a simple instance of WebService of JAVA

  • 2021-01-19 22:04:06
  • OfStack

WebService, developed in JAVA, is called using PHP.
The client submits two arguments of type String, and the server returns one object type.
The server uses AXIS-1.4 as the SOAP engine. The client is PHP5.2.9, using NuSOAP as SOAP engine.

The service side

Object class


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

public class UserLogin {
    public Person login(String loginName, String loginPasswd) {
        Person aPerson = new Person();
        if (loginName.equals("laoli") && loginPasswd.equals("111111")) {
            aPerson.setUsername(" Lao li ");
            aPerson.setAge(55);
            aPerson.setSex(true);
        } else if (loginName.equals("xiaoli") && loginPasswd.equals("123456")) {
            aPerson.setUsername(" Small beautiful ");
            aPerson.setAge(23);
            aPerson.setSex(false);
        } else {
            aPerson = null;
        }
        return aPerson;
    }
}

The client

<?php
/*
 * 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 '<h2>Constructor error</h2><pre>' . $err . '</pre>';
    // 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 '<h2>Fault</h2><pre>';
    print_r($result);
    echo '</pre>';
} else {
    // Check for errors
    $err = $client->getError();
    if ($err) {
        // Display the error
        echo '<h2>Error</h2><pre>' . $err . '</pre>';
    } else {
        // Display the result
        echo '<h2>Result</h2><pre>';
        print_r($result);
        echo '</pre>';
    }
}
echo '<br>';
$param=array('loginName'=>'xiaoli', 'loginPasswd'=>'123456');
$result = $client->call('login', $param);
// Check for a fault
if ($client->fault) {
    echo '<h2>Fault</h2><pre>';
    print_r($result);
    echo '</pre>';
} else {
    // Check for errors
    $err = $client->getError();
    if ($err) {
        // Display the error
        echo '<h2>Error</h2><pre>' . $err . '</pre>';
    } else {
        // Display the result
        echo '<h2>Result</h2><pre>';
        print_r($result);
        echo '</pre>';
    }
}
?>


Related articles: