PHP calls JAVA WebService simple instance

Source: Internet
Author: User
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 '
';
}
}
?>

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.