사실 제 코드는 나온지 오래됐지만 오픈소스 차이나에는 올린 적도 없고, 당연히 프로그램이 최적화되지 않은 걸 보니 웃겼습니다. 그날은 파이썬 하나!그래도 상관없어 모두가 볼 수 있도록 올릴게요~~ 작년에 12306도 보냈는데 그거 너무 단순하고 가격 문의 같은 것도 없었어요 이 기능은 비교적 완벽해요.
- /*
- *@Description 열차표 잔여권 조회 및 가격 조회
- *@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 = 폭발(" -", $data);
- if(count($num)==2){
- $date = date("Y",time())."-".$data;
- } else if(count($num)==3){
- $date = $data;
- }else{
- exit("시간 오류");
- }
- }
- if(empty($startstaion)||empty($endstation)){
- exit("stdin error");
- }
- $url ="http://kyfw.12306.cn/otn/ lcxxcx/query?목적_코드=ADULT&queryDate=$date&from_station=$startstaion&to_station=$endstation";
- $data = doget($url);
- if(!$data['status']){
- 종료( '검사 오류');
- }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[$ 키]["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" , '헤더' => "사용자 에이전트:Mozilla/4.0(호환; 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)rn수락:*/*rnReferer:https://kyfw.12306.cn/otn/lcxxcx/init"));
- $file_contents = file_get_contents($url, false , stream_context_create($optionget)) ;
- }else {
- $ch = 컬_init();
- $timeout = 5;
- $header = array(
- 'Accept:*/*',
- 'Accept-Charset:GBK,utf -8;q=0.7,*;q=0.3',
- '수용-인코딩:gzip,deflate,sdch',
- '수용 언어:zh-CN,zh;q=0.8,ja;q =0.6,en;q=0.4',
- '연결:keep-alive',
- '호스트:kyfw.12306.cn',
- '참조자:https://kyfw.12306.cn/ otn/lcxxcx/init',
- );
- 컬_setopt($ch, CURLOPT_URL, $url);
- 컬_setopt($ch, CURLOPT_HTTPHEADER,$header);
- 컬_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- 컬_setopt($ch, CURLOPT_SSL_VERIFYHOST,1);
- 컬_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- 컬_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- $file_contents = 컬_exec ($ch);
- 컬_닫기($ch);
- }
- $file_contents = json_decode($file_contents,true);
- return $file_contents;
- }
- /*
- *这个由于时间紧写的, 所以很多地方还需要优化, 希望大家改善一下, 可以分身와文件缓存적형의형式提高执行速島--缺点执行速島慢--유효화
- */
- $data = gettrain("北京","天津","01-03");
- /*
- * ["gr_num"]=>高级软卧
- * ["qt_num"]=> ;其他
- * ["rw_num"]=> 软卧
- * ["rz_num"]=>软座
- * ["tz_num"]=>特等座
- * ["wz_num"]=>无座
- * ["yw_num "]=>硬卧
- * ["yz_num"]=>硬座
- * ["ze_num"]=>2等座
- * ["zy_num"]=> 一等座
- * ["swz_num"]=> 商务座
- */
- $str="";
- foreach($data as $key =>$value){
- $str.="화차车列次:{$data[$ key]['station_train_code']}";
- $str.="始发站:{$data[$key]['start_station_name']},终点站{$data[$key]['end_station_name'] }
";
- $str.="출发时间:{$data[$key]['start_time']},到站时间:{$data[$key]['arrive_time']}< ;br>";
- $str.="";
- $str.= strpos($data[$key]["gr_num"],"()") ? "" :"높은 수준의 데이터[$key]["gr_num"]."
";
- $str.= strpos($data[$key]["qt_num" "],"()") ? "" :"其他余票화价格:".$data[$key]["qt_num"]."
";
- $str.= strpos($data[$key]["rw_num" ],"()") ? "" :"软卧余票화价格:".$data[$key]["rw_num"]."
";
- $str.= strpos($data[$key]["rz_num" ],"()") ? "" :"软座余票화价格:".$data[$key]["rz_num"]."
";
- $str.= strpos($data[$key]["tz_num "],"()") ? "" :"特等座余票화价格:".$data[$key]["tz_num"]."
";
- $str.= strpos($data[$key]["wz_num "],"()") ? "" :"无座余票화价格:".$data[$key]["wz_num"]."
";
- $str.= strpos($data[$key]["yw_num" "],"()") ? "" :"硬卧余票화价格:".$data[$key]["yw_num"]."
";
- $str.= strpos($data[$key]["yz_num" ],"()") ? "" :"硬座余票화价格:".$data[$key]["yz_num"]."
";
- $str.= strpos($data[$key]["ze_num" ],"()") ? "" :"이중 저장 데이터:".$data[$key]["ze_num"]."
";
- $str.= strpos($data[$key][" zy_num"],"()") ? "" :"一等座余票和价格:".$data[$key]["zy_num"]."
";
- $str.= strpos($data[$key][" swz_num"],"()") ? "" :"商务座余票화价格:".$data[$key]["swz_num"]."
";
- $str.= "========== ============下一列火车上===============================
- }
- echo $str;
复제대码
|