PHP- Kodu:
namespace ProjectControllers;
Use Db, DBForge, Curl, Date, SimpleXMLElement;
class Currency extends Controller
{
public function update()
{
$today = Date::set('Y-m-d');
$currency_codes = DB::select('code')->where( 'id NOT IN ', DB::notIn(101,949) )->get('currency')->result();
foreach ($currency_codes as $currency_code) $xpath_expressions[] = sprintf("//Currency[@CurrencyCode='%s']", $currency_code->code);
$xpath = join('|', $xpath_expressions);
$contents = Curl::init()
->option('url', 'http://www.tcmb.gov.tr/kurlar/today.xml')
->option('header', 0)
->option('returntransfer', 1)
->option('timeout', 10)
->exec();
try {
$currency_xml = new SimpleXMLElement($contents);
$currency_edit_date = preg_replace('/(d{2})/(d{2})/(d{4})/', '$3-$1-$2', (string)$currency_xml['Date']);
$currency_nodes = $currency_xml->xpath($xpath);
$exchange_rates = array();
foreach ($currency_nodes as $currency) {
$currency_code = (string) $currency['CurrencyCode'];
$exchange_rates[$currency_code]['buying'] = (string)$currency->ForexBuying;
$exchange_rates[$currency_code]['selling'] = (string)$currency->ForexSelling;
$exchange_rates[$currency_code]['edit_date'] = $currency_edit_date;
}
foreach ($exchange_rates as $currency_code => $exchange_rate) {
DB::where('code', $currency_code)
->column('buying_rate', $exchange_rate['buying'])
->column('selling_rate', $exchange_rate['selling'])
->column('edit_date', $exchange_rate['edit_date'])
->column('update_date', $today)
->update('currency');
}
output('KURLAR GÜNCELLENDİ');
} catch (Exception $e) {
output('HATA');
output($e);
}
exit();
}
public function get()
{
$currency_rates = DB::where('int:is_active', 1)->where('int:id !=', 949)->get('currency')->result();
output($currency_rates);
exit();
}
public function install()
{
DBForge::dropTable('currency');
DBForge::createTable('currency',[
'id' => [DB::SMALLINT(5), 'UNSIGNED NOT NULL DEFAULT 0', DB::primaryKey()],
'code' => [DB::VARCHAR(3), DB::UNIQUE()],
'symbol' => [DB::VARCHAR(10)],
'name' => [DB::VARCHAR(50)],
'buying_rate' => [DB::DOUBLE(), 'UNSIGNED NOT NULL DEFAULT 0'],
'selling_rate' => [DB::DOUBLE(), 'UNSIGNED NOT NULL DEFAULT 0'],
'is_active' => [DB::TINYINT(1), 'DEFAULT 1'],
'edit_date' => [DB::DATETIME(), 'DEFAULT CURRENT_TIMESTAMP'],
'update_date' => [DB::DATETIME(), 'DEFAULT CURRENT_TIMESTAMP'],
]);
DB::insert('currency',['id' => 826, 'code' => 'GBP', 'symbol' => '£', 'name' => 'İngiliz Sterlini']);
DB::insert('currency',['id' => 840, 'code' => 'USD', 'symbol' => '$', 'name' => 'ABD Doları']);
DB::insert('currency',['id' => 949, 'code' => 'TRY', 'symbol' => 'TL', 'buying_rate' => 1, 'selling_rate' => 1, 'name' => 'Türk Lirası']);
DB::insert('currency',['id' => 978, 'code' => 'EUR', 'symbol' => '€', 'name' => 'Euro']);
}
}