public function index() { function json_array($json) { if ($json) { foreach ((array) $json as $k => $v) { $data[$k] = !is_string($v) ? json_array($v) : $v; } return $data; } } function doget($start, $end, $time) { if (empty($time)) { $time = date('Y-m-d', time()); } else { if (substr($time, 0, 1) != 0) { $time = date('Y-0', time()) . $time; echo substr($time, 0, 1); } else { $time = date('Y-', time()) . $time; } } $name = (include "../name.php"); $star = $name[$start]; $end = $name[$end]; $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"; $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")); $file = file_get_contents($url, false, stream_context_create($optionget)); return json_array(json_decode($file)); } $result = doget("±±¾©", "ÀÈ·»", "6-23"); $result = strip_tags($result['datas']); $return_str = str_replace(" ", "", $result); $return_str = str_replace("\\n", "", $return_str); $a = explode(",", $return_str); $name = array(); $c = array_chunk($a, 16); array_pop($c); foreach ($c as $k => $v) { $str = "ÓàƱ£º<br>ÉÌÎñ×ù:" . $v[5] . "£¬ÌصÈ×ù:" . $v[6] . "£¬Ò»µÈ×ù:" . $v[7] . "£¬¶þµÈ×ù:" . $v[8] . "£¬¸ß¼¶ÈíÎÔ:" . $v[9] . "£¬ÈíÎÔ:" . $v[10] . "£¬Ó²ÎÔ:" . $v[11] . "£¬Èí×ù:" . $v[12] . "£¬Ó²×ù:" . $v[13] . "£¬ÎÞ×ù:" . $v[14] . "£¬ÆäËû:" . $v[15]; $str = preg_replace("/£¬Ó²×ù\\:--£¬/", "", $str); $str = preg_replace("/ÉÌÎñ×ù\\:--£¬/", "", $str); $str = preg_replace("/£¬ÌصÈ×ù\\:--£¬/", "", $str); $str = preg_replace("/£¬Ò»µÈ×ù\\:--£¬/", "", $str); $str = preg_replace("/£¬¶þµÈ×ù\\:--£¬/", "", $str); $str = preg_replace("/£¬¸ß¼¶ÈíÎÔ\\:--£¬/", "", $str); $str = preg_replace("/£¬ÈíÎÔ\\:--£¬/", "", $str); $str = preg_replace("/£¬Ó²ÎÔ\\:--£¬/", "", $str); $str = preg_replace("/£¬Èí×ù\\:--£¬/", "", $str); $str = preg_replace("/£¬Ó²×ù\\:--£¬/", "", $str); $str = preg_replace("/£¬ÆäËû\\:--/", "", $str); echo "³µ´Î:{$v[1]},·¢Õ¾£º{$v[2]}£¬µ½Õ¾£º{$v[3]}£¬Àúʱ£º{$v[4]}£¬<br>{$str}<br>"; } }
function gettrain($start, $end, $data) { $station = (include './name.php'); $startstaion = $station[$start]; $endstation = $station[$end]; if (empty($data)) { $date = date("Y-m-d", time()); } else { $num = explode("-", $data); if (count($num) == 2) { $date = date("Y", time()) . "-" . $data; } else { if (count($num) == 3) { $date = $data; } else { exit("time error"); } } } if (empty($startstaion) || empty($endstation)) { exit("stdin error"); } $url = "http://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=ADULT&queryDate={$date}&from_station={$startstaion}&to_station={$endstation}"; $data = doget($url); if (!$data['status']) { exit('check error'); } else { $data = $data['data']['datas']; foreach ($data as $key => $value) { $price = doget("http://kyfw.12306.cn/otn/leftTicket/queryTicketPrice?train_no=" . $data[$key]["train_no"] . "&from_station_no=" . $data[$key]["from_station_no"] . "&to_station_no=" . $data[$key]["to_station_no"] . "&seat_types=" . $data[$key]["seat_types"] . "&train_date={$date}"); $data[$key]["gr_num"] = $data[$key]["gr_num"] . "(" . $price["data"]["A6"] . ")"; $data[$key]["qt_num"] = $data[$key]["qt_num"] . "(" . $price["data"]["OT"][0] . ")"; $data[$key]["rw_num"] = $data[$key]["rw_num"] . "(" . $price["data"]["A4"] . ")"; $data[$key]["rz_num"] = $data[$key]["rz_num"] . "(" . $price["data"]["A2"] . ")"; $data[$key]["tz_num"] = $data[$key]["tz_num"] . "(" . $price["data"]["P"] . ")"; $data[$key]["wz_num"] = $data[$key]["wz_num"] . "(" . $price["data"]["WZ"] . ")"; $data[$key]["yw_num"] = $data[$key]["yw_num"] . "(" . $price["data"]["A3"] . ")"; $data[$key]["yz_num"] = $data[$key]["yz_num"] . "(" . $price["data"]["A1"] . ")"; $data[$key]["ze_num"] = $data[$key]["ze_num"] . "(" . $price["data"]["O"] . ")"; $data[$key]["zy_num"] = $data[$key]["zy_num"] . "(" . $price["data"]["M"] . ")"; $data[$key]["swz_num"] = $data[$key]["swz_num"] . "(" . $price["data"]["A9"] . ")"; } } return $data; }
function httprequest($url, $data = array(), $abort = false) { if (!function_exists('curl_init')) { return empty($data) ? doget($url) : dopost($url, $data); } $timeout = $abort ? 1 : 2; $ch = curl_init(); if (is_array($data) && $data) { $formdata = http_build_query($data); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $formdata); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); $result = curl_exec($ch); return false === $result && false == $abort ? empty($data) ? doget($url) : dopost($url, $data) : $result; }
$time = date('Y-0', time()) . $time; echo substr($time, 0, 1); } else { $time = date('Y-', time()) . $time; } } $name = (include "name.php"); $star = $name[$start]; $end = $name[$end]; $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"; $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")); $file = file_get_contents($url, false, stream_context_create($optionget)); echo $file; return json_array(json_decode($file)); } $result = doget("北京", "廊坊", "6-23"); $result = strip_tags($result['datas']); $return_str = str_replace(" ", "", $result); $return_str = str_replace("\\n", "", $return_str); $a = explode(",", $return_str); $name = array(); $c = array_chunk($a, 16); array_pop($c); foreach ($c as $k => $v) { $str = "余票:<br>商务座:" . $v[5] . ",特等座:" . $v[6] . ",一等座:" . $v[7] . ",二等座:" . $v[8] . ",高级软卧:" . $v[9] . ",软卧:" . $v[10] . ",硬卧:" . $v[11] . ",软座:" . $v[12] . ",硬座:" . $v[13] . ",无座:" . $v[14] . ",其他:" . $v[15]; $str = preg_replace("/,硬座\\:--,/", "", $str); $str = preg_replace("/商务座\\:--,/", "", $str); $str = preg_replace("/,特等座\\:--,/", "", $str); $str = preg_replace("/,一等座\\:--,/", "", $str); $str = preg_replace("/,二等座\\:--,/", "", $str); $str = preg_replace("/,高级软卧\\:--,/", "", $str);