You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
information-system/application/third_party/tuniu/controllers/tuniu_train.php

269 lines
10 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
//途牛apiurl
define("TUNIU_URL","218.94.82.118:13532");
//途牛apikey
define("TUNIU_KEY","Te56CBQmorzJGeYIIK");
class Tuniu_train extends CI_Controller{
public function __construct(){
// header("Content-Type: text/html;charset=utf-8");
parent::__construct();
date_default_timezone_set('PRC');
$this->load->model("tuniuprice_model");
}
public function index(){
echo 'hello tuniu';
}
//创建请求签名
public function create_sign(){
$time = date('Y-m-d H:i:s',time());
$secretKey = 'qvHMJVywEQqsd4EneHQl';
$id = 'retailId25';
$str = 'planDateStrhaina';
$timeStamp = 'timestamp'.$time;
$sign = $secretKey.$id.'apiKey'.TUNIU_KEY.$str.$timeStamp.$secretKey;
return strtoupper(md5($sign));
}
//获取余票
public function search($date,$fromStation,$toStation){
$sign = $this->create_sign();
$url = TUNIU_URL.'/train/search';
$time = date('Y-m-d H:i:s',time());
$post_data = '{
"apiKey": "'.TUNIU_KEY.'",
"sign": "'.$sign.'",
"timestamp": "'.$time.'",
"data": { "trainDate": "'.$date.'", "fromStation": "'.$fromStation.'", "toStation": "'.$toStation.'", "trainCode": "" }
}';
$back_data = $this->get_http($url,$post_data,$method = 'POST');
//print_r('返回码'.json_decode($back_data)->returnCode.'<br>');
$return_data = new stdClass();
$return_data->data = new stdClass();
$return_data->data->result = array();
$return_data->returnCode = json_decode($back_data)->returnCode;
$i = 0;
$pricestr = '';
foreach (json_decode($back_data)->data as $value){
$seat_type = '';
if(is_numeric($value->gjrwPrice)){
$gjrwPrice = $value->gjrwPrice * 10;
$seat_type .= '"6":"'.$gjrwPrice.'","A6":"¥'.$value->gjrwPrice.'",';
}
if(is_numeric($value->qtxbPrice)){
$seat_type .= '"H":"¥'.$value->qtxbPrice.'",';
}
if(is_numeric($value->rwPrice)){
$rwPrice = $value->rwPrice * 10;
$seat_type .= '"4":"'.$rwPrice.'","A4":"¥'.$value->rwPrice.'",';
}
if(is_numeric($value->rzPrice)){
$rzPrice = $value->rzPrice * 10;
$seat_type .= '"2":"'.$rzPrice.'","A2":"¥'.$value->rzPrice.'",';
}
if(is_numeric($value->tdzPrice)){
$seat_type .= '"P":"¥'.$value->tdzPrice.'",';
}
if(is_numeric($value->wzPrice)){
$wzPrice = $value->wzPrice * 10;
$seat_type .= '"WZ":"¥'.$value->wzPrice.'",';
}
if(is_numeric($value->ywPrice)){
$ywPrice = $value->ywPrice * 10;
$seat_type .= '"A3":"'.$ywPrice.'","3":"¥'.$value->ywPrice.'",';
}
if(is_numeric($value->yzPrice)){
$yzPrice = $value->yzPrice * 10;
$seat_type .= '"A1":"'.$yzPrice.'","1":"¥'.$value->yzPrice.'",';
}
if(is_numeric($value->edzPrice)){
$seat_type .= '"O":"¥'.$value->edzPrice.'",';
}
if(is_numeric($value->ydzPrice)){
$seat_type .= '"M":"¥'.$value->ydzPrice.'",';
}
if(is_numeric($value->swzPrice)){
$swzPrice = $value->swzPrice * 10;
$seat_type .= '"A9":"'.$swzPrice.'","9":"¥'.$value->swzPrice.'",';
}
$pricestr = $seat_type.'"train_no":'.'"'.$value->trainNo.'"';
$return_data->data->result[$i] = '|预定|'.$value->trainNo.'|'.$value->trainCode.'|'.$value->fromStationName.'|'.$value->toStationName.'|'.$value->fromStationCode.'|'.$value->toStationCode.'|'.$value->startTime.'|'.$value->arriveTime.'|'.$value->runTime.'|'.$value->canBuyNow.'||'.$value->trainStartDate.'||||||||'.$value->gjrwNum.'|'.$value->qtxbNum.'|'.$value->rwNum.'|'.$value->rzNum.'|'.$value->tdzNum.'|'.$value->wzNum.'||'.$value->ywNum.'|'.$value->yzNum.'|'.$value->edzNum.'|'.$value->ydzNum.'|'.$value->swzNum.'|'.$value->dwNum.'||';
$i++;
$data = '{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{'.$pricestr.'},"messages":[],"validateMessages":{}}';
$this->tuniuprice_model->addOrUpdate($value->trainNo,$value->fromStationCode,$value->toStationCode,json_encode($data));
}
print_r(json_encode($return_data));
}
//获取价格
/*
fromStationCode出发站三字码
toStationCode:终点站三字码
trainCode:车次
*/
public function get_price($fromStationCode,$toStationCode,$trainCode){
$return_data = $this->tuniuprice_model->get_price($fromStationCode,$toStationCode,$trainCode);
print_r(json_decode($return_data->tnp_price));
}
//占座下单
public function book_ticket(){
$sign = $this->create_sign();
$url = TUNIU_URL.'/train/book';
$time = date('Y-m-d H:i:s',time());
$data = '{
"retailOrderId": "1",
"cheCi": "D2946",
"fromStationCode": "GLZ",
"fromStationName":"桂林",
"toStationCode":"GBZ",
"toStationName":"桂林北",
"trainDate""2017-09-23",
"callBackUrl":"http://www.mycht.cn/info.php/apps/train/tuniu_callback/book",
"contact":"陈宇超",
"phone":"18877381547",
"insureCode ":"",
"passengers":[{
"passengerId":1,
"ticketNo": null,
"passengerName": "陈宇超",
"passportNo": "450302199208131039",
"passportTypeId": "1",
"passportTypeName": "二代身份证",
"piaoType": "1",
"piaoTypeName": "成人票",
"zwCode": "O",
"zwName": "二等座",
"cxin": null,
"price": "5.5",
"reason": 0,
"provinceCode": null,
"schoolCode": null,
"schoolName": null,
"studentNo": null,
"schoolSystem": null,
"enterYear": null,
"preferenceFromStationName": null,
"preferenceFromStationCode": null,
"preferenceToStationName": null,
"preferenceToStationCode": null
}]
}';
$post_data = '{
"apiKey": "'.TUNIU_KEY.'",
"sign": "'.$sign.'",
"timestamp": "'.$time.'",
"data": "'.$data.'"
}';
//print_r($url);
print_r($post_data);
//$back_data = $this->get_http($url,$post_data,$method = 'POST');
//print_r($back_data);
}
//身份验证
public function validate(){
//$name = $this->input->post('name');
//$identityType = $this->input->post('identityType');
//$identifyCard = $this->input->post('identifyCard');
$sign = $this->create_sign();
$url = TUNIU_URL.'/train/validate';
$time = date('Y-m-d H:i:s',time());
$post_data = '{
"apiKey": "'.TUNIU_KEY.'",
"sign": "'.$sign.'",
"timestamp": "'.$time.'",
"data": { "name": "", "identityType": "", "identifyCard": ""}
}';
$back_data = $this->get_http($url,$post_data,$method = 'POST');
print_r($back_data);
}
public function des_encrypt() {
$this->load->library('Des');
$key = 'Te56CBQmorzJGeYIIK';
$str = '{
"retailOrderId": "452514",
"cheCi": "D2946",
"fromStationCode": "GLZ",
"fromStationName":"桂林",
"toStationCode":"GBZ",
"toStationName":"桂林北",
"trainDate":"2017-09-11",
"callBackUrl":"http://www.mycht.cn/info.php/apps/train/tuniu_callback/book",
"hasSeat":false,
"contact":"陈宇超",
"phone":"18877381547",
"insureCode ":"",
"passengers":[{
"passengerId":1,
"ticketNo": null,
"passengerName": "陈宇超",
"passportNo": "450302199208131039",
"passportTypeId": "1",
"passportTypeName": "二代身份证",
"piaoType": "1",
"piaoTypeName": "成人票",
"zwCode": "O",
"zwName": "二等座",
"cxin": null,
"price": "5.5",
"reason": 0,
"provinceCode": null,
"schoolCode": null,
"schoolName": null,
"studentNo": null,
"schoolSystem": null,
"enterYear": null,
"preferenceFromStationName": null,
"preferenceFromStationCode": null,
"preferenceToStationName": null,
"preferenceToStationCode": null
}]
}';
$crypt = new DES();
$mstr = $crypt->encrypt($str,$key);
echo $mstr;
}
public function des_decrypt(){
echo base64_decode('JSh+QwFlC/uPYxpXDpx+g6LQ/82MAh3czFaFvSowBUt/A6LpIVPcNzYC5H8RpjlsGCQ67zcTJs6KA/mnW7+qiVZNgOvRi3x7at57s8k0sYEY23ow5dLanooD+adbv6qJVk2A69GLfHsZ2P7tON4xzWuPgXGftReOigP5p1u/qok8tDL6rQsR1Up+EbOJa5K9/a5Z/gAXP1rb3H95xn1oBIBRwRXG0nS0tHBZ3EcG/+WzzqcER2gRP7O9cXSfnYfagk9nRgjgwCgKEQ8njmZn/JKTf9hwxv9Le/fJyy/abAkUzYx5upiR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Qi51ro0xdTeTPpiibrr+JKiAyHKBlgmrsX+OX6ej4ks19Q5D+98vgpAT13mlSr0Q/lKffCXRX0VOdhhM=');
}
//发送请求函数
public 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_HTTPHEADER, array('Content-Type:application/json'));
}
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;
echo $errno . curl_error($curl); //记录错误日志
}
curl_close($curl); //关闭CURL会话
return $tmpInfo; //返回数据
}
}