PHP调用JAVA的WebService简单实例

yipeiwu_com5年前PHP代码库

使用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>';
    }
}
?>

相关文章

海河写的 Discuz论坛帖子调用js的php代码

调用代码 <script language="javascript" src="js_bbs.php?fid=1"></script> js_...

PHP错误和异长常处理总结

通过日志记录功能,你可以将信息直接发送到其他日志服务器,或者发送到指定的电子邮箱(或者通过邮件网关发送),或者发送到操作系统日志等,从而可以有选择的记录和监视你的应用程序和网站的最重要的...

PHP数字金额转换成中文大写显示

整个功能其实不难,只是还算挺实用,所以记一下哈,其他编程语言转换一下也是能可以的! 思路:把传过来的金额转换成整数和小数两部分,再对其分别进行转换处理 代码附上: function...

php中用socket模拟http中post或者get提交数据的示例代码

废话不多说。直接上代码:sock_post.php:复制代码 代码如下:<?phpfunction sock_post($url, $data='') {  $url =...

学习php设计模式 php实现备忘录模式(Memento)

学习php设计模式 php实现备忘录模式(Memento)

一、意图 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样可以在以后把该对象的状态恢复到之前保存的状态。 二、备忘录模式结构图 三、备忘录模式中主要角色...