php查询火车票开发实例,index.html

PHP12306火车票查询接口API实例
12306火车票查询 -- 作者:ShenZhongChao

出发地:

目的地:

出发时间:

车次类型:

GC-高铁/城际

D-动车

Z-直达

T-特快

K-快速

车次:

刷票频率:

查询

注意 :

1、输入出发、到达站点时,请选择下拉框中的值,否则无法查询。

2、默认查询全部车次。

3、不填写 车次 默认查询所有符合条件的车次。

4、不填写刷票频率不会进行刷票,刷票频率请填写大于 2 的数字

查询结果 -- 作者:ShenZhongChao

车次出发站
到达站出发时间
到达时间车程历时商务座
特等座一等座二等座高级软卧软卧动卧硬卧软座硬座无座其他备注

var layer;

layui.use('layer', function() {

layer = layui.layer;

});

$(function() {

$(".i-checks").iCheck({checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green", });

var fsSuggest = $("#f_s").bsSuggest({

url: "sites_data/station.json",

idField: "stationFlag",

keyField: "stationName",

//inputWarnColor: '', //输入框内容不是下拉列表选择时的警告色

showBtn: true //显示下拉

}).on("onDataRequestSuccess",

function(e, result) {

//console.log("onDataRequestSuccess: ", result)

}).on("onSetSelectValue",

function(e, keyword) {

$("#f_c").val(keyword.id);

//console.log("onSetSelectValue: ", keyword)

}).on("onUnsetSelectValue",

function(e) {

console.log("onUnsetSelectValue")

});

var tsSuggest = $("#t_s").bsSuggest({

url: "sites_data/station.json",

idField: "stationFlag",

keyField: "stationName",

//inputWarnColor: '', //输入框内容不是下拉列表选择时的警告色

showBtn: true //显示下拉

}).on("onDataRequestSuccess",

function(e, result) {

//console.log("onDataRequestSuccess: ", result)

}).on("onSetSelectValue",

function(e, keyword) {

$("#t_c").val(keyword.id);

//console.log("onSetSelectValue: ", keyword)

}).on("onUnsetSelectValue",

function(e) {

console.log("onUnsetSelectValue")

});

layui.use('laydate', function() {

var laydate = layui.laydate;

//日历定义

var start = {

min: laydate.now(),

format: "YYYY-MM-DD",

max: "2050-01-01",

istime: true,

istoday: true,

};

document.getElementById('date').onclick = function() {

start.elem = this;

laydate(start);

}

});

var id;

$("#search").click(function() {

clearInterval(id);

var f_s = $("#f_c").val();

if (f_s == '') {

layer.msg('请选择出发城市', {'time': 1000});

return false;

}

var t_s = $("#t_c").val();

if (t_s == '') {

layer.msg('请选择到达城市', {'time': 1000});

return false;

}

var date = $("#date").val();

if (date == '') {

layer.msg('请选择出发日期', {'time': 1000});

return false;

}

//指定的车次

var train = $.trim($("#train").val());

//车次类型

var train_type = [];

$('input[name="train_type"]:checked').each(function() {

train_type.push($(this).val());

});

//刷新频率

var flash = $("#flash").val();

if (flash == '') {

showData(f_s, t_s, date, train, train_type);

} else {

if (parseInt(flash) < 2) {

layer.msg('刷新频率必须大于2', {'time': 1000});

return false;

}

showData(f_s, t_s, date, train, train_type);

layer.msg('数据将' + parseInt(flash) + '秒刷新一次', {'time': 2000});

id = setInterval(function() {

showData(f_s, t_s, date, train, train_type);

}, parseInt(flash) * 1000);

}

});

});

//展示数据

function showData(f_s, t_s, date, train, train_type) {

var loading = layer.load(1, {shade: false}); //0代表加载的风格,支持0-2

$.getJSON('./ajax.php', {'f_s': f_s, 't_s': t_s, 'date': date}, function(res) {

layer.close(loading);

//console.log(res.data); //完整JSON数据,包括flag(代号标记状态)、map(站点信息)、result(余票数据)等

//console.log(res.data.result); //余票JSON数据

//console.log(res.data.map); //车站站点JSON数据

if (res.data.flag) {

var _html = '';

$.each(res.data.result, function(k, v) {

if (train != '' && train != v.station_train_no) {

return true;

}

if (train_type.length > 0 && $.inArray(v.station_train_no.substr(0, 1), train_type) < 0) {

return true;

}

//转化出发站点代号成出发站点中文、目的站点代号成目的站点中文

v.start_station = stationChange(res.data.map, v.start_station);

v.end_station = stationChange(res.data.map, v.end_station);

_html += "

" + v.station_train_no + "";

_html += "

始:" + v.start_station;

_html += "终:" + v.end_station + "

";

_html += "

" + v.start_time + "
" + v.end_time + "";

_html += "

" + v.spend_time + "
当日到达";

_html += "

" + v.swztz_num + "";

_html += "

" + v.yd_num + "";

_html += "

" + v.ed_num + "";

_html += "

" + v.gr_num + "";

_html += "

" + v.rw_num + "";

_html += "

" + v.dw_num + "";

_html += "

" + v.yw_num + "";

_html += "

" + v.rz_num + "";

_html += "

" + v.yz_num + "";

_html += "

" + v.wz_num + "";

_html += "

" + v.qt_num + "";

_html += "

" + v.bz_num + "";

});

$("#s_res").html(_html);

$("#res").html("( " + dateChange($('#date').val())/*得到的JSON数据中的日期是字符串,需要进行格式转化*/ + " ) 共计 " + $("#s_res tr").length + " 个车次");

$('.bz').click(function(){alert('未实现真正预订功能!');});

} else {

$("#s_res").empty();

$("#res").empty();

layer.msg('没有符合的数据', {'time': 1000});

}

});

}

//转化站点代号成站点中文

function stationChange(stationMap, station){

//console.log(stationMap);

//遍历检索出来的站点JSON数据

$.each(stationMap, function(k, v) {

//console.log(k+v);

if (k == null || k == '') {

layer.msg('获取站点信息失败!', {'time': 1000});

return false;

}

if (station == k) {

//console.log(v);

station = v;

}

});

return station;

}

//转化时间字符串成日期对象,再通过日期对象获取年、月、日、星期几等

function dateChange(dateStr){

// Date.parse()方法解析一个表示某个日期的字符串(如2020/01/01等),并返回从1970-1-1 00:00:00 UTC 到该日期对象(该日期对象的UTC时间)的毫秒数

// 2020-01-01这种为不合法日期字符串,需要替换"-"变成"/"

var date = new Date(Date.parse(dateStr.replace(/-/g,"/")));

var dateStr = date.getFullYear() + '年' + (date.getMonth()+1) + '月' + date.getDate() + '日';

return dateStr;

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值