PHP calls Java WebService simple instance _php tutorial

Source: Internet
Author: User
Use PHP to invoke the webservice developed by the Java language.
The client submits two arguments of type string, 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.

Service side

Object class
Copy the Code code 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.) {
This.age = age;
}

public Boolean getsex () {
return sex;
}

public void Setsex (Boolean sex) {
This.sex = sex;
}
}

Service class
Copy the Code code 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 ("Xiao Li");
Aperson.setage (23);
Aperson.setsex (FALSE);
} else {
Aperson = null;
}
return Aperson;
}

}

Client
Copy the Code code 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 follows would 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

';
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 ' = ' 123456 ');
$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 '
';
}
}
?>

http://www.bkjia.com/PHPjc/740216.html www.bkjia.com true http://www.bkjia.com/PHPjc/740216.html techarticle use PHP to invoke the webservice developed by the Java language. The client submits two arguments of type string, and the server returns an object type. The server uses AXIS-1.4 as the SOAP engine. The client is ...

  • 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.