首頁 > 後端開發 > php教程 > 12306火車票餘票查詢、價格查詢最新api(PHP)

12306火車票餘票查詢、價格查詢最新api(PHP)

WBOY
發布: 2016-07-25 08:48:54
原創
1651 人瀏覽過
其實我這個程式碼很早以前就出來了,只是一直沒有放在開源中國上一直放在我的博客上了,當然程式沒有優化,大家可以優化一下,那天看見出了個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']){ check error');
  30. }else{
  31. $data = $data['data']['datas'];
  32. foreach ($data as $key => $value) {
  33. $ 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");
  34. $ $ data[$key]["gr_num"] = $data[$key]["gr_num"]."(".$price["data"]["A6"].")";
  35. $data[ $key]["qt_num"] = $data[$key]["qt_num"]."(".$price["data"]["OT"][0].")";
  36. $data [$key]["rw_num"] = $data[$key]["rw_num"]."(".$price["data"]["A4"].")";
  37. $data[$ key]["rz_num"] = $data[$key]["rz_num"]."(".$price["data"]["A2"].")";
  38. $data[$key] ["tz_num"] = $data[$key]["tz_num"]."(".$price["data"]["P"].")";
  39. $data[$key][" wz_num"] = $data[$key]["wz_num"]."(".$price["data"]["WZ"].")";
  40. $data[$key]["yw_num" ] = $data[$key]["yw_num"]."(".$price["data"]["A3"].")";
  41. $data[$key]["yz_num"] = $data[$key]["yz_num"]."(".$price["data"]["A1"].")";
  42. $data[$key]["ze_num"] = $data [$key]["ze_num"]."(".$price["data"]["O"].")";
  43. $data[$key]["zy_num"] = $data[$ key]["zy_num"]."(".$price["data"]["M"].")";
  44. $data[$key]["swz_num"]= $data[$key] ["swz_num"]."(".$price["data"]["A9"].")";
  45. }
  46. }
  47. return $data;
  48. }
  49. function doget($url){
  50. if(!function_exists('file_get_contents')) {
  51. $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) rnAccept:*/*rnReferer:https://kyfw.12306.cn/otn/lcxxcx/init"));
  52. $file_contents = file_get_contents($url, false , stream_context_create($optionget));
  53. }
  54. }else {
  55. $ch = curl_init();
  56. $timeout = 5;
  57. $header = array(
  58. '接受:*/*',
  59. '接受字元集:GBK,utf -8;q=0.7,*;q=0.3',
  60. '接受編碼:gzip,deflate,sdch',
  61. '接受語言:zh-CN,zh;q=0.8,ja;q =0.6 ,en;q=0.4',
  62. '連接:keep-alive',
  63. '主機:kyfw.12306.cn',
  64. '推薦人:https://kyfw.12306.cn/ otn /lcxxcx/init',
  65. );
  66. curl_setopt ($ch, CURLOPT_URL, $url);
  67. curl_setopt($ch, CURLOPT_HTTPHEADER,$header);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,1);
  68. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  69. curl_setopt($churl_setopt($CON3_PT_oCONdfile_$S7($)); $ch);
  70. curl_close($ch);
  71. }
  72. $file_contents = json_decode($file_contents,true);
  73. return $file_contents;
  74. }
  75. /*
  76. *
  77. *這個由於時間緊寫的,所以很多地方還需要優化,希望大家改善一下,可以分部和文件緩存的形式提高執行速度--缺點執行速度慢--有待優化
  78. */
  79. $ data = gettrain("北京","天津","01-03");
  80. /*
  81. * ["gr_num"]=>高級軟臥
  82. * ["qt_num"]=> ;其他
  83. * ["rw_num"]=> 軟
  84. * ["rz_num"]=>軟座
  85. * ["tz_num"]=>特等座
  86. * ["wz_num"]=>無座
  87. * ["yw_num "]=>硬臥
  88. * ["yz_num"]=>硬座
  89. * ["ze_num"]=>二等座
  90. * ["zy_num"]=>一等座
  91. * ["swz_num"]=> 商務座
  92. */
  93. $str="";
  94. foreach($data as $key =>$value){
  95. $ str.="火車列次:{$data[$ key]['station_train_code']}";
  96. $str.="始發站:{$data[$key]['start_station_name']},終點站{$data[$key]['end_station_name'] }
    ";
  97. $str.="出發時間:{$data[$key]['start_time']},到站時間:{$ data[$key]['arrive_time']}< ;br>";
  98. $str.="";
  99. $str.= strpos($data[$key]["gr_num"],"( )") ? "" :"高級軟臥餘票和價格:".$data[$key]["gr_num"]."
    ";
  100. $str.= strpos($data[$key] ["qt_num “],”()”) ? "" :"其他餘票與價格:".$data[$key]["qt_num"]."
    ";
  101. $str.= strpos ($data[$key]["rw_num" ],"()") ? "" :"軟臥餘票與價格:".$data[$key]["rw_num"]."
    ";
  102. $str.= strpos($data[$key]["rz_num" ],"()") ? "" :"軟座餘票與價格:".$data[$key]["rz_num"] ."
    ";
  103. $str.= strpos($data[$key]["tz_num “],”()”) ? "" :"特等座餘票和價格:".$data[ $key]["tz_num"]."
    ";
  104. $str.= strpos($data[$key]["wz_num “],”()”) ? "" :"無票和價格:".$data[$key]["wz_num"]."
    ";
  105. $str.= strpos($data[$key]["yw_num “],”()”) ? "" :"硬臥餘票與價格:".$data[$key]["yw_num"]."
    ";
  106. $str.= strpos($data[$key]["yz_num" ] ,"()") ? "" :"硬座餘票與價格:".$data[$key]["yz_num"]."
    ";
  107. $str.= strpos($data[$ key]["ze_num" ],"()") ? "" :"二等座餘票與價格:".$data[$key]["ze_num"]."
    ";
  108. $ str.= strpos($data[$key][" zy_num"],"()") ? "" :"一等座餘票和價格:".$data[$key]["zy_num"]."
    ";
  109. $str.= strpos($data[$key][" swz_num"],"()") ? "" :"商務座餘票與價格:".$data[$key ]["swz_num"]."
    ";
  110. $str.= "========== ===========下一列火車上==== =============================
    }
  111. echo $str;
複製程式碼

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板