火车余票查询的API,12306官方的API
我在做自己微信的时候需要的火车票查询的东西,注册的东西等等太多了,今天就简单的做了下测试看能获取到不,数据怎么样处理速度怎么样,因为是个人做所以就没有去其他网站上找接口,就自己做了下!下边把方法放出来!查看源代码打印帮助0102/**03 *火车票查询方法 简单尝试
·
我在做自己微信的时候需要的火车票查询的东西,注册的东西等等太多了,今天就简单的做了下测试看能获取到不,数据怎么样处理速度怎么样,因为是个人做所以就没有去其他网站上找接口,就自己做了下!下边把方法放出来!
01 | <?php |
02 | /** |
03 | *火车票查询方法 简单尝试 |
04 | * |
05 | * @copyright widuu |
06 | * @license http://www.widuu.com |
07 | * @lastmodify 2013-6-20 |
08 | */ |
09 |
10 | function json_array( $json ){ |
11 | if ( $json ){ |
12 | foreach (( array ) $json as $k => $v ){ |
13 | $data [ $k ] = ! is_string ( $v )? json_array( $v ): $v ; |
14 | } |
15 | return $data ; |
16 | } |
17 | } |
18 | function doget ( $start , $end , $time ) // get获取数据使用 |
19 | { |
20 | if ( empty ( $time )){ |
21 | $time = date ( 'Y-m-d' ,time()); |
22 | } else { |
23 | if ( substr ( $time ,0,1)!=0){ |
24 | $time = date ( 'Y-0' ,time()). $time ; |
25 | echo substr ( $time ,0,1); |
26 | } else { |
27 | $time = date ( 'Y-' ,time()). $time ; |
28 | } |
29 | } |
30 | $name = include ( "../name.php" ); |
31 | $star = $name [ $start ]; |
32 | $end = $name [ $end ]; |
33 | $url = "http://dynamic.12306.cn/otsquery/query/queryRemanentTicketAction.do?method=queryLeftTicket&orderRequest.train_date={$time}&orderRequest.from_station_telecode={$star}&orderRequest.to_station_telecode={$end}&orderRequest.train_no=&trainPassType=QB&trainClass=QB%23D%23Z%23T%23K%23QT%23&includeStudent=00&seatTypeAndNum=&orderRequest.start_time_str=00%3A00--24%3A00" ; |
34 | $optionget = array ( 'http' => array ( 'method' => "GET" , 'header' => "User-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.0.04506; CIBA)\r\nAccept:*/*\r\nReferer:http://dynamic.12306.cn/otsquery/query/queryRemanentTicketAction.do?method=init" )); |
35 | $file = file_get_contents ( $url , false , stream_context_create( $optionget )); |
36 | return json_array(json_decode( $file )); |
37 | } |
38 | $result = doget( "北京" , "廊坊" , "6-23" ); |
39 | $result = strip_tags ( $result [ 'datas' ]); |
40 | $return_str = str_replace ( " " , "" , $result ); |
41 | $return_str = str_replace ( "\\n" , "" , $return_str ); |
42 | $a = explode ( "," , $return_str ); |
43 | $name = array (); |
44 | $c = array_chunk ( $a ,16); |
45 | array_pop ( $c ); |
46 | foreach ( $c as $k => $v ){ |
47 | $str = "余票:<br>商务座:" . $v [5]. ",特等座:" . $v [6]. ",一等座:" . $v [7]. ",二等座:" . $v [8]. ",高级软卧:" . $v [9]. ",软卧:" . $v [10]. ",硬卧:" . $v [11]. ",软座:" . $v [12]. ",硬座:" . $v [13]. ",无座:" . $v [14]. ",其他:" . $v [15]; |
48 | $str = preg_replace( "/,硬座\:--,/" , "" , $str ); |
49 | $str = preg_replace( "/商务座\:--,/" , "" , $str ); |
50 | $str = preg_replace( "/,特等座\:--,/" , "" , $str ); |
51 | $str = preg_replace( "/,一等座\:--,/" , "" , $str ); |
52 | $str = preg_replace( "/,二等座\:--,/" , "" , $str ); |
53 | $str = preg_replace( "/,高级软卧\:--,/" , "" , $str ); |
54 | $str = preg_replace( "/,软卧\:--,/" , "" , $str ); |
55 | $str = preg_replace( "/,硬卧\:--,/" , "" , $str ); |
56 | $str = preg_replace( "/,软座\:--,/" , "" , $str ); |
57 | $str = preg_replace( "/,硬座\:--,/" , "" , $str ); |
58 | $str = preg_replace( "/,其他\:--/" , "" , $str ); |
59 | echo "车次:{$v[1]},发站:{$v[2]},到站:{$v[3]},历时:{$v[4]},<br>{$str}<br>" ; |
60 | } |
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献2条内容
所有评论(0)