12306 latest api for querying the remaining ticket and price of train tickets (PHP)

Source: Internet
Author: User
12306 latest api for querying the remaining ticket and price of train tickets (PHP)
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.

  1. /*
  2. * @ Description query of the remaining ticket and price of a train ticket
  3. * @ Author widuu
  4. * @ Mktime 2014/1/03
  5. * @ License http://www.widuu.com
  6. */
  7. Header ("content-type: text/html; charset = 'utf-8 '");
  8. Function gettrain ($ start, $ end, $ data ){
  9. $ Station = include ('./name. php ');
  10. $ Startstaion = $ station [$ start];
  11. $ Endstation = $ station [$ end];
  12. If (empty ($ data )){
  13. $ Date = date ("Y-m-d", time ());
  14. } Else {
  15. $ Num = explode ("-", $ data );
  16. If (count ($ num) = 2 ){
  17. $ Date = date ("Y", time (). "-". $ data;
  18. } Else if (count ($ num) = 3 ){
  19. $ Date = $ data;
  20. } Else {
  21. Exit ("time error ");
  22. }
  23. }
  24. If (empty ($ startstaion) | empty ($ endstation )){
  25. Exit ("stdin error ");
  26. }
  27. $ Url = "http://kyfw.12306.cn/otn/lcxxcx/query? Purpose_codes = ADULT & queryDate = $ date & from_station = $ startstaion & to_station = $ endstation ";
  28. $ Data = doget ($ url );
  29. If (! $ Data ['status']) {
  30. Exit ('check error ');
  31. } Else {
  32. $ Data = $ data ['data'] ['datas'];
  33. Foreach ($ data as $ key => $ value ){
  34. $ 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 ");
  35. $ Data [$ key] ["gr_num"] = $ data [$ key] ["gr_num"]. "(". $ price ["data"] ["A6"]. ")";
  36. $ Data [$ key] ["qt_num"] = $ data [$ key] ["qt_num"]. "(". $ price ["data"] ["OT"] [0]. ")";
  37. $ Data [$ key] ["rw_num"] = $ data [$ key] ["rw_num"]. "(". $ price ["data"] ["A4"]. ")";
  38. $ Data [$ key] ["rz_num"] = $ data [$ key] ["rz_num"]. "(". $ price ["data"] ["A2"]. ")";
  39. $ Data [$ key] ["tz_num"] = $ data [$ key] ["tz_num"]. "(". $ price ["data"] ["P"]. ")";
  40. $ Data [$ key] ["wz_num"] = $ data [$ key] ["wz_num"]. "(". $ price ["data"] ["WZ"]. ")";
  41. $ Data [$ key] ["yw_num"] = $ data [$ key] ["yw_num"]. "(". $ price ["data"] ["A3"]. ")";
  42. $ Data [$ key] ["yz_num"] = $ data [$ key] ["yz_num"]. "(". $ price ["data"] ["A1"]. ")";
  43. $ Data [$ key] ["ze_num"] = $ data [$ key] ["ze_num"]. "(". $ price ["data"] ["O"]. ")";
  44. $ Data [$ key] ["zy_num"] = $ data [$ key] ["zy_num"]. "(". $ price ["data"] ["M"]. ")";
  45. $ Data [$ key] ["swz_num"] = $ data [$ key] ["swz_num"]. "(". $ price ["data"] ["A9"]. ")";
  46. }
  47. }
  48. Return $ data;
  49. }
  50. Function doget ($ url ){
  51. If (! Function_exists ('File _ get_contents ')){
  52. $ 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 "));
  53. $ File_contents = file_get_contents ($ url, false, stream_context_create ($ optionget ));
  54. } Else {
  55. $ Ch = curl_init ();
  56. $ Timeout = 5;
  57. $ Header = array (
  58. 'Accept :*/*',
  59. 'Accept-Charset: GBK, UTF-8; q = 0.7, *; q = 100 ',
  60. 'Accept-Encoding: gzip, deflate, sdch ',
  61. 'Accept-Language: zh-CN, zh; q = 0.8, ja; q = 0.6, en; q = 100 ',
  62. 'Connection: keep-alive ',
  63. 'Host: kyf201712306.cn ',
  64. 'Referer: https://kyf201712306.cn/otn/lcxxcx/init ',
  65. );
  66. Curl_setopt ($ ch, CURLOPT_URL, $ url );
  67. Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header );
  68. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
  69. Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, 1 );
  70. Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, 0 );
  71. Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
  72. $ File_contents = curl_exec ($ ch );
  73. Curl_close ($ ch );
  74. }
  75. $ File_contents = json_decode ($ file_contents, true );
  76. Return $ file_contents;
  77. }
  78. /*
  79. * 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
  80. */
  81. $ Data = gettrain ("Beijing", "Tianjin", "01-03 ");
  82. /*
  83. * ["Gr_num"] => advanced soft sleeper
  84. * ["Qt_num"] => Other
  85. * ["Rw_num"] => soft sleeper
  86. * ["Rz_num"] => soft seat
  87. * ["Tz_num"] => special seat
  88. * ["Wz_num"] => no seat
  89. * ["Yw_num"] => hard sleeper
  90. * ["Yz_num"] => hard seat
  91. * ["Ze_num"] => second-class seat
  92. * ["Zy_num"] => first-class seat
  93. * ["Swz_num"] => Business seat
  94. */
  95. $ Str = "";
  96. Foreach ($ data as $ key => $ value ){
  97. $ Str. = "train record: {$ data [$ key] ['Station _ train_code ']}";
  98. $ Str. = "origin site: {$ data [$ key] ['start _ station_name ']}, terminal {$ data [$ key] ['end _ station_name']}
    ";
  99. $ Str. = "departure time: {$ data [$ key] ['start _ time']}, arrival time: {$ data [$ key] ['Rive _ time']}
    ";
  100. $ Str. = "";
  101. $ Str. = strpos ($ data [$ key] ["gr_num"], "()")? "": "Additional ticket and price:". $ data [$ key] ["gr_num"]."
    ";
  102. $ Str. = strpos ($ data [$ key] ["qt_num"], "()")? "": "Other remaining tickets and prices:". $ data [$ key] ["qt_num"]."
    ";
  103. $ Str. = strpos ($ data [$ key] ["rw_num"], "()")? "": "Remaining ticket and price:". $ data [$ key] ["rw_num"]."
    ";
  104. $ Str. = strpos ($ data [$ key] ["rz_num"], "()")? "": "Soft seat remaining tickets and prices:". $ data [$ key] ["rz_num"]."
    ";
  105. $ Str. = strpos ($ data [$ key] ["tz_num"], "()")? "": "Special round-seat remaining tickets and prices:". $ data [$ key] ["tz_num"]."
    ";
  106. $ Str. = strpos ($ data [$ key] ["wz_num"], "()")? "": "No-seat ticket and price:". $ data [$ key] ["wz_num"]."
    ";
  107. $ Str. = strpos ($ data [$ key] ["yw_num"], "()")? "": "Ticket remaining and price:". $ data [$ key] ["yw_num"]."
    ";
  108. $ Str. = strpos ($ data [$ key] ["yz_num"], "()")? "": "Remaining ticket and price:". $ data [$ key] ["yz_num"]."
    ";
  109. $ Str. = strpos ($ data [$ key] ["ze_num"], "()")? "": "Second-class remaining seats and prices:". $ data [$ key] ["ze_num"]."
    ";
  110. $ Str. = strpos ($ data [$ key] ["zy_num"], "()")? "": "First-class seat remaining tickets and prices:". $ data [$ key] ["zy_num"]."
    ";
  111. $ Str. = strpos ($ data [$ key] ["swz_num"], "()")? "": "Business seat remaining votes and prices:". $ data [$ key] ["swz_num"]."
    ";
  112. $ Str. = "================================== next train ================================
    ";
  113. }
  114. Echo $ str;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.