PHP be Mysql ile Merkez Bankasından günlük kurları almak için ufak bir script. PHP5 gerekiyor tabii ki.
Kullanımı:
$TCMB = new TCMB("http://www.tcmb.gov.tr/kurlar/today.xml"); $TCMB->updateCurrecy(); |
İlk olarak tablomuz:
CREATE TABLE IF NOT EXISTS `kurlar` ( `kur_no` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, `kur_isim` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL, `kur_kod` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL, `kur_alis` DECIMAL(6,5) UNSIGNED NOT NULL, `kur_satis` DECIMAL(6,5) UNSIGNED NOT NULL, `kur_tarih` datetime NOT NULL, PRIMARY KEY (`kur_no`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; |
Son Olarak php scriptimiz:
< ?php class TCMB { private $buying; private $selling; private $currency_name; private $currency_code; private $date; private $TCMBxml; function __construct($url) { $this->setTCMBxml($url); } function __destruct() { } public function updateCurrecy() { $xmlContents = simplexml_load_file($this->getTCMBxml(), 'SimpleXMLElement', LIBXML_NOCDATA); $this->setDate(strtotime($xmlContents['Tarih'][0])); foreach($xmlContents as $object => $value) { $this->setBuying($value->ForexBuying); $this->setSelling($value->ForexSelling); $this->setCurrencyCode($value['Kod'][0]); $this->setCurrencyName($value->Isim); $this->insertDate($this->getCurrencyName(), $this->getCurrencyCode(), $this->getBuying(), $this->getSelling(), date("Y-m-d H:i:s",$this->getDate())); } } private function insertDate($kur_isim, $kur_kod, $kur_alis, $kur_satis, $kur_tarih) { $retVal = FALSE; $sql = "INSERT INTO kurlar(kur_isim,kur_kod,kur_alis,kur_satis,kur_tarih) VALUES('".mysql_real_escape_string($kur_isim)."','".mysql_real_escape_string($kur_kod)."','".mysql_real_escape_string($kur_alis)."','".mysql_real_escape_string($kur_satis)."','".mysql_real_escape_string($kur_tarih)."')"; $query = mysql_query($sql, $GLOBALS['baglanti']); if(mysql_affected_rows() == 1) { $retVal = TRUE; } return $retVal; } function setTCMBxml($url) { $this->TCMBxml = $url; } function getTCMBxml() { return $this->TCMBxml; } function setDate($input) { $this->date = $input; } function getDate() { return $this->date; } function setSelling($input) { $this->selling = $input; } function getSelling() { return $this->selling; } function setBuying($input) { $this->buying = $input; } function getBuying() { return $this->buying; } function setCurrencyName($input) { $this->currency_name = $input; } function getCurrencyName() { return $this->currency_name; } function setCurrencyCode($input) { $this->currency_code = $input; } function getCurrencyCode() { return $this->currency_code; } } ?> |