携程数据查询
parent
f54d532d2e
commit
dd1b94d738
@ -0,0 +1,225 @@
|
||||
<?php
|
||||
if (!defined('BASEPATH'))
|
||||
exit('No direct script access allowed');
|
||||
|
||||
//数据接口
|
||||
define("DATAUSER","guilintravel");
|
||||
define("DATAKEY","0dbf1652763c2289533e27437a971854");
|
||||
|
||||
//订单接口
|
||||
define("ORDERUSER","guilintravel");
|
||||
define("ORDERKEY","07f811fe29f04008a8fcc86e81c012b9");
|
||||
|
||||
//数据返回格式地址
|
||||
define("JSONRETURN","http://m.ctrip.com/restapi/soa2/12976/json/");
|
||||
define("XMLRETURN","http://m.ctrip.com/restapi/soa2/12976/xml/");
|
||||
|
||||
|
||||
class ctrip_train extends CI_Controller{
|
||||
|
||||
public function __construct(){
|
||||
parent::__construct();
|
||||
header('Access-Control-Allow-Origin:*');
|
||||
header('Access-Control-Allow-Methods:POST, GET');
|
||||
header('Access-Control-Max-Age:0');
|
||||
header('Access-Control-Allow-Headers:x-requested-with, Content-Type');
|
||||
header('Access-Control-Allow-Credentials:true');
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
//中文转换为座位简码
|
||||
/*
|
||||
硬座、软座、二等座、一等座、特等座、商务座、硬卧、软卧、高级软卧、棚车、包厢硬卧、一等软座、二等软座、高级动卧、混编硬座、混编硬卧、包厢软座、特等软座、动卧、二人软包、一人软包、一等双软、二等双软、混编软座、混编软卧、观光座、一等包座、无座、硬卧上、硬卧中、硬卧下、软卧上、软卧下、高级软卧上、高级软卧下、高级动卧上、高级动卧下、动卧上、动卧下
|
||||
*/
|
||||
function ReplaceToCode($SeatName){
|
||||
|
||||
}
|
||||
|
||||
//验证站点信息是否为空
|
||||
function checkNull($info){
|
||||
if(empty($info)){
|
||||
return '';
|
||||
}else{
|
||||
return $info;
|
||||
}
|
||||
}
|
||||
|
||||
//验证参数是否存在并且是否为数字
|
||||
function checkNum($num){
|
||||
if(isset($num)){
|
||||
if(is_numeric($num)){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//余票转换
|
||||
function ticket_exchange($num,$iseven){
|
||||
if($iseven){
|
||||
return '有';
|
||||
}else{
|
||||
if(is_numeric($num)){
|
||||
if($num == 0){
|
||||
return '无';
|
||||
}elseif($num >= 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; //返回数据
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue