Файл: billing/_rootinc/privatapi.inc.php
Строк: 64
<?
class PRIVATAPI
{
public $controller = true;
public $error = null;
public function startup(&$controller)
{
$this->controller =& $controller;
}
public function send($api)
{
$fp = curl_init();
curl_setopt($fp, CURLOPT_URL, $api);
curl_setopt($fp, CURL_HTTP_VERSION_1_1, 1);
curl_setopt($fp, CURLOPT_POST, 0);
curl_setopt($fp, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($fp, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($fp, CURLOPT_RETURNTRANSFER,1);
curl_setopt($fp, CURLOPT_TIMEOUT, 120);
$result = curl_exec($fp);
curl_close($fp);
return $result;
}
public function getCourses()
{
$result = $this->send("https://privat24.privatbank.ua/p24/accountorder?oper=prp&exchange&PUREXML&coursid=4");
$result = $this->parseCoursesResult($result);
if ($result[USD][buy]) {
return $result;
} else {
$this->error = "getCourses: can't get courses";
return false;
}
}
public function getCoursesOfficial($type)
{
if ($type == "nbu") { $result = $this->send("https://privat24.privatbank.ua/p24/accountorder?oper=prp&PUREXML&apicour&country=ua&full"); }
if ($type == "cbrf") { $result = $this->send("https://privat24.privatbank.ua/p24/accountorder?oper=prp&PUREXML&apicour&country=ru&full"); }
$result = $this->parseCoursesOfficialResult($result,$type);
if ($result[USD]) {
return $result;
} else {
$this->error = "getCoursesOfficial: can't get courses";
return false;
}
}
public function parseCoursesResult($input)
{
$input = preg_replace("/r/ui","",$input);
$input = preg_replace("/n/ui","",$input);
preg_match_all("/<exchangerate ccy="([a-z]+)" base_ccy="([a-z]+)" buy="([0-9.]+)" sale="([0-9.]+)"/>/iu",$input,$arr);
while (list($i,$v) = @each($arr[0])) {
$val = $arr[1][$i];
if ($val == "RUR") { $val = "RUB"; }
$buy = $arr[3][$i];
$sale = $arr[4][$i];
$result[$val][buy] = $buy;
$result[$val][sale] = $sale;
}
$result[UAH][buy] = 1;
$result[UAH][sale] = 1;
return $result;
}
public function parseCoursesOfficialResult($input,$type)
{
$input = preg_replace("/r/ui","",$input);
$input = preg_replace("/n/ui","",$input);
preg_match_all("/<exchangerate ccy="([a-z]+)" ccy_name_ru="[^"]+" ccy_name_ua="[^"]+" ccy_name_en="[^"]+" base_ccy="[^"]+" buy="([0-9.]+)" unit="([0-9.]+)" date="([0-9.]+)" />/iu",$input,$arr);
while (list($i,$v) = @each($arr[1])) {
$val = $arr[1][$i];
if ($val == "RUR") { $val = "RUB"; }
$result[$val] = $arr[2][$i]/$arr[3][$i]/10000;
}
if ($type == "nbu") {
$result[UAH] = 1;
}
if ($type == "cbrf") {
$result[RUB] = 1;
}
return $result;
}
}
?>