12306 train ticket Check, Price Inquiry latest API (PHP)

Source: Internet
Author: User
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
  1. /*
  2. * @Description train ticket check and price inquiry
  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; 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 "));
  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=0.3 ',
  60. ' Accept-encoding:gzip,deflate,sdch ',
  61. ' accept-language:zh-cn,zh;q=0.8,ja;q=0.6,en;q=0.4 ',
  62. ' Connection:keep-alive ',
  63. ' Host:kyfw.12306.cn ',
  64. ' Referer:https://kyfw.12306.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. * 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
  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"]=> seat
  88. * ["Wz_num"]=> no seat
  89. * ["Yw_num"]=> hard Sleeper
  90. * ["Yz_num"]=> hard seat
  91. * ["Ze_num"]=> second seat
  92. * ["Zy_num"]=>
  93. * ["Swz_num"]=> business seat
  94. */
  95. $str = "";
  96. foreach ($data as $key = = $value) {
  97. $str. = "Train time: {$data [$key] [' Station_train_code ']}";
  98. $str. = "Originating Station: {$data [$key] [' Start_station_name ']}, terminal {$data [$key] [' End_station_name ']}
    ";
  99. $str. = "Departure time: {$data [$key] [' Start_time ']}, Arrival time: {$data [$key] [' Arrive_time ']}
    ";
  100. $str. = "";
  101. $str. = Strpos ($data [$key] ["Gr_num"], "()")? "": "Premium soft sleeper surplus 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"], "()")? "": "soft sleeper surplus ticket and Price:". $data [$key] [rw_num]. "
    ";
  104. $str. = Strpos ($data [$key] ["Rz_num"], "()")? "": "soft seat surplus ticket and Price:". $data [$key] [rz_num]. "
    ";
  105. $str. = Strpos ($data [$key] ["Tz_num"], "()")? "": "Premium seats and Prices:". $data [$key] [tz_num]. "
    ";
  106. $str. = Strpos ($data [$key] ["Wz_num"], "()")? "": "No seat surplus ticket and Price:". $data [$key] [wz_num]. "
    ";
  107. $str. = Strpos ($data [$key] ["Yw_num"], "()")? "": "Hard Sleeper and Price:". $data [$key] ["Yw_num"]. "
    ";
  108. $str. = Strpos ($data [$key] ["Yz_num"], "()")? "": "Hard seat surplus ticket and Price:". $data [$key] [yz_num]. "
    ";
  109. $str. = Strpos ($data [$key] ["Ze_num"], "()")? "": "second-rate seat surplus ticket and Price:". $data [$key] [ze_num]. "
    ";
  110. $str. = Strpos ($data [$key] ["Zy_num"], "()")? "": "A-Class ticket and Price:". $data [$key] [zy_num]. "
    ";
  111. $str. = Strpos ($data [$key] ["Swz_num"], "()")? "": "Business seat surplus ticket and Price:". $data [$key] [swz_num]. "
    ";
  112. $str. = "====================== on the next train ===============================
    ";
  113. }
  114. Echo $str;
Copy Code
  • Related Article

    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.