PHP調用JAVA的WebService簡單一實例

來源:互聯網
上載者:User

 本篇文章主要是對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 coderequire_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 faultif ($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 faultif ($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>';    }}?> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.