function CBR_XML_Daily_Ru() { $json_daily_file = $_SERVER['DOCUMENT_ROOT'].'/CBR_XML_daily.json'; if (!is_file($json_daily_file) || filemtime($json_daily_file) < time() - 3600) { // Стучимся в CBR $cbr_url = "http://www.cbr.ru/scripts/XML_daily.asp"; $cbr_valid = false; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $cbr_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $cbr_output = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); $json_daily_temp = simplexml_load_string($cbr_output); if($http_code === 200 AND $json_daily_temp){ $cbr_valid = true; } if($cbr_valid){ $valute = array(); foreach($json_daily_temp->Valute as $k => $v){ $ta = []; $ta["ID"] = (string) $v->attributes()->ID; $ta["NumCode"] = (string) $v->NumCode; $ta["CharCode"] = (string) $v->CharCode; $ta["Nominal"] = (string) $v->Nominal; $ta["Name"] = (string) $v->Name; $ta["Value"] = (float) str_replace(",",".", $v->Value); $valute[$ta["CharCode"]] = $ta; } file_put_contents($json_daily_file, json_encode(array(date("c") => $valute), JSON_UNESCAPED_UNICODE)); }else{ // Пробуем достучаться до альтернативы $json_daily = json_decode(file_get_contents('https://www.cbr-xml-daily.ru/daily_json.js'), true); file_put_contents($json_daily_file, " ".json_encode(array($json_daily['Timestamp'] => $json_daily['Valute']), JSON_UNESCAPED_UNICODE)); } } return json_decode(file_get_contents($json_daily_file), true); } $cbr_value = CBR_XML_Daily_Ru(); print_r($cbr_value);
Комментарии (1)
Не писать ответ