Created
December 23, 2019 18:46
-
-
Save swuppio/4789faf17012e625582123c1f4e5351b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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