溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在PHP中調(diào)用WebService函數(shù)

發(fā)布時間:2021-03-09 16:57:46 來源:億速云 閱讀:139 作者:Leah 欄目:開發(fā)技術(shù)

怎么在PHP中調(diào)用WebService函數(shù)?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

對象類

代碼如下:


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

服務(wù)類

復(fù)制代碼 代碼如下:


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

}

客戶端

復(fù)制代碼 代碼如下:


<?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 '<h3>Constructor error</h3><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 '<h3>Fault</h3><pre>';
    print_r($result);
    echo '</pre>';
} else {
    // Check for errors
    $err = $client->getError();
    if ($err) {
        // Display the error
        echo '<h3>Error</h3><pre>' . $err . '</pre>';
    } else {
        // Display the result
        echo '<h3>Result</h3><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 '<h3>Fault</h3><pre>';
    print_r($result);
    echo '</pre>';
} else {
    // Check for errors
    $err = $client->getError();
    if ($err) {
        // Display the error
        echo '<h3>Error</h3><pre>' . $err . '</pre>';
    } else {
        // Display the result
        echo '<h3>Result</h3><pre>';
        print_r($result);
        echo '</pre>';
    }
}
?>

看完上述內(nèi)容,你們掌握怎么在PHP中調(diào)用WebService函數(shù)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI