/** * 駅周辺のイベント情報 * @param sName string 駅名 * @return xml */ public function get_eventInfo() { //Load Config $config = \Config::load('api', 'api'); $memGetKey = $config['data-type'][4]; //sName = Station Name $sName = Input::get('sName', null); if (isset($sName) === false) { return false; } //Memcacheから駅情報取得 $mem = new \Mem(); $sData = $mem->memRead($memGetKey . ':' . $sName); if (is_array($sData) === false) { return false; } //駅近辺にイベント情報がキャッシュになければ取得しにいく $memSaveKey = $sName . ':' . $config['event']['art']['data-type']; if (!($eventData = $mem->memRead($memSaveKey))) { //lat = 緯度 long = 経度 $url = $config['event']['art']['url'] . '?Latitude=' . $sData['stationInfo'][0]['geo:lat'] . '&Longitude=' . $sData['stationInfo'][0]['geo:long'] . '&=' . $sData['stationInfo'][0]['geo:long'] . '&MaxResults=50&SortOrder=distance&free=0&Language=ja'; $curl = \Request::forge($url, 'curl'); $curl->set_method('get'); $response = $curl->execute(); $result = $curl->response(); if ($result->status == 200) { //xml形式 $eventData = $result->body; } else { return false; } $mem->memSave($memSaveKey, $eventData, $life_time = 3600); } //$eventData = simplexml_load_string($eventData); //$eventData = json_encode($eventData); // $eventData = str_replace('[', '', $eventData); // $eventData = str_replace(']', '', $eventData); return $this->response($eventData); }
public static function run($getDataType) { //Load Config $config = \Config::load('api', 'api'); $dataType = $config['data-type']; if ($getDataType == 1) { //日比谷線のロケーション情報 //rdf:type=odpt:Train 90秒更新 $url = $config['url'] . '?rdf:type=odpt:Train&odpt:railway=odpt.Railway:TokyoMetro.Hibiya&acl:consumerKey=' . $config['token']; $setKeyName = $dataType[1]; } else { if ($getDataType == 2) { //日比谷線の運行情報 //rdf:type=odpt:TrainInformation: 問題なく動いているかどうかを返す $url = $config['url'] . '?rdf:type=odpt:TrainInformation&odpt:railway=odpt.Railway:TokyoMetro.Hibiya&acl:consumerKey=' . $config['token']; $setKeyName = $dataType[2]; } else { if ($getDataType == 3) { //全駅情報(乗降人員数) //odpt:PassengerSurvey //フィルタかけるなら $@id=urn:ucode:ほにゃららかな $url = $config['url'] . '?rdf:type=odpt:PassengerSurvey&acl:consumerKey=' . $config['token']; $setKeyName = $dataType[3]; } else { if ($getDataType == 4) { //路線情報 $url = $config['url'] . '?rdf:type=odpt:Railway&dc:title=日比谷&acl:consumerKey=' . $config['token']; $setKeyName = $dataType[4]; } else { if ($getDataType == 5) { //路線情報 $url = $config['url'] . '?rdf:type=odpt:Train&acl:consumerKey=' . $config['token']; $setKeyName = $dataType[5]; } else { if ($getDataType == 6) { //路線情報 $url = $config['url'] . '?rdf:type=odpt:Railway&acl:consumerKey=' . $config['token']; $setKeyName = $dataType[4]; $setKeyNameLineAll = $dataType[7]; } else { \Session::set_flash('error', '電車情報がありません'); \Response::redirect('top/index'); } } } } } } //Connect API $curl = \Request::forge($url, 'curl'); $curl->set_method('get'); $response = $curl->execute(); $result = $curl->response(); //Write if ($result->status == 200) { $trainKey = 'odpt:trainNumber'; $data = json_decode($result->body, true); $mem = new \Mem(); if ($getDataType == 1) { foreach ($data as $key => $value) { //開発用 //$value[$trainKey] = 'A0000'; $r = $mem->memSave($setKeyName . ':' . $value[$trainKey], $value, $life_time = 3600); $all[] = $value; } $r = $mem->memSave($setKeyName, $all, $life_time = 3600); } else { if ($getDataType == 4 || $getDataType == 6) { foreach ($data as $value) { $memData = array(); $lineKey = strrchr($value['owl:sameAs'], 'TokyoMetro'); if ($lineKey == 'Tozai') { $lineKey = 'TokyoMetro.Tozai'; } else { if ($lineKey == 'Tsukiji') { $lineKey = 'TokyoMetro.Tsukiji'; } } //日本語の路線名 $memData['title'] = $value['dc:title']; foreach ($value['odpt:stationOrder'] as $key2 => $value2) { $stationIndex = $value2['odpt:index']; $memData[$stationIndex]['name'] = strrchr($value2['odpt:station'], 'TokyoMetro'); if ($memData[$stationIndex]['name'] == 'Tsukiji') { $memData[$stationIndex]['name'] = 'TokyoMetro.Tsukiji'; } //Connect API & GET INFO ABOUT STATITON $url = $config['url'] . '/' . $value2['odpt:station'] . '?acl:consumerKey=' . $config['token']; $curlStation = \Request::forge($url, 'curl'); $curlStation->set_method('get'); $resStationData = $curlStation->execute(); $stationData = $curlStation->response(); $memData[$stationIndex]['stationInfo'] = json_decode($stationData->body, true); sleep(10); //駅情報のみにキャッシュ化 $mem->memSave($setKeyName . ':' . $memData[$stationIndex]['name'], $memData[$stationIndex], $life_time = 2592000); } //目的駅までの所用時間を整形 foreach ($value['odpt:travelTime'] as $key3 => $value3) { if (isset($memData[$key3]) == true) { $memData[$key3]['travelTime'] = $value3['odpt:necessaryTime']; $memData[$key3]['toStation'] = strrchr($value3['odpt:toStation'], 'TokyoMetro'); $memData[$key3]['trainType'] = strrchr($value3['odpt:trainType'], 'TrainType'); } } //路線名のみを格納 if ($getDataType == 6) { $lineAll[] = $lineKey; } //路線情報ごとにキャッシュ化 $r = $mem->memSave($setKeyName . ':' . $lineKey, $memData, $life_time = 2592000); } //路線名のみを格納 if ($getDataType == 6) { $r = $mem->memSave($setKeyNameLineAll, $lineAll, $life_time = 2592000); } } else { $r = $mem->memSave($setKeyName, $data, $life_time = 3600); } } } if ($r) { echo 'success データ保存完了'; } else { echo 'error データ保存失敗'; } }