In fact, this code has come out a long time ago, but it has never been put on open source China and has been put on my blog. of course, the program is not optimized. you can optimize it, I laughed when I saw a python one that day! But it doesn't matter. send it to you ~~ I used to send a 12306 last year, but it was too simple to query the price. This function is quite complete.
- /*
- * @ Description query of the remaining ticket and price of a train ticket
- * @ 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; server load balancer ;.. net clr 2.0.50727; Media Center PC 5.0 ;. net clr 3.5.21022 ;. net clr 3.0.04506; CBA) \ 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 = 100 ',
- 'Accept-Encoding: gzip, deflate, sdch ',
- 'Accept-Language: zh-CN, zh; q = 0.8, ja; q = 0.6, en; q = 100 ',
- 'Connection: keep-alive ',
- 'Host: kyf201712306.cn ',
- 'Referer: https://kyf201712306.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;
- }
- /*
- * Due to the time constraints, optimization is required in many places. we hope you can improve the execution speed in the form of segments and file caches-The disadvantage is that the execution speed is slow-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"] => special seat
- * ["Wz_num"] => no seat
- * ["Yw_num"] => hard sleeper
- * ["Yz_num"] => hard seat
- * ["Ze_num"] => second-class seat
- * ["Zy_num"] => first-class seat
- * ["Swz_num"] => Business seat
- */
- $ Str = "";
- Foreach ($ data as $ key => $ value ){
- $ Str. = "train record: {$ data [$ key] ['Station _ train_code ']}";
- $ Str. = "origin site: {$ data [$ key] ['start _ station_name ']}, terminal {$ data [$ key] ['end _ station_name']}
";
- $ Str. = "departure time: {$ data [$ key] ['start _ time']}, arrival time: {$ data [$ key] ['Rive _ time']}
";
- $ Str. = "";
- $ Str. = strpos ($ data [$ key] ["gr_num"], "()")? "": "Additional 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"], "()")? "": "Remaining ticket and price:". $ data [$ key] ["rw_num"]."
";
- $ Str. = strpos ($ data [$ key] ["rz_num"], "()")? "": "Soft seat remaining tickets and prices:". $ data [$ key] ["rz_num"]."
";
- $ Str. = strpos ($ data [$ key] ["tz_num"], "()")? "": "Special round-seat remaining tickets and prices:". $ data [$ key] ["tz_num"]."
";
- $ Str. = strpos ($ data [$ key] ["wz_num"], "()")? "": "No-seat ticket and price:". $ data [$ key] ["wz_num"]."
";
- $ Str. = strpos ($ data [$ key] ["yw_num"], "()")? "": "Ticket remaining and price:". $ data [$ key] ["yw_num"]."
";
- $ Str. = strpos ($ data [$ key] ["yz_num"], "()")? "": "Remaining ticket and price:". $ data [$ key] ["yz_num"]."
";
- $ Str. = strpos ($ data [$ key] ["ze_num"], "()")? "": "Second-class remaining seats and prices:". $ data [$ key] ["ze_num"]."
";
- $ Str. = strpos ($ data [$ key] ["zy_num"], "()")? "": "First-class seat remaining tickets and prices:". $ data [$ key] ["zy_num"]."
";
- $ Str. = strpos ($ data [$ key] ["swz_num"], "()")? "": "Business seat remaining votes and prices:". $ data [$ key] ["swz_num"]."
";
- $ Str. = "================================== next train ================================
";
- }
- Echo $ str;
|