XML應用之Web Service

來源:互聯網
上載者:User

標籤:資料   image   mashup   sdl   meta   head   編程   alt   add   

1.什麼是web service?

Service:服務,在電腦安裝一個軟體(程式),可以為我們提供某些功能,就可以稱之為服務。本地服務比較多。

Web:和本地相對應,在互連網上的。

Web Service,通過互連網來提供某種服務,本質就是通過網路調用其他網站的資源。

本地服務的缺陷:

(1) 本地資源不足;很多資料和資料,本地得不到,只有向其他網站要。

(2)成本因素;本地提供服務,往往是不經濟的,使用專業網站的服務更便宜。

(3)可移植性差。

Web Service的優勢:

(1)平台無關。不管你使用什麼平台,都可以使用Web service。

(2) 程式設計語言無關。只要遵守相關協議,就可以使用任意程式設計語言實現Web service。

(3)對於Web service提供者來說,部署、升級和維護Web service都非常簡單

(4)對於Web service使用者來說,可以輕易實現多種資料、多種服務的彙總(mashup),因此能夠做出一些以前根本無法想像的事情。

 

2.Web service核心技術介紹(1).SOAP

SOAP:Simple Object Access Protocol,簡易物件存取通訊協定 (SOAP)。

SOAP = XML + HTTP

(2).WSDL

Web Service Description Language : web 服務描述語言。

簡而言之,就是xml文檔。

使用web service需要在php的設定檔php.ini中開啟擴充,如下

將前面的分好去掉即可

 

3.使用web service實現航班查詢(1).服務在哪兒

http://www.webxml.com.cn/zh_cn/web_services.aspx

該網站還提供了一些其它web service,這裡以航班查詢服務為例

WSDL,其實就是一個xml文檔,如下:

 

提供了一些方法,讓我們調用,從而獲得航班的資訊

點擊進去,如下

具體實現代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head>    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />    <title></title>    <style>        .airline{border:1px solid #3195D3; border-collapse:collapse;}        .airline th{height:35px; line-height:35px; border:1px solid #e2e2e2; font-weight:bold; background:#3195D3; color:#fff;}        .airline td{height:28px; line-height:28px; border:1px solid #e2e2e2; text-align:center; background:#F5FCFF;}    </style></head><body><?php    //執行個體化一個soapclient對象    $client = new SoapClient("http://ws.webxml.com.cn/webservices/DomesticAirline.asmx?wsdl");    $cities = $client->getDomesticCity();    $city = $cities->getDomesticCityResult->any;    $sxe = new SimpleXMLElement($city);    $cityarr = array();    foreach ($sxe->children()->children() as $child){           $cityarr[] = $child->cnCityName;    }    //查詢航班    if(isset($_POST[‘button‘])){        $startCity = $_POST[‘fromcity‘];        $lastCity = $_POST[‘tocity‘];        $theDate = $_POST[‘date‘];        $param = array(            ‘startCity‘ => $startCity,            ‘lastCity‘ => $lastCity,            ‘theDate‘ => $theDate,            ‘userID‘ => ‘‘        );        $res = $client->getDomesticAirlinesTime($param);        $airline = $res->getDomesticAirlinesTimeResult->any;        $sxe = new SimpleXmlElement($airline);        $airlinearr = array();        foreach($sxe->children()->children() as $child){            $airlinearr[] = $child;        }    }?><table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">    <tr>        <td>&nbsp;</td>    </tr>    <tr>        <td align="center">            <a href="http://www.webxml.com.cn/" target="_blank"></a><strong>航班時刻表 Web Service 執行個體</strong>        </td>    </tr>    <tr>        <td>&nbsp;</td>    </tr>    <tr>        <td>               <form name="form1" method="post" action="" id="form1">                <table width="100%" border="0" cellspacing="0" cellpadding="0">                    <tr>                        <td style="width: 50%;">出發城市                        <select name="fromcity" >                        <?php foreach($cityarr as $v):?>                            <option value="<?php echo $v;?>"><?php echo $v;?></option>                       <?php endforeach;?>                        </select>                              &nbsp;&nbsp;&nbsp;到達城市                        <select name="tocity" >                         <?php foreach($cityarr as $v):?>                               <option value="<?php echo $v;?>"><?php echo $v;?></option>                        <?php endforeach;?>                        </select>                        &nbsp;&nbsp;&nbsp;                        <label for="CheckBox1">切換城市</label>                        &nbsp;&nbsp;&nbsp;                    </td>                    <td valign="middle">                            日期                    <input name="date" value="2014-12-20" type="text" maxlength="10" size="10" id="date" class="input1" />                                &nbsp;&nbsp;&nbsp;                    <input type="submit" name="button" value="查詢" id="button" class="input2" /></td>                        </tr>                    </table>                    </form>                </td>            </tr>            <tr>                <td>&nbsp;                    </td>            </tr>            <tr>                <td>                    <table width="100%" border="0" cellpadding="2" cellspacing="1" class="airline">                        <tr>                            <th>航空公司</th>                              <th>航班編號</th>                            <th>出發機場</th>                            <th>出發時間</th>                            <th>到達機場</th>                            <th>到達時間</th>                            <th>機型</th>                            <th>中途是否停</th>                        </tr>                        <?php foreach($airlinearr as $v):?>                         <tr>                            <td class="tdbg"><?php echo $v->Company;?></td>                            <td class="tdbg"><?php echo $v->AirlineCode;?></td>                            <td class="tdbg"><?php echo $v->StartDrome;?></td>                            <td class="tdbg"><?php echo $v->StartTime;?></td>                            <td class="tdbg"><?php echo $v->ArriveDrome;?></td>                            <td class="tdbg"><?php echo $v->ArriveTime;?></td>                            <td class="tdbg"><?php echo $v->Mode;?></td>                            <td class="tdbg"><?php echo $v->AirlineStop;?></td>                        </tr>                    <?php endforeach;?>                    </table>                </td>            </tr>      </table></body></html>

查詢結果如下:

 

XML應用之Web Service

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.