Answer the question
In order to leave comments, you need to log in
How to set up caching of information about the current exchange rate from the cbr site?
Hello.
There was such a situation that the cbr site blocks frequent requests to their resource, so there was a need to cache the results (every 2-3 hours).
Actually, the code itself:
<?php
$date = date("d/m/Y");
$link = "http://www.cbr.ru/scripts/XML_daily.asp?date_req=$date";
$content = file_get_contents($link);
$dom = new domDocument("1.0", "cp1251");
$dom->loadXML($content);
$root = $dom->documentElement;
$childs = $root->childNodes;
$data = array();
for ($i = 0; $i < $childs->length; $i++) {
$childs_new = $childs->item($i)->childNodes;
for ($j = 0; $j < $childs_new->length; $j++) {
$el = $childs_new->item($j);
$code = $el->nodeValue;
if ($code == "EUR") $data[] = $childs_new;
}
}
for ($i = 0; $i < count($data); $i++) {
$list = $data[$i];
for ($j = 0; $j < $list->length; $j++) {
$el = $list->item($j);
if ($el->nodeName == "Name");
elseif ($el->nodeName == "Value") echo $el->nodeValue."";
}
}
?>
Answer the question
In order to leave comments, you need to log in
Why don’t you like the convenient and free API of the same exchange rate from the largest player in the Russian Federation in this area - RosBusinessConsulting
the easiest option is to create a file, in the first line indicate the time when it becomes irrelevant, contact the bank, take the courses, write them down.
The next time you check the file, check the relevance of the time in it, if the time is up, access the site and overwrite the file. If the time is not up, then take the information from it.
Something like this, in general, you can find some kind of lightweight cacher library and use it
Something similar is implemented - using the set_transient() and get_transient() functions
Make sure Memcached is connected (or connect)
AND in the code
$m = new Memcached();
$m->addServer('localhost', 11211);
if(($data = $m->get('someKey')) === false) {
// do some actions do get data from remote source
// place your code here, store result in $data variable
$m->set('someKey', $data, 60*60*2);
}
return $data;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question