Table of Contents
Constructor error
Fault
Error
Result
Home php教程 php手册 PHP调用JAVA的WebService简单实例

PHP调用JAVA的WebService简单实例

Jun 06, 2016 pm 08:24 PM

本篇文章主要是对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;
    }

}


客户端

复制代码 代码如下:


/*
 * 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 . '
Copy after login
Copy after login
Copy after login
';
    // 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 '

Fault

';<br>    print_r($result);<br>    echo '
Copy after login
Copy after login
';
} else {
    // Check for errors
    $err = $client->getError();
    if ($err) {
        // Display the error
        echo '

Error

' . $err . '
Copy after login
Copy after login
Copy after login
';
    } else {
        // Display the result
        echo '

Result

';<br>        print_r($result);<br>        echo '
Copy after login
Copy after login
';
    }
}
echo '
';
$param=array('loginName'=>'xiaoli', 'loginPasswd'=>'123456');
$result = $client->call('login', $param);
// Check for a fault
if ($client->fault) {
    echo '

Fault

';<br>    print_r($result);<br>    echo '
Copy after login
Copy after login
';
} else {
    // Check for errors
    $err = $client->getError();
    if ($err) {
        // Display the error
        echo '

Error

' . $err . '
Copy after login
Copy after login
Copy after login
';
    } else {
        // Display the result
        echo '

Result

';<br>        print_r($result);<br>        echo '
Copy after login
Copy after login
';
    }
}
?>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)