简单使用web服务

来源:互联网 发布:tensorflow 多gpu 编辑:程序博客网 时间:2024/05/21 20:10

web服务是使各个组织任意数量的进程之间能够以平台无关和语言无关的方式进行无缝链接。按W3C组织的定义,Web服务是通过统一资源标识URI(Uniform Resource Identifiers)标识的软件系统,它的共用接口和绑定用XML来定义和描述。Web Service的定义能够被其它的软件系统发现,这些系统然后可以通过Internet协议传递基于XML的消息,这样就可以用Web Service 所定义的方式与其交互。

我们在网站上可以经常看到登录此网站,除了由用户名登录,还可以使用QQ登录,微信登录等。其实是这个公司为了提高软件的知名度,提供外界的一个借口,使外界不用访问他们的数据库却可以查到部分信息。

使用QQ登录接口有点繁琐,我还没有使用过。本例我将使用PHP语言访问一个免费的提供web服务的网站,查询飞机航班信息。

首先打开提供web服务的网站http://www.webxml.com.cn/zh_cn/web_services.aspx

第一个网址是提供了两个方法,getDomesticAirlinesTime 获得航班时刻表 DataSet

getDomesticCity 获得这国内飞机航班时刻表Web Services支持的全部城市中英文名称和缩写 DataSet

  以下是获取航班信息的代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<?php
$client=new SoapClient('http://ws.webxml.com.cn/webservices/DomesticAirline.asmx?wsdl');
$city=$client->getDomesticCity()->getDomesticCityResult->any;
$simple=new simpleXMLElement($city);
$arr=$simple->Airline1->Address;
    
?>
<form action="" method="post">
    出发城市:<select name="start_city">
            <?php foreach ($arr as $a):?>
            <option value="<?php echo $a->cnCityName;?>"><?php echo $a->cnCityName;?></option>
            <?php endforeach;?>
        </select>
           到达城市:<select name="end_city">
            <?php foreach ($arr as $a):?>
            <option value="<?php echo $a->cnCityName;?>"><?php echo $a->cnCityName;?></option>
            <?php endforeach;?>
        </select>
    出发日期:<input type="text" name="start_time" value="<?php echo date('Y-m-d');?>" />
    <input type="submit" value="提交">
    </form>
<?php   
    if (!empty($_POST)){
        $start_city=$_POST['start_city'];
        $end_city = $_POST['end_city'];
        $start_time = $_POST['start_time'];
        $air_arr=array(
                'startCity'=>$start_city,
                'lastCity'  =>$end_city,
                'theDate'=>$start_time,
                'userID' =>''
            );
        $time=$client->getDomesticAirlinesTime($air_arr)->getDomesticAirlinesTimeResult->any;
        $simple = new simpleXMLElement($time);
        $air_line=$simple->Airlines->AirlinesTime
?>
<table border='1' width='80%' align='center'>
    <tr>
        <th>航空公司</th>
        <th>航班号</th>
        <th>出发机场</th>
        <th>到达机场</th>
        <th>出发时间</th>
        <th>到达时间</th>
        <th>机型</th>
        <th>经停</th>
        <th>飞行周期</th>
    </tr>
    <?php foreach ($air_line as $air):?>
    <tr>
        <td><?php echo $air->Company;?></td>
        <td><?php echo $air->AirlineCode;?></td>
        <td><?php echo $air->StartDrome;?></td>
        <td><?php echo $air->ArriveDrome;?></td>
        <td><?php echo $air->StartTime;?></td>
        <td><?php echo $air->ArriveTime;?></td>
        <td><?php echo $air->Mode;?></td>
        <td><?php echo $air->AirlineStop;?></td>
        <td><?php echo $air->Week;?></td>
    </tr>
    <?php endforeach;?>
</table>
<?php 
    }
?>
</body>
</html>

查询北京到上海的飞机,如下图:


0 0
原创粉丝点击