diff --git a/application/third_party/ctrip/controllers/ctrip_train.php b/application/third_party/ctrip/controllers/ctrip_train.php new file mode 100644 index 00000000..41509649 --- /dev/null +++ b/application/third_party/ctrip/controllers/ctrip_train.php @@ -0,0 +1,225 @@ +load->helper('ctrip'); + $this->load->model("ctrip_model"); + date_default_timezone_set('PRC'); + + } + + public function index(){ + exit('hello ctrip'); + } + + //查询车次余票 + public function search(){ + //接收参数 + $date = $this->input->get_post('date'); + $from = $this->input->get_post('from'); + $to = $this->input->get_post('to'); + $TrainNo = $this->input->get_post('TrainNo'); + + if(!$date || !$from || !$to){ + exit('传参错误!'); + }else{ + $isCache = false; + $iseven = false; + $cachedata = false; + $now_time = time(); + $differ = (strtotime($date) - $now_time) / 86400; + if($differ > 29){ + $date = date('Y-m-d',strtotime('+7day')); + $iseven = true; + } + + //转换三字码为中文 + $from = $this->ctrip_model->ReplaceCodeToName($from)->station_name; + $to = $this->ctrip_model->ReplaceCodeToName($to)->station_name; + + //生成请求链接 + $TimeStamp = time(); + $Sign = md5($TimeStamp.DATAKEY); + + $url = JSONRETURN.'SearchS2S/?From='.urlencode($from).'&To='.urlencode($to).'&DepartDate='.$date.'&User='.DATAUSER.'&TimeStamp='.$TimeStamp.'&Sign='.$Sign; + + //获取数据 + $ResponseJson = post_ctrip($url,'','GET'); + + $ResponseData = json_decode($ResponseJson); + + //定义返回的json + $ReturnData = new stdClass(); + $ReturnData->httpstatus = 200; + $ReturnData->data = new stdClass(); + $ReturnData->data->seven = $iseven; + $ReturnData->data->cache = $isCache; + $ReturnData->data->result = array(); + $ReturnData->data->map = new stdClass(); + $obj = array(); + $i = 0; + $PriceStr = ''; + //数据解析 + if(!empty($ResponseData->Trains)){ + foreach ($ResponseData->Trains as $TrainInfo){ + $obj[$TrainInfo->FromTelcode] = $TrainInfo->FromStationName; + $obj[$TrainInfo->ToTelcode] = $TrainInfo->ToStationName; + $SeaType = ''; + foreach($TrainInfo->Seats as $Seats){ + if($Seats->SeatName == '高级软卧下'){ + $gjrwXiaPrice = $Seats->Price * 10; + $SeaType .= '"6":"'.$gjrwXiaPrice.'","A6":"¥'.$Seats->Price.'",'; + $gjrwNum = $Seats->TicketLeft; + } + + if($Seats->SeatName == '软卧下'){ + $rwPrice = $Seats->Price * 10; + $SeaType .= '"4":"'.$rwPrice.'","A4":"¥'.$Seats->Price.'",'; + $rwNum = $Seats->TicketLeft; + } + + if($Seats->SeatName == '软座'){ + $rzPrice = $Seats->Price * 10; + $SeaType .= '"4":"'.$rzPrice.'","A4":"¥'.$Seats->Price.'",'; + $rzNum = $Seats->TicketLeft; + } + + if($Seats->SeatName == '特等座'){ + $SeaType .= '"P":"¥'.$Seats->Price.'",'; + $tdzNum = $Seats->TicketLeft; + } + + if($Seats->SeatName == '无座'){ + $SeaType .= '"WZ":"¥'.$Seats->Price.'",'; + $wzNum = $Seats->TicketLeft; + } + + if($Seats->SeatName == '硬座'){ + $yzPrice = $Seats->Price * 10; + $SeaType .= '"1":"'.$yzPrice.'","A1":"¥'.$Seats->Price.'",'; + $yzNum = $Seats->TicketLeft; + } + + if($Seats->SeatName == '二等座'){ + $SeaType .= '"O":"¥'.$Seats->Price.'",'; + $edzNum = $Seats->TicketLeft; + } + + if($Seats->SeatName == '一等座'){ + $SeaType .= '"M":"¥'.$Seats->Price.'",'; + $ydzNum = $Seats->TicketLeft; + } + + if($Seats->SeatName == '商务座'){ + $swzPrice = $Seats->Price * 10; + $SeaType .= '"9":"'.$swzPrice.'","A9":"¥'.$Seats->Price.'",'; + $swzNum = $Seats->TicketLeft; + } + + if($Seats->SeatName == '硬卧下'){ + $ywPrice = $Seats->Price * 10; + $SeaType .= '"3":"'.$ywPrice.'","A3":"¥'.$Seats->Price.'",'; + $ywNum = $Seats->TicketLeft; + } + + if($Seats->SeatName == '动卧下'){ + $SeaType .= '"F":"¥'.$Seats->Price.'",'; + $dwNum = $Seats->TicketLeft; + } + + $PriceStr = $SeaType.'"train_no":'.'"'.$TrainInfo->TrainNo.'"'; + } + + //对返回的数据进行容错处理 + $gjrwNum = isset($gjrwNum) ? ticket_exchange($gjrwNum,$iseven) : ''; + $rwNum = isset($rwNum) ? ticket_exchange($rwNum,$iseven) : ''; + $rzNum = isset($rzNum) ? ticket_exchange($rzNum,$iseven) : ''; + $tdzNum = isset($tdzNum) ? ticket_exchange($tdzNum,$iseven) : ''; + $wzNum = isset($wzNum) ? ticket_exchange($wzNum,$iseven) : ''; + $ywNum = isset($ywNum) ? ticket_exchange($ywNum,$iseven) : ''; + $yzNum = isset($yzNum) ? ticket_exchange($yzNum,$iseven) : ''; + $edzNum = isset($edzNum) ? ticket_exchange($edzNum,$iseven) : ''; + $ydzNum = isset($ydzNum) ? ticket_exchange($ydzNum,$iseven) : ''; + $swzNum = isset($swzNum) ? ticket_exchange($swzNum,$iseven) : ''; + $dwNum = isset($dwNum) ? ticket_exchange($dwNum,$iseven) : ''; + //$runTime = $TrainInfo->DurationMinutes % 60; + + $ReturnData->data->result[$i] = '|预定|'.$TrainInfo->Train12306No.'|'.$TrainInfo->TrainNo.'|'.$TrainInfo->FromStationName.'|'.$TrainInfo->ToStationName.'|'.$TrainInfo->FromTelcode.'|'.$TrainInfo->ToTelcode.'|'.$TrainInfo->StartTime.'|'.$TrainInfo->ArriveTime.'|'.$TrainInfo->DurationMinutes.'|'.$TrainInfo->CanWebBuy.'||'.date('Ymd',strtotime($date)).'||||||||'.$gjrwNum.'||'.$rwNum.'|'.$rzNum.'|'.$tdzNum.'|'.$wzNum.'||'.$ywNum.'|'.$yzNum.'|'.$edzNum.'|'.$ydzNum.'|'.$swzNum.'|'.$dwNum.'||'; + + $data = '{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{'.$PriceStr.'},"messages":[],"validateMessages":{}}'; + $ReturnData->data->price[$i] = $data; + $i++; + } + } + //print_r($ResponseData); + $ReturnData->data->map = (object)$obj; + print_r(json_encode($ReturnData)); + } + } + + //查询经停站 + public function GetStopStation(){ + //接收参数 + $DepartDate = $this->input->get_post('DepartDate'); + $TrainNo = $this->input->get_post('TrainNo'); + + if(!$DepartDate || !$TrainNo){ + exit('传参错误!'); + }else{ + //生成请求链接 + $TimeStamp = time(); + $Sign = md5($TimeStamp.DATAKEY); + + $url = JSONRETURN.'GetStopStations/?DepartDate='.$DepartDate.'&TrainNo='.$TrainNo.'&User='.DATAUSER.'&TimeStamp='.$TimeStamp.'&Sign='.$Sign; + echo $url; + + } + } + + //获取所有站点信息 + public function GetAllStation(){ + //生成请求参数 + $TimeStamp = time(); + $Sign = md5($TimeStamp.DATAKEY); + + $url = JSONRETURN.'/GetAllStations/?&User='.DATAUSER.'&TimeStamp='.$TimeStamp.'&Sign='.$Sign; + + $ResponseJson = post_ctrip($url,'','GET'); + + $ResponseData = json_decode($ResponseJson); + + $data = array(); + foreach ($ResponseData->Stations as $items){ + $data['StationName'] = isset($items->StationName) ? checkNull($items->StationName) : ''; + $data['PinYin'] = isset($items->PinYin) ? checkNull($items->PinYin) : ''; + $data['Telecode'] = isset($items->Telecode) ? checkNull($items->Telecode) : ''; + $data['Address'] = isset($items->Address) ? checkNull($items->Address) : ''; + $data['Geography'] = isset($items->Geography) ? checkNull($items->Geography) : ''; + $this->ctrip_model->AddOrUpdate($data); + } + } + + +} \ No newline at end of file diff --git a/application/third_party/ctrip/helpers/ctrip_helper.php b/application/third_party/ctrip/helpers/ctrip_helper.php new file mode 100644 index 00000000..fe9c072d --- /dev/null +++ b/application/third_party/ctrip/helpers/ctrip_helper.php @@ -0,0 +1,77 @@ += 99){ + return '有'; + }else{ + return $num; + } + } + } + } + + //发送请求函数 + function post_ctrip($url, $data = '', $method = 'GET') { + $curl = curl_init(); // 启动一个CURL会话 + curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在 + curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器 + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 + curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer + if ($method == 'POST' && !empty($data)) { + curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包 + curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); + } + curl_setopt($curl, CURLOPT_TIMEOUT, 40); // 设置超时限制防止死循环 + curl_setopt($curl, CURLOPT_TIMEOUT_MS, 40000); // 设置超时限制防止死循环 + curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 + $tmpInfo = curl_exec($curl); // 执行操作 + $errno = curl_errno($curl); + if ($errno !== 0) { + log_message('error', 'ctripost'.$errno.curl_error($curl)); + } + curl_close($curl); //关闭CURL会话 + return $tmpInfo; //返回数据 + } + +?> \ No newline at end of file diff --git a/application/third_party/ctrip/models/ctrip_model.php b/application/third_party/ctrip/models/ctrip_model.php new file mode 100644 index 00000000..fac981e4 --- /dev/null +++ b/application/third_party/ctrip/models/ctrip_model.php @@ -0,0 +1,68 @@ +INFO = $this->load->database('INFO', TRUE); + } + + //» + public function AddOrUpdate($data){ + $sql = "IF NOT EXISTS( + SELECT 1 + FROM train_stations + WHERE + station_telecode = '{$data['Telecode']}' + ) BEGIN + INSERT INTO train_stations + ( + station_name, + station_telecode, + station_geography, + station_pinyin, + station_address + ) + VALUES + ( + '{$data['StationName']}','{$data['Telecode']}','{$data['Geography']}','{$data['PinYin']}','{$data['Address']}' + ) + END + ELSE + BEGIN + UPDATE train_stations + SET station_name = '{$data['StationName']}', + station_geography = '{$data['Geography']}', + station_pinyin = '{$data['PinYin']}', + station_address = '{$data['Address']}' + WHERE + station_telecode = '{$data['Telecode']}' + END + "; + $query = $this->INFO->query($sql); + return $query; + } + + //ȡĻϢ + //ȡ7ǰݾͲκݣҽɾ + public function get_train_cache($tpc_from_station,$tpc_to_station){ + $sql = "SELECT + * + FROM + TrainPriceCache + WHERE + tpc_from_station = '$tpc_from_station' + AND + tpc_to_station = '$tpc_to_station'"; + $query = $this->INFO->query($sql); + return $query->row(); + } + + //ȡվ + public function ReplaceCodeToName($code){ + $sql = "SELECT station_name from train_stations where station_telecode = '{$code}'"; + $query = $this->INFO->query($sql); + return $query->row(); + } + +} +?> \ No newline at end of file