stations!=null){ $stationsArray = $cache -> stations; if(count($stationsArray)>0){ $stationKey = isset($stationsArray[$stationID]) ? $stationsArray[$stationID] : null; if($stationKey===false||$stationKey===null){ $station_name = GetStationByID($stationID,$userKey); $station_name_conv = iconv("WINDOWS-1251","UTF-8",$station_name); if($station_name_conv<>"???") { $cache ->stations[$stationID] = $station_name_conv; } //echo 'Station cache - no station'; }else{ $station_name = iconv("UTF-8","WINDOWS-1251",$stationsArray[$stationID]); }; }else{ $cacheFailed = true; //echo 'Station cache empty'; }; }else{ $cacheFailed = true; //echo 'Station cache - cahce not exist'; }; if($cacheFailed == true){ $station_name = GetStationByID($stationID,$userKey); $station_name_conv = iconv("WINDOWS-1251","UTF-8",$station_name); if($station_name_conv<>"???") { $cache->stations = array($stationID=>$station_name_conv); } } return $station_name; } function GetStationByID($stationID,$userKey) { $json_url_station ='https://catalog.api.2gis.com/3.0/items/byid?id='.$stationID.'&key='.$userKey; $station_ch = curl_init( $json_url_station); $station_options = array( CURLOPT_SSL_VERIFYPEER => false, CURLOPT_RETURNTRANSFER => true, curl_setopt($station_ch, CURLOPT_ENCODING ,"UTF-8"), CURLOPT_HTTPHEADER => array( "Accept: application/json" ) ); curl_setopt_array($station_ch, $station_options); $stationJSON = curl_exec($station_ch); if($GLOBALS['logging']){ file_put_contents($GLOBALS['logfile'], time()." API request - station name".PHP_EOL,FILE_APPEND); } $GLOBALS['totalRequests']++; $station_description = json_decode($stationJSON); if(property_exists($station_description,'result')){ $station_name = iconv("UTF-8","WINDOWS-1251",$station_description->result->items[0]->full_name); }else{ $station_name = "???"; }; return $station_name; } function GetRouteName($routeID,&$cache,$userKey) { if(property_exists($cache,"routes")& $cache -> routes!=null){ $routesArray = $cache -> routes; if(count($routesArray)>0) { $routeKey = isset($routesArray[$routeID]) ? $routesArray[$routeID] : null; if($routeKey===false||$routeKey===null){ $routeName = GetRouteByID($routeID,$userKey,'route'); $routeName_conv = iconv("WINDOWS-1251","UTF-8",$routeName); if($routeName_conv<>'-'){ $cache ->routes[$routeID] = $routeName_conv; } //echo 'Route cash - no route in cache'; } else{ $routeName = iconv("UTF-8","WINDOWS-1251",$routesArray[$routeID]); if($GLOBALS['logging']){ file_put_contents($GLOBALS['logfile'], " - Route name from cache: ".$routeName.PHP_EOL,FILE_APPEND); } }; }else{ $routeName = GetRouteByID($routeID,$userKey,'route'); $routeName_conv = iconv("WINDOWS-1251","UTF-8",$routeName); if($routeName_conv<>'-'){ $cache->routes = array($routeID=>$routeName_conv); } //echo 'Route cash - cache empty'; }; }else{ $routeName = GetRouteByID($routeID,$userKey,'route'); $routeName_conv = iconv("WINDOWS-1251","UTF-8",$routeName); if($routeName_conv<>'-'){ $cache->routes = array($routeID=>$routeName_conv); } //echo 'Route cache - cache not exist'; }; return $routeName; } function GetRouteByID($routeID,$userKey,$field) { $json_url_route ='https://catalog.api.2gis.com/3.0/items/byid?id='.$routeID.'&key='.$userKey; $route_ch = curl_init( $json_url_route); $route_options = array( CURLOPT_SSL_VERIFYPEER => false, CURLOPT_RETURNTRANSFER => true, curl_setopt($route_ch, CURLOPT_ENCODING ,"UTF-8"), CURLOPT_HTTPHEADER => array( "Accept: application/json" ) ); curl_setopt_array($route_ch, $route_options); $routeJSON = curl_exec($route_ch); if($GLOBALS['logging']){ file_put_contents($GLOBALS['logfile'], time()." API request - route type or name".PHP_EOL,FILE_APPEND); } $GLOBALS['totalRequests']++; $route_description = json_decode($routeJSON); if($field=='route'){ if(property_exists($route_description,'result')){ $route_name=iconv("UTF-8","WINDOWS-1251",$route_description->result->items[0]->name); }else{ $route_name = "-"; }; return $route_name; }else if($field=='type'){ if(property_exists($route_description,'result')){ //echo var_dump($route_description); $route_name=iconv("UTF-8","WINDOWS-1251",$route_description->result->items[0]->subtype); }else{ $route_name = "-"; }; return $route_name; } } function GetTransportType($routeID,&$cache,$userKey) { $cacheFailed = false; if(property_exists($cache,"types")& $cache -> types!=null){ $typesArray = $cache -> types; if(count($typesArray)>0){ $typeKey = isset($typesArray[$routeID]) ? $typesArray[$routeID] : null; if($typeKey===false||$typeKey===null){ $type = GetRouteByID($routeID,$userKey,'type'); if($type<>'-') { $cache ->types[$routeID] = $type; } //echo 'no record in cache'.$type; } else{ $type = $typesArray[$routeID]; //echo 'record from cache'; }; }else{ $cacheFailed = true; //echo 'no records in cache'; }; }else{ $cacheFailed = true; }; if($cacheFailed == true){ $type = GetRouteByID($routeID,$userKey,'type'); if($type<>'-'){ $cache->types = array($routeID=>$type); } } return $type; } function draw_header($stationName, $time){ echo "

$stationName

$time

"; } function draw_logo() { echo "
"; } function stationInfo() { $stationID = $_GET['SID']; $hourOffset = 0; $timeStamp = time()+$hourOffset * 60 * 60; $even=true; $period = 30; // максимальный интервал отображения прогноза $maxLines = 10; // максимальное количество строк в таблице $arriveInterval = 3; // интервал, когда отображается слово "Прибывает" $userKey = "76864e71-0499-4ad6-bda3-76cb2b001211"; $cachefile = 'cache/stationsOmsk'.$stationID.'.cache'; $cachefileRoutes = 'cache/routesOmsk'.$stationID.'.cache'; $cachefileTypes = 'cache/typesOmsk'.$stationID.'.cache'; $cachetime = 86400; // time to cache in seconds $cacheData = new cacheDataContent; if($GLOBALS['logging']){ file_put_contents($GLOBALS['logfile'], time()." Log for station begin - ".$stationID.PHP_EOL); } if (file_exists($cachefile) && time() - $cachetime <= filemtime($cachefile)) { $cacheDataJSON = json_decode(@file_get_contents($cachefile),true); $cacheData->stations = $cacheDataJSON; if($GLOBALS['logging']){ file_put_contents($GLOBALS['logfile'], "Stations cache: ".(time() - filemtime($cachefile)).PHP_EOL,FILE_APPEND); file_put_contents($GLOBALS['logfile'], "Stations cache contents: ".count((array)$cacheData->stations).PHP_EOL,FILE_APPEND); } }else if(file_exists($cachefile) && $GLOBALS['logging']){ file_put_contents($GLOBALS['logfile'], "Stations cache too old:".(time() - filemtime($cachefile)),FILE_APPEND); } if (file_exists($cachefileRoutes) && time() - $cachetime <= filemtime($cachefileRoutes)) { $cacheDataRoutesJSON = json_decode(@file_get_contents($cachefileRoutes),true); $cacheData->routes = $cacheDataRoutesJSON; if($GLOBALS['logging']){ file_put_contents($GLOBALS['logfile'], "Routes cache ".(time() - filemtime($cachefileRoutes)).PHP_EOL,FILE_APPEND); file_put_contents($GLOBALS['logfile'], "Routes cache contents: ".count((array)$cacheData->routes).PHP_EOL,FILE_APPEND); } }else if(file_exists($cachefileRoutes) && $GLOBALS['logging']){ file_put_contents($GLOBALS['logfile'], "Routes cache too old:".(time() - filemtime($cachefileRoutes)),FILE_APPEND); } if (file_exists($cachefileTypes) && time() - $cachetime <= filemtime($cachefileTypes)) { $cacheDataTypesJSON = json_decode(@file_get_contents($cachefileTypes),true); $cacheData->types = $cacheDataTypesJSON; if($GLOBALS['logging']){ file_put_contents($GLOBALS['logfile'], "Types cache ".(time() - filemtime($cachefileTypes)).PHP_EOL,FILE_APPEND); file_put_contents($GLOBALS['logfile'], "Types cache contents: ".count((array)$cacheData->types).PHP_EOL,FILE_APPEND); } }else if(file_exists($cachefileTypes) && $GLOBALS['logging']){ file_put_contents($GLOBALS['logfile'], "Types cache too old:".(time() - filemtime($cachefileTypes)),FILE_APPEND); } $station_name = GetStationName($stationID, $cacheData, $userKey); $json_url = "https://routing.api.2gis.com/search_schedule?key=".$userKey; $ch = curl_init( $json_url ); $requestsText =(object) ['requests' => array(array( 'type' => "full_day_platform", 'request' => array( 'departure_time' => $timeStamp, 'platforms' => array($stationID) ), 'project' => "omsk" ))]; $requestsJSON = json_encode($requestsText); $options = array( CURLOPT_SSL_VERIFYPEER => false, CURLOPT_RETURNTRANSFER => true, curl_setopt($ch, CURLOPT_ENCODING ,"UTF-8"), CURLOPT_POSTFIELDS => $requestsJSON, CURLOPT_HTTPHEADER => array( "Accept: application/json" ) ); curl_setopt_array($ch, $options); $resultJSON = curl_exec($ch); if($GLOBALS['logging']){ file_put_contents($GLOBALS['logfile'], time()." API request - main request".PHP_EOL,FILE_APPEND); } $GLOBALS['totalRequests']++; $result = json_decode($resultJSON); $current_time = date('H:i',$timeStamp); draw_header($station_name,$current_time); //draw_logo(); echo "
"; echo ""; echo "
"; //echo "
"; echo ""; $tt_list = $result->responses[0]->schedules; $cnt=0; if($GLOBALS['logging']){ file_put_contents($GLOBALS['logfile'], time()."Main request rowcount ".count((array) $tt_list).PHP_EOL,FILE_APPEND); } foreach($tt_list as $tt_row) { // проверка на то, что это - прогноз. Не прогнозы показывать не надо! if (!property_exists($tt_row->schedule, 'forecast') || $tt_row->schedule->forecast !== true) continue; if(property_exists($tt_row->schedule, 'precise_time')==false) continue; $precise_time = $tt_row->schedule->precise_time; $hr1 = substr($precise_time, 0,2)-$hourOffset; $min1 = substr($precise_time, -1*strpos($precise_time, ":")); $hr2 = substr($current_time, 0,2); $min2 = substr($current_time, -1*strpos($current_time, ":")); $time_diffHR = $hr2-$hr1; if($min2<$min1) { $time_diffMIN = $min1-$min2; }else{ $time_diffMIN = $min1+(60-$min2); } $diff = $time_diffMIN; if($diff>$period) {break;}// else {//echo 'time_diff='.$diff.'
';}; if($even){ echo ""; $even=false; }else{ echo ""; $even=true; } $routeID = $tt_row->route_id; $route_name = GetRouteName($routeID,$cacheData,$userKey); $type = GetTransportType($routeID,$cacheData,$userKey); //echo $type; echo ''; if($diff<$arriveInterval) { $arrival_time = 'Прибывает'; }else{ $arrival_time = sprintf('%02d',$time_diffMIN,2).' мин'; } echo ""; $cnt++; if($cnt>$maxLines) break; echo ""; } echo "
Маршрут Прибудет через
'; //echo var_dump($type); if($type == 'bus'){ echo ''; }else if($type == 'shuttle_bus'){ echo ''; }else if($type == 'trolleybus'){ echo ''; }else{ echo ''; } echo $route_name.'".$arrival_time; echo "
"; //echo "
echo "
";//"; draw_logo(); echo "
"; file_put_contents($cachefile, json_encode($cacheData->stations)); file_put_contents($cachefileRoutes, json_encode($cacheData->routes)); file_put_contents($cachefileTypes, json_encode($cacheData->types)); if($GLOBALS['logging']){ file_put_contents($GLOBALS['logfile'], time()." Log for station end - ".$stationID,FILE_APPEND); } if (file_exists($GLOBALS['logging']) && time() - $cachetime > filemtime($cachefile)){ unlink($GLOBALS['logfile']); } if($GLOBALS['loggingLevel2']){ file_put_contents($GLOBALS['logfile'], date('m/d/Y H:i:s', time())." - End of log for station ID - ".$stationID." Number of requests: ".$GLOBALS['totalRequests'].PHP_EOL,FILE_APPEND); } } if(file_exists("LOGS")==false) { mkdir("LOGS"); } stationInfo(); ?>