首页 > Web开发 > 详细

通过PHP调用天市数据的火车查询接口

时间:2016-10-21 13:25:59      阅读:280      评论:0      收藏:0      [点我收藏+]

前置条件 
在开始前,请作如下准备 
1.学会用PHP输出“Hello World” 
2.去 天市数据 申请火车查询专用的KEY

操作步骤 
1.配置好PHP开发环境 
2.新建一个index.php文件,并输入以下内容:

<?php

require_once ‘curl.func.php‘;

$appkey = ‘your_appkey_here‘;//你在天市申请的appkey
$start = ‘杭州‘;//utf8
$end = ‘北京‘;//utf8
$ishigh = 0;
$url = "http://http://apis.tiisi.com/train/line?appkey=$appkey&start=$start&end=$end&ishigh=$ishigh";
$result = curlOpen($url);
$jsonarr = json_decode($result, true);
//exit(var_dump($jsonarr));
if($jsonarr[‘status‘] != 0)
{
    echo $jsonarr[‘msg‘];
    exit();
}

foreach($jsonarr[‘result‘] as $val)
{
    echo $val[‘trainno‘].‘ ‘.$val[‘type‘].‘ ‘.$val[‘station‘].‘ ‘.$val[‘endstation‘].‘ ‘.$val[‘departuretime‘].‘ ‘.$val[‘arrivaltime‘].‘<br>‘;  
}

3.打开浏览器,访问http://localhost/index.php,正常情况下你应该看到类似下面的内容:

{
    "status": "0",
    "msg": "ok",
    "result": [
        {
            "trainno": "G34",
            "type": "高铁",
            "station": "杭州东",
            "endstation": "北京南",
            "departuretime": "07:18",
            "arrivaltime": "13:07",
            "sequenceno": "1",
            "costtime": "5时49分",
            "distance": "1279",
            "isend": "1",
            "pricesw": "",
            "pricetd": "",
            "pricegr1": "",
            "pricegr2": "",
            "pricerw1": "0.0",
            "pricerw2": "0.0",
            "priceyw1": "0.0",
            "priceyw2": "0.0",
            "priceyw3": "0.0",
            "priceyd": "907.0",
            "priceed": "538.5"
        },
        {
            "trainno": "G32",
            "type": "高铁",
            "station": "杭州东",
            "endstation": "北京南",
            "departuretime": "08:30",
            "arrivaltime": "13:28",
            "sequenceno": "1",
            "costtime": "4时58分",
            "distance": "1279",
            "isend": "1",
            "pricesw": "",
            "pricetd": "",
            "pricegr1": "",
            "pricegr2": "",
            "pricerw1": "0.0",
            "pricerw2": "0.0",
            "priceyw1": "0.0",
            "priceyw2": "0.0",
            "priceyw3": "0.0",
            "priceyd": "907.0",
            "priceed": "538.5"
        }
    ]
}

通过PHP调用天市数据的火车查询接口

原文:http://www.cnblogs.com/tangsmall/p/5984139.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!