全国火车票查询
万维易源 官方文档
数据来源于12306。 包括城市列表\列车时刻\票价的查询
基本说明:
接口地址:http://route.showapi.com/909-1
返回格式:json
请求方式:get/post
请求示例:0
请求参数说明:
名称 类型 必填 说明
from String 必填 出发城市
to String 必填 目的地
trainDate String 必填 出发的日期,格式为:yyyy-MM-dd
返回参数说明:
名称 类型 说明
flag String 接口操作是否成功
ret_code String 接口调用是否成功 0为成功,其他值为失败
from String 同入参
to String 同入参
trainDate String 同入参
msg String 接口操作的提示信息
trai trainDetail 各列车座位和票价信息
eginCity String 始发站
endCity String 终点站
fromCity String 出发站
toCity String 到达站
fromTime String 出发时间
toTime String 到站时间
usedTime String 路途上需要的时间,单位是分钟
um String 车次
ticketInfo String 座位相关的信息
hardseat eatInfo 硬座
rice String 票价
ticketName String 座位的类型(名称),无座\硬座\硬卧\软卧...
ticketNum String 剩余票的数量
hardsleepermid eatInfo 硬卧
rice String 票价
ticketName String 座位的类型(名称),无座\硬座\硬卧\软卧...
ticketNum String 剩余票的数量
oseat eatInfo 无座
rice String 票价
ticketName String 座位的类型(名称),无座\硬座\硬卧\软卧...
ticketNum String 剩余票的数量
oftsleeperdow eatInfo 软卧
rice String 票价
ticketName String 座位的类型(名称),无座\硬座\硬卧\软卧...
ticketNum String 剩余票的数量
usinessseat String 商务座
firstseat String 一等座
econdseat String 二等座
JSON返回示例:
{
	"showapi_res_code": 0,
	"showapi_res_error": "",
	"showapi_res_body": {
		"flag": true,
		"from": "北京",
		"msg": "查询成功!",
		"ret_code": "0",
		"to": "武汉",
		"trainDate": "2016-03-18",
		"trains": [{
				"num": "K599",
				"fromTime": "05:15",
				"toTime": "22:10",
				"usedTime": 1015,
				"fromCity": "北京西",
				"toCity": "武昌",
				"beginCity": "包头",
				"endCity": "广州",
				"ticketInfo": {
					"hardseat": {
						"price": 152.5,
						"ticketName": "硬座",
						"ticketNum": "0"
					},
					"hardsleepermid": {
						"price": 279.5,
						"ticketName": "硬卧",
						"ticketNum": "5"
					},
					"noseat": {
						"price": 152.5,
						"ticketName": "无座",
						"ticketNum": "205"
					},
					"softsleeperdown": {
						"price": 427.5,
						"ticketName": "软卧",
						"ticketNum": "9"
					}
				}
			}
		]
	}
}
服务级错误码参照
错误码 说明
-1 系统调用错误
-2 可调用次数或金额为0
-3 读取超时
-4 服务端返回数据解析错误
-5 后端服务器DNS解析错误
-6 服务不存在或未上线
-1000 系统维护
-1002 showapi_appid字段必传
-1003 showapi_sign字段必传
-1004 签名sign验证有误
-1005 showapi_timestamp无效
-1006 app无权限调用接口
-1007 没有订购套餐
-1008 服务商关闭对您的调用权限
-1009 调用频率受限
-1010 找不到您的应用
-1011 子授权app_child_id无效
-1012 子授权已过期或失效
-1013 子授权ip受限
完整教学代码示例
public static void main(String path[]) throws Exception {
URL u = new URL("http://route.showapi.com/909-1?showapi_appid%3Dmyappid%26from%3D%E5%8C%97%E4%BA%AC%26to%3D%E6%AD%A6%E6%B1%89%26trainDate%3D2016-03-26%26showapi_sign%3Dmysecret");
InputStream in = u.openStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
byte buf[] = new byte[1024];
int read = 0;
while ((read = in .read(buf)) > 0) {
out.write(buf, 0, read);
}
} finally {
if ( in != null) {
in .close();
}
}
byte b[] = out.toByteArray();
System.out.println(new String(b, "utf-8"));
}
<?php
/**
 * Created by PhpStorm.
 * User: FZS
 * Time: 2019/3/15 17:50
 */
//----------------------------------
// 易源 火车票查询 调用类
//----------------------------------
class freeApi{
    private $apiId = false; //易源appid
    private $apiSecret = false; //易源secret
    private $apiUrl = 'http://route.showapi.com/909-1';
    public function __construct($apiid,$secretkey){
        $this->apiId = $apiid;
        $this->apiSecret = $secretkey;
    }
    /**
     * url拼接
     * @return string
     */
    private function handleParam($paramArr,$showapi_secret){
        $paraStr = "";
        $signStr = "";
        ksort($paramArr);
        foreach ($paramArr as $key => $val) {
            if ($key != '' && $val != '') {
                $signStr .= $key.$val;
                $paraStr .= $key.'='.urlencode($val).'&';
            }
        }
        $signStr .= $showapi_secret;//排好序的参数加上secret,进行md5
        $sign = strtolower(md5($signStr));
        $paraStr .= 'showapi_sign='.$sign;//将md5后的值作为参数,便于服务器的效验
        return '?'.$paraStr;
    }
    /**
     * 将JSON内容转为数据,并返回
     * @param string $content [内容]
     * @return array
     */
    public function returnArray($content){
        return json_decode($content,true);
    }
    /**
     * 获取 火车票查询 结果
     * @return array
     */
    public function getResult(){
        $paramArr = [
            'showapi_appid'=> $this->apiId,
            'from'=> "北京",
            'to'=> "武汉",
            'trainDate'=> "2016-03-26"
        ];
        return $this->returnArray($this->freeApiCurl($this->apiUrl.$this->handleParam($paramArr,$this->apiSecret)));
    }
    /**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    public function freeApiCurl($url,$params=false,$ispost=0){
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'free-api' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($params){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            return false;
        }
        curl_close( $ch );
        return $response;
    }
}
# python3.6.5
# 需要引入requests包 :运行终端->进入python/Scripts ->输入:pip install requests
from ShowapiRequest import ShowapiRequest

r = ShowapiRequest("http://route.showapi.com/909-1","my_appId","my_appSecret" )
r.addBodyPara("from", "北京")
r.addBodyPara("to", "武汉")
r.addBodyPara("trainDate", "2016-03-26")
# r.addFilePara("img", r"C:\Users\showa\Desktop\使用过的\4.png") #文件上传时设置
res = r.post()
print(res.text) # 返回信息
package main

import (
"showSdk/normalRequest"
"fmt"
)
res := normalRequest.ShowapiRequest("http://route.showapi.com/909-1",my_appId,"my_appSecret")
res.AddTextPara("from","北京")
res.AddTextPara("to","武汉")
res.AddTextPara("trainDate","2016-03-26")
//res.AddFilePara("img", "C:\\Users\\showa\\Desktop\\使用过的\\4.png")//文件上传时设置
fmt.Println(res.Post())
# python3.6.5
# 需要引入requests包 :运行终端->进入python/Scripts ->输入:pip install requests
from ShowapiRequest import ShowapiRequest

r = ShowapiRequest("http://route.showapi.com/909-1","my_appId","my_appSecret" )
r.addBodyPara("from", "北京")
r.addBodyPara("to", "武汉")
r.addBodyPara("trainDate", "2016-03-26")
# r.addFilePara("img", r"C:\Users\showa\Desktop\使用过的\4.png") #文件上传时设置
res = r.post()
print(res.text) # 返回信息