前置条件
在开始前,请作如下准备
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"
}
]
}
原文:http://www.cnblogs.com/tangsmall/p/5984139.html