当前位置:Gxlcms > PHP教程 > 12306火车票余票查询、价格查询最新api(PHP)

12306火车票余票查询、价格查询最新api(PHP)

时间:2021-07-01 10:21:17 帮助过:58人阅读

其实我这个代码很早以前就出来了,只是一直没有放在开源中国上一直放在我的博客上了,当然程序没有优化,大家可以优化一下,那天看见出了个python的我就笑了!不过没关系了,发出来给大家看看~~以前也发了一个 去年的12306但是那个太简陋了,没有价格查询什么的,这个功能比较完善
  1. /*
  2. *@Description 火车票余票查询和价格查询
  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. *这个由于时间紧写的,所以很多地方还需要优化,希望大家改善一下,可以分部和文件缓存的形式提高执行速度--缺点执行速度慢--有待优化
  80. */
  81. $data = gettrain("北京","天津","01-03");
  82. /*
  83. * ["gr_num"]=>高级软卧
  84. * ["qt_num"]=>其他
  85. * ["rw_num"]=> 软卧
  86. * ["rz_num"]=>软座
  87. * ["tz_num"]=>特等座
  88. * ["wz_num"]=>无座
  89. * ["yw_num"]=>硬卧
  90. * ["yz_num"]=>硬座
  91. * ["ze_num"]=>二等座
  92. * ["zy_num"]=> 一等座
  93. * ["swz_num"]=> 商务座
  94. */
  95. $str="";
  96. foreach($data as $key =>$value){
  97. $str.="火车列次:{$data[$key]['station_train_code']}";
  98. $str.="始发站:{$data[$key]['start_station_name']},终点站{$data[$key]['end_station_name']}
    ";
  99. $str.="出发时间:{$data[$key]['start_time']},到站时间:{$data[$key]['arrive_time']}
    ";
  100. $str.="";
  101. $str.= strpos($data[$key]["gr_num"],"()") ? "" :"高级软卧余票和价格:".$data[$key]["gr_num"]."
    ";
  102. $str.= strpos($data[$key]["qt_num"],"()") ? "" :"其他余票和价格:".$data[$key]["qt_num"]."
    ";
  103. $str.= strpos($data[$key]["rw_num"],"()") ? "" :"软卧余票和价格:".$data[$key]["rw_num"]."
    ";
  104. $str.= strpos($data[$key]["rz_num"],"()") ? "" :"软座余票和价格:".$data[$key]["rz_num"]."
    ";
  105. $str.= strpos($data[$key]["tz_num"],"()") ? "" :"特等座余票和价格:".$data[$key]["tz_num"]."
    ";
  106. $str.= strpos($data[$key]["wz_num"],"()") ? "" :"无座余票和价格:".$data[$key]["wz_num"]."
    ";
  107. $str.= strpos($data[$key]["yw_num"],"()") ? "" :"硬卧余票和价格:".$data[$key]["yw_num"]."
    ";
  108. $str.= strpos($data[$key]["yz_num"],"()") ? "" :"硬座余票和价格:".$data[$key]["yz_num"]."
    ";
  109. $str.= strpos($data[$key]["ze_num"],"()") ? "" :"二等座余票和价格:".$data[$key]["ze_num"]."
    ";
  110. $str.= strpos($data[$key]["zy_num"],"()") ? "" :"一等座余票和价格:".$data[$key]["zy_num"]."
    ";
  111. $str.= strpos($data[$key]["swz_num"],"()") ? "" :"商务座余票和价格:".$data[$key]["swz_num"]."
    ";
  112. $str.= "======================下一列火车上===============================
    ";
  113. }
  114. echo $str;

人气教程排行