<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	//验证参数是否存在并且是否为数字
	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 get_microtime (){
		list($s1, $s2) = explode(' ', microtime());
		return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); 
	}
	
	//获取飞机查询报文
	function get_serach_json($departime,$fromcode,$tocode,$flightnumber=null){
		$timestamp = time();
		$function = 'queryNew';
		$flightnumberstr = '';
		if(!empty($flightnumber)){
			$flightnumberstr = ',"flightNum":"'.$flightnumber.'"';
		}
		
		$data = '{"channelId":7,"departureDate":"'.$departime.'","dstCityCode":"'.$tocode.'"'.$flightnumberstr.',"orgCityCode":"'.$fromcode.'"}';
		$sign = md5(TUNIU_FLIGHT_KEY.$data.$timestamp);
		
		$postJson = '{
			"purchaseId": '.TUNIU_FLIGHT_ID.',
			"sign": "'.$sign .'",
			"timestamp": '.$timestamp.',
			"function": "'.$function.'",
			"data":'.$data.'
		}';
		
		return $postJson;
	}
	
	//发送请求函数
	function post_tuniu($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) {
			return false;
			echo $errno . curl_error($curl); //记录错误日志
		}
		curl_close($curl); //关闭CURL会话  
		return $tmpInfo; //返回数据
	}

?>