/** * 根据根据条件数组 * @param $searchArea (取车,还车)条件 * @return array 返回rentalcars 数据 【数组格式】 */ public static function getSearchResultArrayForSearchArea($searchArea) { $Redis = YiiRedis::getInstance(); $search_result_array = array(); if (!empty($searchArea)) { $search = $searchArea; $array_params['PickUp']['Location']['id'] = $search['pickUp_location']; $array_params['PickUp']['Location']['country'] = $search['pickUp_country']; $array_params['PickUp']['Location']['city'] = $search['pickUp_city']; $array_params['DropOff']['Location']['id'] = $search['dropOff_location']; $array_params['DropOff']['Location']['country'] = $search['dropOff_country']; $array_params['DropOff']['Location']['city'] = $search['dropOff_city']; $pickUp_date_strtotime = strtotime($search['pickUp_date']); $pickUp_date_year = date('Y', $pickUp_date_strtotime); $pickUp_date_month = date('m', $pickUp_date_strtotime); $pickUp_date_day = date('d', $pickUp_date_strtotime); $array_params['PickUp']['Date']['year'] = $pickUp_date_year; $array_params['PickUp']['Date']['month'] = $pickUp_date_month; $array_params['PickUp']['Date']['day'] = $pickUp_date_day; $array_params['PickUp']['Date']['hour'] = $search['pickUp_hour']; $array_params['PickUp']['Date']['minute'] = $search['pickUp_minute']; $dropOff_date_strtotime = strtotime($search['dropOff_date']); $dropOff_date_year = date('Y', $dropOff_date_strtotime); $dropOff_date_month = date('m', $dropOff_date_strtotime); $dropOff_date_day = date('d', $dropOff_date_strtotime); $array_params['DropOff']['Date']['year'] = $dropOff_date_year; $array_params['DropOff']['Date']['month'] = $dropOff_date_month; $array_params['DropOff']['Date']['day'] = $dropOff_date_day; $array_params['DropOff']['Date']['hour'] = $search['dropOff_hour']; $array_params['DropOff']['Date']['minute'] = $search['dropOff_minute']; $array_params['DriverAge'] = 26; //搜素的时候年龄默认到25周岁以上 // $search_redis_key = 'carrental_search_' . md5(serialize(array_merge($array_params['PickUp'], $array_params['DropOff']))); $search_redis_key = 'carrental_search_' . md5(serialize($search)); if ($Redis->get($search_redis_key)) { $request_result_json = $Redis->get($search_redis_key, $array_params['DropOff']); } else { $json_params = array('json_params' => json_encode($array_params)); $request_url = HTTP_URL . 'index.php?r=CarrentalApi/Rc/SearchRQ'; //接口请求地址 $request_result_json = GetUrlContent::fileGetContent($json_params, $request_url, 300, 'POST'); //请求数据 if (!empty($request_result_json)) { $Redis->set($search_redis_key, $request_result_json, 60 * 60); } } $request_result_array = !empty($request_result_json) ? json_decode($request_result_json, true) : array(); $search_result_array = is_array($request_result_array) ? $request_result_array : array(); } return $search_result_array; }
/** *18查询门店车辆列表 * @param $json_params * @return string */ public static function SearchRQ($json_params) { $request_time = time(); // 验证JSON串的正确性 if (!empty($json_params) && IconvEncode::IsJson($json_params)) { // 解析Json $request_param = json_decode($json_params, true); // 验证Json串中每一组参数的正确性 [S] if (empty($request_param['DriverAge'])) { $request_param['DriverAge'] = 26; } // 验证PickUp、DropOff、DriverAge是否存在 if (empty($request_param['PickUp']) || empty($request_param['DropOff'])) { $return_json = json_encode(array('status' => false, 'error_msg' => '0x180002_PickUp、DropOff、DriverAge不可为空')); return $return_json; } // DriverAge只能是大于0小于100的数字 if (!is_numeric($request_param['DriverAge']) || $request_param['DriverAge'] <= 18 || $request_param['DriverAge'] >= 75) { $return_json = json_encode(array('status' => false, 'error_msg' => '0x180003_DriverAge只能是大于18小于75的数字')); return $return_json; } // 声明简短变量 $PickUp_Location = empty($request_param['PickUp']['Location']) ? '' : $request_param['PickUp']['Location']; // PickUp.Location $DropOff_Location = empty($request_param['DropOff']['Location']) ? '' : $request_param['DropOff']['Location']; // DropOff.Location $PickUp_Date = empty($request_param['PickUp']['Date']) ? '' : $request_param['PickUp']['Date']; // PickUp.Date $DropOff_Date = empty($request_param['DropOff']['Date']) ? '' : $request_param['DropOff']['Date']; // DropOff.Date // 验证PickUp与DropOff下的 Location与Date是否存在 if (empty($PickUp_Location) || empty($PickUp_Date) || empty($DropOff_Location) || empty($DropOff_Date)) { $return_json = json_encode(array('status' => false, 'error_msg' => '0x180004_PickUp与DropOff下的 Location与Date是否存在')); return $return_json; } // Location下正确的参数名 $Location_Key = array('id', 'country', 'city'); // 验证PickUp.Location 中的参数名是否正确 foreach ($PickUp_Location as $key => $val) { if (!in_array($key, $Location_Key)) { $return_json = json_encode(array('status' => false, 'error_msg' => '0x180005_PickUp. Location 下的参数名错误')); return $return_json; } } // 验证DropOff.Location 中的参数名是否正确 foreach ($DropOff_Location as $key => $val) { if (!in_array($key, $Location_Key)) { $return_json = json_encode(array('status' => false, 'error_msg' => '0x180006_DropOff. Location 下的参数名错误')); return $return_json; } } // 验证PickUp.Location 中是否为填写任何条件 [id / country / city] if (count($PickUp_Location) == 0) { $return_json = json_encode(array('status' => false, 'error_msg' => '0x180007_PickUp. Location 下缺少必填参数')); return $return_json; } else { // 如按国家+城市的方式查询 if (empty($PickUp_Location['id'])) { // country与city不能单独使用 如填写了country或city的其中一个 另一个也必须填写 if (empty($PickUp_Location['country']) && !empty($PickUp_Location['city']) || !empty($PickUp_Location['country']) && empty($PickUp_Location['city'])) { $return_json = json_encode(array('status' => false, 'error_msg' => '0x180008_单独使用了PickUp.Location 下的country或city')); return $return_json; } } } // 验证DropOff.Location 中是否为填写任何条件 [id / country / city] if (count($DropOff_Location) == 0) { $return_json = json_encode(array('status' => false, 'error_msg' => '0x180009_DropOff. Location 下缺少必填参数')); return $return_json; } else { // 如按国家+城市的方式查询 if (empty($DropOff_Location['id'])) { // country与city不能单独使用 如填写了country或city的其中一个 另一个也必须填写 if (empty($DropOff_Location['country']) && !empty($DropOff_Location['city']) || !empty($DropOff_Location['country']) && empty($DropOff_Location['city'])) { $return_json = json_encode(array('status' => false, 'error_msg' => '0x180010_单独使用了DropOff.Location 下的country或city')); return $return_json; } } } // PickUp.Date 中的参数均为必填项 if (empty($PickUp_Date['year']) || empty($PickUp_Date['month']) || empty($PickUp_Date['day']) || empty($PickUp_Date['hour']) || empty($PickUp_Date['minute'])) { $return_json = json_encode(array('status' => false, 'error_msg' => '0x180011_PickUp.Date下缺少必填参数')); return $return_json; } // DropOff.Date 中的参数均为必填项 if (empty($DropOff_Date['year']) || empty($DropOff_Date['month']) || empty($DropOff_Date['day']) || empty($DropOff_Date['hour']) || empty($DropOff_Date['minute'])) { $return_json = json_encode(array('status' => false, 'error_msg' => '0x180012_DropOff.Date 下缺少必填参数')); return $return_json; } $start_time = strtotime($PickUp_Date['year'] . '-' . $PickUp_Date['month'] . '-' . $PickUp_Date['day'] . ' ' . $PickUp_Date['hour'] . ':' . $DropOff_Date['minute']); $end_time = strtotime($DropOff_Date['year'] . '-' . $DropOff_Date['month'] . '-' . $DropOff_Date['day'] . ' ' . $DropOff_Date['hour'] . ':' . $DropOff_Date['minute']); // 验证时间范围是否正确 [例:2014-13-38 错误时间strtotime会返回false] if ($start_time && $end_time) { // 结束时间不能小于开始时间 if ($end_time < $start_time) { $return_json = json_encode(array('status' => false, 'error_msg' => '0x180014_DropOff.Date 小于了PickUp.Date')); return $return_json; } } else { $return_json = json_encode(array('status' => false, 'error_msg' => '0x180013_PickUp.Date或DropOff.Date不是正确的时间')); return $return_json; } // 验证Json串中每一组参数的正确性 [E] // 判断根据ID查询还是根据国家地区查询 $PickUp_Location = empty($request_param['PickUp']['Location']['id']) ? 'country="' . $request_param['PickUp']['Location']['country'] . '" city="' . $request_param['PickUp']['Location']['city'] . '"' : 'id="' . $request_param['PickUp']['Location']['id'] . '"'; $DropOff_Location = empty($request_param['DropOff']['Location']['id']) ? 'country="' . $request_param['DropOff']['Location']['country'] . '" city="' . $request_param['DropOff']['Location']['city'] . '"' : 'id="' . $request_param['DropOff']['Location']['id'] . '"'; //remoteIp 得到客户端ip $remoteIp = !empty($request_param['remoteIp']) ? $request_param['remoteIp'] : CARRENTALAPI_REMOTEIP; if (!filter_var($remoteIp, FILTER_VALIDATE_IP)) { $return_json = json_encode(array('status' => false, 'error_msg' => '0x180015_remoteIp不是正确ip地址')); return $return_json; } // 拼接XML <SearchRQ supplierInfo="true" showReviews="true" > $request_xml = ' <SearchRQ supplierInfo="true" prefcurr="USD"> <Credentials username="******" password="******" remoteIp="' . $remoteIp . '" /> <PickUp> <Location ' . $PickUp_Location . '/> <Date year="' . $request_param['PickUp']['Date']['year'] . '" month="' . $request_param['PickUp']['Date']['month'] . '" day="' . $request_param['PickUp']['Date']['day'] . '" hour="' . $request_param['PickUp']['Date']['hour'] . '" minute="' . $request_param['PickUp']['Date']['minute'] . '"/> </PickUp> <DropOff> <Location ' . $DropOff_Location . '/> <Date year="' . $request_param['DropOff']['Date']['year'] . '" month="' . $request_param['DropOff']['Date']['month'] . '" day="' . $request_param['DropOff']['Date']['day'] . '" hour="' . $request_param['DropOff']['Date']['hour'] . '" minute="' . $request_param['DropOff']['Date']['minute'] . '"/> </DropOff> <DriverAge>' . $request_param['DriverAge'] . '</DriverAge> <Tracking adcamp="example" adplat="example"/> </SearchRQ>'; // 调用接口获取数据 $request_data = array('xml' => $request_xml); $searchRQ = GetUrlContent::fileGetContent($request_data, CARRENTALAPI_URL, 100, 'POST'); // $searchRQ='<SearchRS> <MatchList> </MatchList></SearchRS>'; // ApiLogManage::addRentalcars('SearchRQ_once',$request_xml,$searchRQ,'',$request_time,time(),1);//记录日志 $simple_xml_obj = simplexml_load_string($searchRQ); if (!$simple_xml_obj) { //判断数据是否为空 $return_json = json_encode(array('status' => false, 'error_msg' => '0x180099_返回数据为空或生成xml对象数据为false')); return $return_json; } $data_arr = SimpleXML::simplexml4array($simple_xml_obj); if (!empty($data_arr['SearchRS'][0]['MatchList']) && isset($data_arr['SearchRS'][0]['MatchList'])) { $result_arr = $data_arr['SearchRS'][0]['MatchList']; if (!is_array($result_arr[0]['Match'])) { //判断是否有车辆 $return_json = json_encode(array('status' => false, 'error_msg' => '0x180099_返回数据为空或生成xml对象数据为false')); return $return_json; } } //对数据进行处理[S] $result_arr = self::filterArray($result_arr); //过滤中间层 //下载rentalcars图片到本地 /* ini_set('display_errors', 'On');// 显示所有错误 error_reporting(E_ALL);*/ foreach ($result_arr as &$match) { if (isset($match['Match'])) { //处理车辆图片[S] $oldImageURL = $match['Match']['Vehicle']['ImageURL']['@text']; $parse_oldImageURL = parse_url($oldImageURL); $newImageFile = CARRENTAL_IMAGES_PATH . $parse_oldImageURL['path']; if (!file_exists($newImageFile)) { $match['Match']['Vehicle']['ImageURL']['@text'] = HTTP_URL . GetUrlContent::GrabImage($oldImageURL, $newImageFile); } else { $match['Match']['Vehicle']['ImageURL']['@text'] = HTTP_URL . $newImageFile; } $oldLargeImageURL = $match['Match']['Vehicle']['LargeImageURL']['@text']; $parse_oldLargeImageURL = parse_url($oldLargeImageURL); $newLargeImageFile = CARRENTAL_IMAGES_PATH . $parse_oldLargeImageURL['path']; if (!file_exists($newLargeImageFile)) { $match['Match']['Vehicle']['LargeImageURL']['@text'] = HTTP_URL . GetUrlContent::GrabImage($oldLargeImageURL, $newLargeImageFile); } else { $match['Match']['Vehicle']['LargeImageURL']['@text'] = HTTP_URL . $newLargeImageFile; } //处理车辆图片[E] //处理车辆公司logo[S] $oldSmall_logo = $match['Match']['Supplier']['@attributes']['small_logo']; $parse_oldSmall_logo = parse_url($oldSmall_logo); $newSmall_logo = CARRENTAL_IMAGES_PATH . $parse_oldSmall_logo['path']; if (!file_exists($newSmall_logo)) { $match['Match']['Supplier']['@attributes']['small_logo'] = HTTP_URL . GetUrlContent::GrabImage($oldSmall_logo, $newSmall_logo); } else { $match['Match']['Supplier']['@attributes']['small_logo'] = HTTP_URL . $newSmall_logo; } //处理车辆公司logo[E] } } //对数据进行处理[E] $result = json_encode($result_arr); if (!empty($result)) { $return_json = json_encode(array('status' => true, 'data' => $result_arr)); ApiLogManage::addRentalcars('SearchRQ', $json_params, $searchRQ, $return_json, $request_time, time(), 1); //记录日志 return $return_json; } else { $return_json = json_encode(array('status' => false, 'error_msg' => '0x180099_请求超时')); ApiLogManage::addRentalcars('SearchRQ', $json_params, $searchRQ, $return_json, $request_time, time(), 10); //记录日志 return $return_json; //请求超时 } } else { $return_json = json_encode(array('status' => false, 'error_msg' => '0x180001_JSON参数格式错误')); return $return_json; } }
public function actionGetImg() { $imgPath = 'http://xml.rentalcars.com:80/images/car_images/newsite/D/fiat_500l.jpg'; $newFile = CARRENTAL_IMAGES_PATH . parse_url($imgPath)['path']; echo GetUrlContent::GrabImage($imgPath, $newFile); }
/** * 18查询租车信息 * @return String * 错误代码:[1.JSON参数格式错误 / 2.PickUp或DropOff或DriverAge 的参数名错误 / 3.DriverAge的参数错误 / 4.PickUp或DropOff下 缺少Location或Date参数 / 5.PickUp. Location 下的参数名错误 * 6.DropOff. Location 下的参数名错误 / 7.PickUp. Location 下缺少必填参数 / 8.单独使用了PickUp.Location 下的country或city / 9.DropOff. Location 下缺少必填参数 / * 10.单独使用了DropOff.Location 下的country或city / 11.PickUp.Date下缺少必填参数 / 12.DropOff.Date 下缺少必填参数 / 13.PickUp.Date或DropOff.Date不是正确的时间 / 14.DropOff.Date 小于了 PickUp.Date] */ public function actionSearchRQ() { $request_time = time(); $json_params = Yii::app()->request->getParam('json_params'); //客户端请求的json参数 // $json_params = '{"PickUp":{"Location":{"id":"1124991"},"Date":{"year":"2014","month":"12","day":"12","hour":"12","minute":"30"}},"DropOff":{"Location":{"id":"1124991"},"Date":{"year":"2014","month":"12","day":"30","hour":"12","minute":"30"}},"DriverAge":"32"}'; // 验证JSON串的正确性 if (!empty($json_params) && IconvEncode::IsJson($json_params)) { // 解析Json $request_param = json_decode($json_params, true); // 验证Json串中每一组参数的正确性 [S] // 验证PickUp、DropOff、DriverAge是否存在 if (empty($request_param['PickUp']) || empty($request_param['DropOff']) || empty($request_param['DriverAge'])) { $return_json = json_encode(array('status' => false, 'error_msg' => '2_PickUp、DropOff、DriverAge不可为空')); exit($return_json); } // DriverAge只能是大于0小于100的数字 if (!is_numeric($request_param['DriverAge']) || $request_param['DriverAge'] <= 18 || $request_param['DriverAge'] >= 75) { $return_json = json_encode(array('status' => false, 'error_msg' => '3_DriverAge只能是大于18小于75的数字')); exit($return_json); } // 声明简短变量 $PickUp_Location = empty($request_param['PickUp']['Location']) ? '' : $request_param['PickUp']['Location']; // PickUp.Location $DropOff_Location = empty($request_param['DropOff']['Location']) ? '' : $request_param['DropOff']['Location']; // DropOff.Location $PickUp_Date = empty($request_param['PickUp']['Date']) ? '' : $request_param['PickUp']['Date']; // PickUp.Date $DropOff_Date = empty($request_param['DropOff']['Date']) ? '' : $request_param['DropOff']['Date']; // DropOff.Date // 验证PickUp与DropOff下的 Location与Date是否存在 if (empty($PickUp_Location) || empty($PickUp_Date) || empty($DropOff_Location) || empty($DropOff_Date)) { $return_json = json_encode(array('status' => false, 'error_msg' => '4_PickUp与DropOff下的 Location与Date是否存在')); exit($return_json); } // Location下正确的参数名 $Location_Key = array('id', 'country', 'city'); // 验证PickUp.Location 中的参数名是否正确 foreach ($PickUp_Location as $key => $val) { if (!in_array($key, $Location_Key)) { $return_json = json_encode(array('status' => false, 'error_msg' => '5_PickUp. Location 下的参数名错误')); exit($return_json); } } // 验证DropOff.Location 中的参数名是否正确 foreach ($DropOff_Location as $key => $val) { if (!in_array($key, $Location_Key)) { $return_json = json_encode(array('status' => false, 'error_msg' => '6_DropOff. Location 下的参数名错误')); exit($return_json); } } // 验证PickUp.Location 中是否为填写任何条件 [id / country / city] if (count($PickUp_Location) == 0) { $return_json = json_encode(array('status' => false, 'error_msg' => '7_PickUp. Location 下缺少必填参数')); exit($return_json); } else { // 如按国家+城市的方式查询 if (empty($PickUp_Location['id'])) { // country与city不能单独使用 如填写了country或city的其中一个 另一个也必须填写 if (empty($PickUp_Location['country']) && !empty($PickUp_Location['city']) || !empty($PickUp_Location['country']) && empty($PickUp_Location['city'])) { $return_json = json_encode(array('status' => false, 'error_msg' => '8_单独使用了PickUp.Location 下的country或city')); exit($return_json); } } } // 验证DropOff.Location 中是否为填写任何条件 [id / country / city] if (count($DropOff_Location) == 0) { $return_json = json_encode(array('status' => false, 'error_msg' => '9_DropOff. Location 下缺少必填参数')); exit($return_json); } else { // 如按国家+城市的方式查询 if (empty($DropOff_Location['id'])) { // country与city不能单独使用 如填写了country或city的其中一个 另一个也必须填写 if (empty($DropOff_Location['country']) && !empty($DropOff_Location['city']) || !empty($DropOff_Location['country']) && empty($DropOff_Location['city'])) { $return_json = json_encode(array('status' => false, 'error_msg' => '10_单独使用了DropOff.Location 下的country或city')); exit($return_json); } } } // PickUp.Date 中的参数均为必填项 if (empty($PickUp_Date['year']) || empty($PickUp_Date['month']) || empty($PickUp_Date['day']) || empty($PickUp_Date['hour']) || empty($PickUp_Date['minute'])) { $return_json = json_encode(array('status' => false, 'error_msg' => '11_PickUp.Date下缺少必填参数')); exit($return_json); } // DropOff.Date 中的参数均为必填项 if (empty($DropOff_Date['year']) || empty($DropOff_Date['month']) || empty($DropOff_Date['day']) || empty($DropOff_Date['hour']) || empty($DropOff_Date['minute'])) { $return_json = json_encode(array('status' => false, 'error_msg' => '12_DropOff.Date 下缺少必填参数')); exit($return_json); } $start_time = strtotime($PickUp_Date['year'] . '-' . $PickUp_Date['month'] . '-' . $PickUp_Date['day'] . ' ' . $PickUp_Date['hour'] . ':' . $DropOff_Date['minute']); $end_time = strtotime($DropOff_Date['year'] . '-' . $DropOff_Date['month'] . '-' . $DropOff_Date['day'] . ' ' . $DropOff_Date['hour'] . ':' . $DropOff_Date['minute']); // 验证时间范围是否正确 [例:2014-13-38 错误时间strtotime会返回false] if ($start_time && $end_time) { // 结束时间不能小于开始时间 if ($end_time < $start_time) { $return_json = json_encode(array('status' => false, 'error_msg' => '14_DropOff.Date 小于了PickUp.Date')); exit($return_json); } } else { $return_json = json_encode(array('status' => false, 'error_msg' => '13_PickUp.Date或DropOff.Date不是正确的时间')); exit($return_json); } // 验证Json串中每一组参数的正确性 [E] // 判断根据ID查询还是根据国家地区查询 $PickUp_Location = empty($request_param['PickUp']['Location']['id']) ? 'country="' . $request_param['PickUp']['Location']['country'] . '" city="' . $request_param['PickUp']['Location']['city'] . '"' : 'id="' . $request_param['PickUp']['Location']['id'] . '"'; $DropOff_Location = empty($request_param['DropOff']['Location']['id']) ? 'country="' . $request_param['DropOff']['Location']['country'] . '" city="' . $request_param['DropOff']['Location']['city'] . '"' : 'id="' . $request_param['DropOff']['Location']['id'] . '"'; // 拼接XML $request_xml = ' <SearchRQ> <Credentials username="******" password="******" remoteIp="' . CARRENTALAPI_REMOTEIP . '" /> <PickUp> <Location ' . $PickUp_Location . '/> <Date year="' . $request_param['PickUp']['Date']['year'] . '" month="' . $request_param['PickUp']['Date']['month'] . '" day="' . $request_param['PickUp']['Date']['day'] . '" hour="' . $request_param['PickUp']['Date']['hour'] . '" minute="' . $request_param['PickUp']['Date']['minute'] . '"/> </PickUp> <DropOff> <Location ' . $DropOff_Location . '/> <Date year="' . $request_param['DropOff']['Date']['year'] . '" month="' . $request_param['DropOff']['Date']['month'] . '" day="' . $request_param['DropOff']['Date']['day'] . '" hour="' . $request_param['DropOff']['Date']['hour'] . '" minute="' . $request_param['DropOff']['Date']['minute'] . '"/> </DropOff> <DriverAge>' . $request_param['DriverAge'] . '</DriverAge> <Tracking adcamp="example" adplat="example"/> </SearchRQ>'; // 调用接口获取数据 $request_data = array('xml' => $request_xml); $searchRQ = GetUrlContent::fileGetContent($request_data, CARRENTALAPI_URL, 30, 'POST'); $simple_xml_obj = simplexml_load_string($searchRQ); $data_arr = SimpleXML::simplexml4array($simple_xml_obj); if (!empty($data_arr['SearchRS'][0]['MatchList']) && isset($data_arr['SearchRS'][0]['MatchList'])) { $result_arr = $data_arr['SearchRS'][0]['MatchList']; } $result = json_encode($result_arr); if (!empty($result)) { $return_json = json_encode(array('status' => true, 'data' => $result_arr)); ApiLogManage::addRentalcars('SearchRQ', $json_params, $searchRQ, $return_json, $request_time, time(), 1); //记录日志 echo $return_json; exit; } else { $return_json = json_encode(array('status' => false, 'error_msg' => '5_请求超时')); ApiLogManage::addRentalcars('SearchRQ', $json_params, $searchRQ, $return_json, $request_time, time(), 10); //记录日志 exit($return_json); //请求超时 } } else { $return_json = json_encode(array('status' => false, 'error_msg' => '1_JSON参数格式错误')); exit($return_json); } }