load->model("BIZ_train_model");//加载模型 } //查询聚合余票接口,对返回的数据进行处理 public function index($train_date=null,$fromStation=null,$toStation=null){ //定义一些变量 $flag = true; $juheTrainInfo = ''; set_time_limit(0); //判断数据是否合法 if(!$train_date || !$fromStation || !$toStation){ exit('传参不能为空!'); }else{ if(strlen($fromStation) != 3){ exit('fromStation:参数错误!'); } if(strlen($toStation) != 3){ exit('toStation:参数错误!'); } //调用查询模块 $search_return = $this->Searchtrain($train_date,$fromStation,$toStation); //调用数据处理模块 $returnJson = $this->dataOperate($search_return,$fromStation,$toStation); //调用拼接处理模块 $trainjson = $this->createTrainJson($returnJson); print_r($trainjson); } } //搜索模块 function Searchtrain($train_date,$fromStation,$toStation){ $this->train_date = $train_date; $now_time = time(); $differ_time = (strtotime($train_date) - $now_time) / 86400; //屏蔽掉超过预售期的搜索,提高速度 if($differ_time > 29){ $train_date = date('Y-m-d',strtotime('+7day')); $this->seventh = true; } $url = 'http://139.129.246.118:12309/trainTickets/ticketsAvailable?key='.JUHE_TRAIN_API_KEY.'&train_date='.$train_date.'&from_station='.$fromStation.'&to_station='.$toStation; $this->url = $url; $train_info = $this->get_http($url); //如果为网络错误就再执行一次 if(json_decode($train_info)->reason == '网络故障,请重试(0)'){ $train_info = $this->get_http($url); } return $train_info; } //缓存处理模块(包含缓存处理) function dataOperate($search_return,$fromStation,$toStation){ $this->reason = json_decode($search_return)->reason; $this->cache = 'no'; if(!empty($search_return) && !empty(json_decode($search_return)->result)){ $this->BIZ_train_model->addOrUpdate($fromStation,$toStation,$search_return); $operate_data = $search_return; }else{ $cache_train_info = $this->BIZ_train_model->get_train_info($fromStation,$toStation); if(empty($cache_train_info)){ log_message('error','TRAIN 查询为空的链接:'.$this->url.'|出错的原因:'.$this->reason.'|缓存为空'); $operate_data = NULL; }else{ $cache_time = $cache_train_info->tpc_datetime; $now_time = time(); $differ_time = ($now_time - strtotime($cache_time)) / 86400; if($differ_time >= 3){ $this->BIZ_train_model->delete_traincache($fromStation,$toStation); log_message('error','TRAIN 查询为空的链接:'.$this->url.'|出错的原因:'.$this->reason.'|缓存超时,已经删除'); $operate_data = NULL; }else{ $this->cache = 'yes'; $operate_data = $cache_train_info->tpc_content; log_message('error','TRAIN 查询为空的链接:'.$this->url.'|出错的原因:'.$this->reason.'|调用缓存号:'.$cache_train_info->tpc_sn.'|缓存生成的时间:'.$cache_train_info->tpc_datetime); } } } return $operate_data; } //字符串拼接模块 function createTrainJson($returnjson){ $return_data = new stdClass(); $return_data->data = new stdClass(); $return_data->httpstatus = 200; $return_data->reason = $this->reason; $return_data->cache = $this->cache; $return_data->data->result = array(); $return_data->data->map = new stdClass(); $obj = array(); $i = 0; $pricestr = ''; if(!empty($returnjson)){ if(!empty(json_decode($returnjson)->result->list)){ foreach (json_decode($returnjson)->result->list as $value){ $obj[$value->from_station_code] = $value->from_station_name; $obj[$value->to_station_code] = $value->to_station_name; $seat_type = ''; if(isset($value->gjrw_price)){ if($value->gjrw_price > 0){ $gjrwPrice = $value->gjrw_price * 10; $seat_type .= '"6":"'.$gjrwPrice.'","A6":"¥'.$value->gjrw_price.'",'; } } if(isset($value->qtxb_price)){ if($value->qtxb_price > 0){ $seat_type .= '"H":"¥'.$value->qtxb_price.'",'; } } if(isset($value->rw_price)){ if($value->rw_price > 0){ $rwPrice = $value->rw_price * 10; $seat_type .= '"4":"'.$rwPrice.'","A4":"¥'.$value->rw_price.'",'; } } if(isset($value->rz_price)){ if($value->rz_price > 0){ $rzPrice = $value->rz_price * 10; $seat_type .= '"2":"'.$rzPrice.'","A2":"¥'.$value->rz_price.'",'; } } if(isset($value->tdz_price)){ if($value->tdz_price > 0){ $seat_type .= '"P":"¥'.$value->tdz_price.'",'; } } if(isset($value->wz_price)){ if($value->wz_price > 0){ $wzPrice = $value->wz_price * 10; $seat_type .= '"WZ":"¥'.$value->wz_price.'",'; } } if(isset($value->yw_price)){ if($value->yw_price > 0){ $ywPrice = $value->yw_price * 10; $seat_type .= '"3":"'.$ywPrice.'","A3":"¥'.$value->yw_price.'",'; } } if(isset($value->yz_price)){ if($value->yz_price > 0){ $yzPrice = $value->yz_price * 10; $seat_type .= '"1":"'.$yzPrice.'","A1":"¥'.$value->yz_price.'",'; } } if(isset($value->edz_price)){ if($value->edz_price > 0){ $seat_type .= '"O":"¥'.$value->edz_price.'",'; } } if(isset($value->ydz_price)){ if($value->ydz_price > 0){ $seat_type .= '"M":"¥'.$value->ydz_price.'",'; } } if(isset($value->swz_price)){ if($value->swz_price > 0){ $swzPrice = $value->swz_price * 10; $seat_type .= '"9":"'.$swzPrice.'","A9":"¥'.$value->swz_price.'",'; } } if(isset($value->dw_price)){ if($value->dw_price > 0){ $seat_type .= '"F":"¥'.$value->dw_price.'",'; } } $pricestr = $seat_type.'"train_no":'.'"'.$value->train_no.'"'; //余票字符串 $return_data->data->result[$i] = '|预定|'.$value->train_no.'|'.$value->train_code.'|'.$value->from_station_name.'|'.$value->to_station_name.'|'.$value->from_station_code.'|'.$value->to_station_code.'|'.$value->start_time.'|'.$value->arrive_time.'|'.$value->run_time.'|'.$value->can_buy_now.'||'.$value->train_start_date.'||||||||'.$this->ticket_exchange($value->gjrw_num).'|'.$this->ticket_exchange($value->qtxb_num).'|'.$this->ticket_exchange($value->rw_num).'|'.$this->ticket_exchange($value->rz_num).'|'.$this->ticket_exchange($value->tdz_num).'|'.$this->ticket_exchange($value->wz_num).'||'.$this->ticket_exchange($value->yw_num).'|'.$this->ticket_exchange($value->yz_num).'|'.$this->ticket_exchange($value->edz_num).'|'.$this->ticket_exchange($value->ydz_num).'|'.$this->ticket_exchange($value->swz_num).'|'.$this->ticket_exchange($value->dw_num).'||'; $data = '{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{'.$pricestr.'},"messages":[],"validateMessages":{}}'; $return_data->data->price[$i] = $data; $i++; } } } $return_data->data->map = (object)$obj; return json_encode($return_data); } //获取价格(废弃) /* fromStationCode:出发站三字码 toStationCode:终点站三字码 trainCode:车次号 */ public function get_price($fromStationCode=null,$toStationCode=null,$trainCode=null){ if(!$fromStationCode || !$toStationCode || !$trainCode){ exit('传参错误!'); }else{ $return_data = $this->BIZ_train_model->get_price($fromStationCode,$toStationCode,$trainCode); if(!empty($return_data)){ print_r(json_decode($return_data->TPL_Price)); }else{ print_r('没有数据返回'); } } } //余票转换 function ticket_exchange($num){ if($this->cache == 'yes'){ $time = strtotime($this->train_date) - time(); $day = $time / 86400; if($day > 15){ return '有'; }else{ return $num; } }else{ if(is_numeric($num)){ if($num == 0){ return '无'; }elseif($num >= 99){ return '有'; }else{ return $num; } }else{ if($num == '--'){ return ''; } } } } //发送请求 function get_http($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_TIMEOUT, 45); // 设置超时限制防止死循环 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 $tmpInfo = curl_exec($curl); // 执行操作 $errno = curl_errno($curl); if ($errno !== 0) { return false; $error_message = $errno . ' ' . curl_error($curl); //记录错误日志 log_message('error', "third_party/train_app/index/get_http curl {$error_message}"); } curl_close($curl); //关闭CURL会话 return $tmpInfo; //返回数据 } } ?>