首页 > 代码库 > 通过PHP调用天市数据的火车查询接口
通过PHP调用天市数据的火车查询接口
前置条件
在开始前,请作如下准备
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调用天市数据的火车查询接口
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。