Skip to content

Instantly share code, notes, and snippets.

@eto4detak
Last active December 3, 2018 15:11
Show Gist options
  • Save eto4detak/922aa7860ff4f02957b467c0d3021c60 to your computer and use it in GitHub Desktop.
Save eto4detak/922aa7860ff4f02957b467c0d3021c60 to your computer and use it in GitHub Desktop.
php xml
<?php
$movies = new SimpleXMLElement(file_get_contents('http://www.cbr.ru/scripts/XML_daily.asp?date_req=03/12/2018'));
function get_xml_from_url($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$xmlstr = curl_exec($ch);
curl_close($ch);
return $xmlstr;
}
function divie_upadte_rate_euro($value = '')
{
// $homepage = json_decode(file_get_contents('https://www.cbr-xml-daily.ru/daily_json.js'));
// $euro_rate = floatval($homepage->Valute->EUR->Value);
// $xmlstr = file_get_contents('http://www.cbr.ru/scripts/XML_daily.asp?date_req=03/12/2018');
$xmlstr = get_xml_from_url('http://www.cbr.ru/scripts/XML_daily.asp?date_req=03/12/2018');
if(empty($xmlstr)) return;
$xml_element = new SimpleXMLElement($xmlstr);
foreach ($xml_element->Valute as $Valute) {
if ($Valute->CharCode == 'EUR') {
$price = floatval(str_replace(",", ".", $Valute->Value));
$euro_rate = $price;
break;
}
}
// $euro_rate = divie_get_rate_euro_from_xml();
if (!empty($euro_rate)) {
set_transient('time_divie_euro_rate', $euro_rate, 24 * HOUR_IN_SECONDS);
update_option('divie_euro_rate', $euro_rate);
}
echo '<pre class="aaa" style="display:">';
var_dump($xml_element);
echo '</pre>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment