重量级接口!!12306火车票接口的使用
火车票接口的使用并不难 最重要的是一个请求的URL的拼接,URL地址如下:
var url = '/otn/leftTicket/query?leftTicketDTO.train_date='+req.body.date+'&leftTicketDTO.from_station='+req.body.start+'&leftTicketDTO.to_station='+req.body.end+'&purpose_codes=ADULT';
还需要给定一些参数,如下:
var options = {
hostname: 'kyfw.12306.cn',
port: 443,
path: url,
method: 'GET',
rejectUnauthorized: false
};
然后模拟发送一个请求,就会返回一大堆数据。JSON返回示例:
{
"error_code": "0",
"reason": "Success",
"result": [{
"train_no": "D2287",
/*车次*/
"start_station_name": "上海虹桥",
/*车次始发站*/
"end_station_name": "深圳北",
/*车次终点站*/
"from_station_name": "上海虹桥",
/*出发站*/
"to_station_name": "温州南",
/*到达站*/
"start_time": "06:25",
/*出发时间*/
"arrive_time": "10:53",
/*到达时间*/
"train_class_name": "动车",
/*车次类型*/
"day_difference": "0",
/*历时天数*/
"lishi": "04:28",
/*总历时时间*/
"gr_num": "--",
/*高级软卧:-- 说明无该席位*/
"qt_num": "--",
/*其他*/
"rw_num": "--",
/*软卧*/
"rz_num": "--",
/*软座*/
"tz_num": "--",
/*特等座*/
"wz_num": "无",
/*无座*/
"yw_num": "--",
/*硬卧*/
"yz_num": "--",
/*硬座*/
"ze_num": "无",
/*二等座*/
"zy_num": "无",
/*一等座*/
"swz_num": "--"
/*商务座*/
},
{
"train_no": "D3203",
"start_station_name": "上海虹桥",
"end_station_name": "厦门北",
"from_station_name": "上海虹桥",
"to_station_name": "温州南",
"start_time": "06:30",
"arrive_time": "11:09",
"train_class_name": "动车",
"day_difference": "0",
"lishi": "04:39",
"gr_num": "--",
"qt_num": "--",
"rw_num": "--",
"rz_num": "--",
"tz_num": "--",
"wz_num": "无",
"yw_num": "--",
"yz_num": "--",
"ze_num": "无",
"zy_num": "无",
"swz_num": "--",
"ticketprice": {
"OT": [],
"WZ": "¥226.0",
"M": "¥335.5",
"A9": "¥689.0",
"9": "6890",
"O": "¥226.0",
"train_no": "5l000G753722"
}
},
{
"train_no": "G7501",
"start_station_name": "上海虹桥",
"end_station_name": "苍南",
"from_station_name": "上海虹桥",
"to_station_name": "温州南",
"start_time": "07:00",
"arrive_time": "11:00",
"train_class_name": "",
"day_difference": "0",
"lishi": "04:00",
"gr_num": "--",
"qt_num": "--",
"rw_num": "--",
"rz_num": "--",
"tz_num": "--",
"wz_num": "165",
"yw_num": "--",
"yz_num": "--",
"ze_num": "无",
"zy_num": "无",
"swz_num": "15",
"ticketprice": {
"OT": [],
"WZ": "¥226.0",
"M": "¥335.5",
"A9": "¥689.0",
"9": "6890",
"O": "¥226.0",
"train_no": "5l000G753722"
}
}]
}
需要注意的是,站点之间需要用站点编码表示,而不能用中文!所以,需要去网上找编码对应的站点信息。