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

相关文章

smarty实现多级分类的方法

smarty实现多级分类的方法

本文实例讲述了smarty实现多级分类的方法。分享给大家供大家参考。具体分析如下: 这里简单的介绍一下利用php smarty 多级分类读出与循环方法,单循环很简单,但是多级就要复杂一点...

ZF等常用php框架中存在的问题

从Zend Framework v0.13版本开始,我就开始学习使用Zend Framework。当时公司的一个项目也恰好用到了Zend Fram...

PHP 模板高级篇总结

如何使用PHP来快速地编写代码,模版似乎成了唯一的选择。但是一个PHPer最终应该坚持使用模版,放弃模版,还是使用自己的模版?     以下想法是...

PHP基于rabbitmq操作类的生产者和消费者功能示例

本文实例讲述了PHP基于rabbitmq操作类的生产者和消费者功能。分享给大家供大家参考,具体如下: 注意事项: 1、accept.php消费者代码需要在命令行执行 2、'usernam...

php中实现字符串翻转的方法

字符串:$str = "abcdefg"; 方法一(直接使用php自带函数strrev($str)) print_r(strrev($str)); 使用for循环方式,str_split...