首页 / XML / XML应用之Web Service
XML应用之Web Service
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了XML应用之Web Service,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4997字,纯文字阅读大概需要8分钟。
内容图文
![XML应用之Web Service](/upload/InfoBanner/zyjiaocheng/1127/7ed205f2d45747a18555c9f3bac3a018.jpg)
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> </td> </tr> <tr> <td align="center"> <a href="http://www.webxml.com.cn/" target="_blank"></a><strong> 航班时刻表 Web Service 实例</strong> </td> </tr> <tr> <td> </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($cityarras$v):?> <option value="<?php echo $v;?>"><?php echo$v;?></option> <?php endforeach;?> </select> 到达城市 <select name="tocity" > <?php foreach($cityarras$v):?> <option value="<?php echo $v;?>"><?php echo$v;?></option> <?php endforeach;?> </select> <label for="CheckBox1">切换城市</label> </td> <td valign="middle"> 日期 <input name="date" value="2014-12-20" type="text" maxlength="10" size="10" id="date" class="input1" /> <input type="submit" name="button" value="查询" id="button" class="input2" /></td> </tr> </table> </form> </td> </tr> <tr> <td> </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($airlinearras$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>
查询结果如下:
原文:http://www.cnblogs.com/scyang/p/7041614.html
内容总结
以上是互联网集市为您收集整理的XML应用之Web Service全部内容,希望文章能够帮你解决XML应用之Web Service所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。