Skip to content

Instantly share code, notes, and snippets.

@swuppio
Created December 23, 2019 18:46
Show Gist options
  • Save swuppio/4789faf17012e625582123c1f4e5351b to your computer and use it in GitHub Desktop.
Save swuppio/4789faf17012e625582123c1f4e5351b to your computer and use it in GitHub Desktop.
<?php
namespace app\commands;
use Yii;
use app\models\Cur;
class HelloController extends \yii\console\Controller
{
//2019-12-23
public function actionLoad($date)
{
$res = Cur::find()->where(['date' => $date])->all();
if (!empty($res)) {
echo "Эта дата уже добавлена";
exit;
}
$cbr_date = date('d/m/Y', strtotime($date));
$sxe = new \SimpleXMLElement("http://www.cbr.ru/scripts/XML_daily.asp?date_req={$cbr_date}", NULL, TRUE);
foreach ($sxe->Valute as $value) {
$cur = new Cur;
$cur->code = $value->CharCode;
$cur->value = (double) str_replace(',', '.', $value->Value);
$cur->name = $value->Name;
$cur->unit = $value->Nominal;
$cur->date = $date;
$cur->save(false);
echo "{$cur->name} Сохранено".PHP_EOL;
}
}
public function actionGet($date)
{
$res = Cur::find()->where(['date' => $date])->all();
echo "Date: {$date}".PHP_EOL.PHP_EOL;
foreach ($res as $cur) {
echo "{$cur->name}: {$cur->value} рублей за {$cur->unit} {$cur->code}".PHP_EOL;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment