In fact, I came out of this code a long time ago, but has not been placed in open source China has been placed on my blog, of course, the program is not optimized, we can optimize it, the day saw a python I laughed! But it doesn't matter, send to show everyone ~ ~ before also sent a last year of 12306 but that too shabby, no price query what, this function is more perfect
- /*
- * @Description train ticket check and price inquiry
- * @author Widuu
- * @mktime 2014/1/03
- * @license http://www.widuu.com
- */
- Header ("content-type:text/html;charset= ' Utf-8 '");
- 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 Doget ($url) {
- if (!function_exists (' file_get_contents ')) {
- $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:https://kyfw.12306.cn/otn/lcxxcx/init "));
- $file _contents = file_get_contents ($url, False, Stream_context_create ($optionget));
- } else {
- $ch = Curl_init ();
- $timeout = 5;
- $header = Array (
- ' accept:*/* ',
- ' accept-charset:gbk,utf-8;q=0.7,*;q=0.3 ',
- ' Accept-encoding:gzip,deflate,sdch ',
- ' accept-language:zh-cn,zh;q=0.8,ja;q=0.6,en;q=0.4 ',
- ' Connection:keep-alive ',
- ' Host:kyfw.12306.cn ',
- ' Referer:https://kyfw.12306.cn/otn/lcxxcx/init ',
- );
- curl_setopt ($ch, Curlopt_url, $url);
- curl_setopt ($ch, Curlopt_httpheader, $header);
- curl_setopt ($ch, Curlopt_returntransfer, 1);
- curl_setopt ($ch, curlopt_ssl_verifyhost,1);
- curl_setopt ($ch, Curlopt_ssl_verifypeer, 0);
- curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
- $file _contents = curl_exec ($ch);
- Curl_close ($ch);
- }
- $file _contents = Json_decode ($file _contents,true);
- return $file _contents;
- }
- /*
- * This is due to time-sensitive, so a lot of places also need to optimize, I hope you improve, can be in the form of branch and file cache to improve execution speed-the slow implementation of the shortcomings-to be optimized
- */
- $data = Gettrain ("Beijing", "Tianjin", "01-03");
- /*
- * ["Gr_num"]=> advanced soft Sleeper
- * ["Qt_num"]=> Other
- * ["Rw_num"]=> soft Sleeper
- * ["rz_num"]=> soft seat
- * ["Tz_num"]=> seat
- * ["Wz_num"]=> no seat
- * ["Yw_num"]=> hard Sleeper
- * ["Yz_num"]=> hard seat
- * ["Ze_num"]=> second seat
- * ["Zy_num"]=>
- * ["Swz_num"]=> business seat
- */
- $str = "";
- foreach ($data as $key = = $value) {
- $str. = "Train time: {$data [$key] [' Station_train_code ']}";
- $str. = "Originating Station: {$data [$key] [' Start_station_name ']}, terminal {$data [$key] [' End_station_name ']}
";
- $str. = "Departure time: {$data [$key] [' Start_time ']}, Arrival time: {$data [$key] [' Arrive_time ']}
";
- $str. = "";
- $str. = Strpos ($data [$key] ["Gr_num"], "()")? "": "Premium soft sleeper surplus ticket and Price:". $data [$key] [gr_num]. "
";
- $str. = Strpos ($data [$key] ["Qt_num"], "()")? "": "other remaining tickets and prices:". $data [$key] [qt_num]. "
";
- $str. = Strpos ($data [$key] ["Rw_num"], "()")? "": "soft sleeper surplus ticket and Price:". $data [$key] [rw_num]. "
";
- $str. = Strpos ($data [$key] ["Rz_num"], "()")? "": "soft seat surplus ticket and Price:". $data [$key] [rz_num]. "
";
- $str. = Strpos ($data [$key] ["Tz_num"], "()")? "": "Premium seats and Prices:". $data [$key] [tz_num]. "
";
- $str. = Strpos ($data [$key] ["Wz_num"], "()")? "": "No seat surplus ticket and Price:". $data [$key] [wz_num]. "
";
- $str. = Strpos ($data [$key] ["Yw_num"], "()")? "": "Hard Sleeper and Price:". $data [$key] ["Yw_num"]. "
";
- $str. = Strpos ($data [$key] ["Yz_num"], "()")? "": "Hard seat surplus ticket and Price:". $data [$key] [yz_num]. "
";
- $str. = Strpos ($data [$key] ["Ze_num"], "()")? "": "second-rate seat surplus ticket and Price:". $data [$key] [ze_num]. "
";
- $str. = Strpos ($data [$key] ["Zy_num"], "()")? "": "A-Class ticket and Price:". $data [$key] [zy_num]. "
";
- $str. = Strpos ($data [$key] ["Swz_num"], "()")? "": "Business seat surplus ticket and Price:". $data [$key] [swz_num]. "
";
- $str. = "====================== on the next train ===============================
";
- }
- Echo $str;
Copy Code |