This article mainly introduces the simple WebService instance for PHP to call JAVA. if you need it, you can refer to it and hope to help you use PHP to call the WebService developed in JAVA.
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 NuSOAP is used as the SOAP engine.
Server
Object class
The code is as follows:
Import java. io. Serializable;
Public class Person implements Serializable {
/**
*
*/
Private static final long serialVersionUID =-415186774891162281l;
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
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 ("Lao Li ");
APerson. setAge (55 );
APerson. setSex (true );
} Else if (loginName. equals ("xiaoli") & loginPasswd. equals ("123456 ")){
APerson. setUsername ("Lili ");
APerson. setAge (23 );
APerson. setSex (false );
} Else {
APerson = null;
}
Return aPerson;
}
}
Client
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' => '123 ');
$ Result = $ client-> call ('login', $ param );
// Check for a fault
If ($ client-> fault ){
Echo 'Fault
';
print_r($result);
echo '
';
} Else {
// Check for errors
$ Err = $ client-> getError ();
If ($ err ){
// Display the error
Echo 'error
' . $err . '
';
} Else {
// Display the result
Echo 'result
';
print_r($result);
echo '
';
}
}
Echo'
';
$ Param = array ('loginname' => 'Xiaoli', 'loginpasswd' => '20140901 ');
$ Result = $ client-> call ('login', $ param );
// Check for a fault
If ($ client-> fault ){
Echo 'Fault
';
print_r($result);
echo '
';
} Else {
// Check for errors
$ Err = $ client-> getError ();
If ($ err ){
// Display the error
Echo 'error
' . $err . '
';
} Else {
// Display the result
Echo 'result
';
print_r($result);
echo '
';
}
}
?>