码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2016-10-21 13:27:26      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:word   change   key   status   http   iis   repr   decode   index.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调用天市数据的火车查询接口

标签:word   change   key   status   http   iis   repr   decode   index.php   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!