XML应用之Web Service

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XML应用之Web Service相关的知识,希望对你有一定的参考价值。

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 = 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的主要内容,如果未能解决你的问题,请参考以下文章

构建 Web 应用之 Service Worker 初探

PWA之 Service worker

Web service

Web Service 之Axis

Web Service 实现之CXF

maven web项目的web.xml报错The markup in the document following the root element must be well-formed.(代码片段