最近在捣鼓微信公众平台开发者模式,尝试给微信公众平台接入更多功能,而这个火车余票查询也是功能的一部分,断断续续写了两天才完成。此代码支持按日期查询余票,不过日期格式化代码略显简陋(其实各个地方都很简陋哈哈哈),如果您有更好的方法请告诉我哦~~
在这里分享我的源码其实也就是省的我以后忘了这两天出来结构的存在啦,以后看到这篇文章我可能还会想:“诶?我以前居然写过这些代码吗?”之类之类的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
<?php /* Copyright (c) 2014 Hintay <[email protected]> Original Author: Hintay <[email protected]> */ function huoche($data,$time=''){ $data = array_merge($data); if(count($data)!=2){return '您的输入有误哦,查询火车的格式为"火车+出发地至目的地+日期(可选)",日期格式为01-01或2014-01-01';}; $startStation = $data[0]; $arriveStation = $data[1]; $startDate = empty($time)?date('Y-m-d',time()):$time; //正则获取站名相应代码 。12306使用了https连接,无法使用file_get_contents获取 $station_name = curlGet('https://kyfw.12306.cn/otn/resources/js/framework/station_name.js'); $patternstart = '/\|'.$startStation.'\|([^|]+)/'; $patternarrive = '/\|'.$arriveStation.'\|([^|]+)/'; preg_match($patternstart, $station_name, $startStation); preg_match($patternarrive, $station_name, $arriveStation); $url = "https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=0X00&queryDate=$startDate&from_station={$startStation[1]}&to_station={$arriveStation[1]}"; $json = curlGet($url); $tickets = $json; $json = json_decode($json); if($json != null){ //有返回信息则直接返回 if(isset($json -> data -> message)){ return $json -> data -> message; } //相应关系处理 $ticketsname = array('gr_num' => '高级软卧','qt_num' => '其他','rw_num' => '软卧','rz_num' => '软座','tz_num' => '特等座','wz_num' => '无座','yw_num' => '硬卧','yz_num' => '硬座','ze_num' => '二等座','zy_num' => '一等座','swz_num' => '商务座'); $pricename = array('gr_num' => 'A6','qt_num' => 'OT','rw_num' => 'A4','rz_num' => 'A2','tz_num' => 'P','wz_num' => 'WZ','yw_num' => 'A3','yz_num' => 'A1','ze_num' => 'O','zy_num' => 'M','swz_num' => 'A9'); $main = $json -> data -> datas; //正则提取价格 $tickets = json_decode(preg_replace('/{("train_no".*?),"gg_num"/' , '{"gg_num"' , $tickets)); $tickets = $tickets -> data -> datas; $conunt = count($main); $fin = '★ 余票查询'; $fin .= "\n$time"; for($i = 0;$i < $conunt;$i++){ //判断此列车是否全部无票 $ticket = false; foreach($tickets[$i] as $key => $value){ if($value != '--' && $value != '无'){ $ticket = true; break; } } if($ticket == true){ $fin .= "\n【编号】" . $main[$i] -> station_train_code . "\n【出发站】". $main[$i] -> from_station_name . "\n【出发时间】". $main[$i] -> start_time . "\n【到达站】". $main[$i] -> to_station_name . "\n【到达时间】" . $main[$i] -> arrive_time . "\n【历时】" . $main[$i] -> lishi . "\n"; $price = json_decode(curlGet("https://kyfw.12306.cn/otn/leftTicket/queryTicketPrice?train_no=".$main[$i] -> train_no."&from_station_no=".$main[$i] -> from_station_no."&to_station_no=".$main[$i] -> to_station_no."&seat_types=".$main[$i] -> seat_types."&train_date=$startDate")); foreach($tickets[$i] as $key => $value){ if($value != '--'){ $fin .= "{$ticketsname[$key]}:$value "; //只有当有票时才显示价格 if($value != '无'){ $fin .= "{$price -> data -> $pricename[$key]} "; } } } $fin .= "\n----------------------"; } } }else{ $fin = '很抱歉,无法查询到余票,请稍后重试。'; } return $fin; } //curl类,以支持https function curlGet($url,$method='get',$data=''){ $ch = curl_init(); $header[] = "Accept-Charset: utf-8"; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $temp = curl_exec($ch); return $temp; } /* * 方式:城市+至+城市+日期 */ header("Content-type: text/html; charset=utf-8"); $huoche = '温州至杭州东12-3'; $huoche = str_replace('火车','',$huoche); //日期匹配(月-日) if(preg_match("/\d{2}-\d{2}/",$huoche,$date) || preg_match("/\d{1}-\d{2}/",$huoche,$date) || preg_match("/\d{2}-\d{1}/",$huoche,$date) ){ //日期匹配(年-月-日) if(preg_match("/\d{4}-\d{2}-\d{2}/",$huoche,$date1) || preg_match("/\d{4}-\d{1}-\d{2}/",$huoche,$date1) || preg_match("/\d{4}-\d{2}-\d{1}/",$huoche,$date1)){ $date = $date1[0]; $huoche = str_replace($date,'',$huoche); }else{ $huoche = str_replace($date[0],'',$huoche); $date = date('Y-',time()).$date[0]; } //日期格式化 $date = date("Y-m-d",strtotime($date)); }elseif(preg_match("/\d/",$huoche) && !preg_match("/-/",$huoche)){ return array('您输入的日期有误哦,正确的日期格式为01-01或2014-01-01','text'); }else{ $date = ''; } print_r(huoche(explode('至',$huoche),$date)); ?> |
这个代码只是个示例,具体使用还是需要修改的,比较难以理解的地方都写了简单的注释。
一个小秘密:我其实没有学过PHP,代码写得可能会比较复杂吧,不要嘲笑我!
抢沙发