PHP调用JAVA的WebService简单实例
使用php调用java语言开发的webservice。
客户端提交两个string类型的参数,服务端返回一个对象类型。
服务端使用axis-1.4作为soap引擎。客户端为php5.2.9,使用nusoap作为soap引擎。
服务端
对象类
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;
}
}
服务类
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") && loginpasswd.equals("123456")) {
aperson.setusername("小丽");
aperson.setage(23);
aperson.setsex(false);
} else {
aperson = null;
}
return aperson;
}
}
客户端
<?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>';
}
}
?>
上一篇: 明末清初著名文学批判家——金圣叹生平简介
下一篇: 怎么样才能把蓑衣黄瓜做的好味道?