Göktuğ ÖZTÜRK
26Şub/100

TCMB Döviz Kurları Sınıfı

Uzun zaman önce yazmış olduğum Php ile Döviz Kuru Script'ini, güncelliğini yitirdiğinden ve php.ini'ye eklenen allow_url_fopen parametresi yüzünden file ve file_get_contents fonksiyonları uzak sunuculardaki dosyalara ulaşamadığı için tekrardan yazma ihtiyacı duydum.

Aşağıda sınıfın bulunacağı doviz.php ve kullanım örneklerinin olduğu test.php dosyalarının içeriklerini bulabilirsiniz.

İyi günlerde kullanmanız dileğiyle.

doviz.php içeriği

<?php

/**
 * TCMB Döviz Kurları Sınıfı.
 *
 * TCMB sunucularındaki Döviz Kurlarını işler.
 * Daha sonraki kullanımlar için sunucu üzerinde cache_dosyası degiskeninde belirtilen isimle yedekler.
 * Günde bir defa yeni kur değerlerini sunucudan çeker.
 *
 * Geliştirici: Göktuğ ÖZTÜRK
 * E-Posta: goktug [nokta] ozturk [at] gmail [nokta] com
 * Web: http://www.goktugozturk.com.tr
 */
class DovizKuru
{
  var $cache_dosyasi = "doviz_kurlari";
  var $kurlar = array();
  var $aktif_kur = "USD";
  /**
   * Constructor
   *
   * @access protected
   * @param bool $CacheEnabled
   * @param int $ CacheTime
   * @return void
   */
  function DovizKuru( $CacheEnabled = true, $CacheTime = 60 )
  {
    $Data = array();
    if ( $CacheEnabled )
    {
      $CacheSuresi = time() - $CacheTime * 60;
      if ( file_exists( $this->cache_dosyasi . ".db" )
          AND is_readable( $this->cache_dosyasi . ".db" )
          AND filemtime( $this->cache_dosyasi . ".db" ) > $CacheSuresi )
      {
        $FileContent = $this->rFile( $this->cache_dosyasi . ".db" );
        if ( trim( $FileContent ) != "" )
          $this->kurlar = unserialize( base64_decode( $FileContent ) );
      }
    } 

    if ( !count( $Data ) )
    {
      $xmlalanlari = array( "Isim" => "Isim",
        "ForexBuying" => "Alis",
        "ForexSelling" => "Satis",
        "BanknoteBuying" => "EfektifAlis",
        "BanknoteSelling" => "EfektifSatis",
        );

      $FileContent = $this->rFile( "http://www.tcmb.gov.tr/kurlar/today.xml" );

      preg_match_all( '@]*>(.*)< \/Currency>@iU', $FileContent, $Matches, PREG_SET_ORDER );
      foreach( $Matches as $value )
      {
        if ( !in_array( $value[1], array( "USD", "EUR", "CAD", "DKK", "SEK", "CHF", "NOK", "JPY", "SAR", "KWD", "AUD", "GBP" ) ) )
          continue;

        preg_match_all( '@< ([a-z]+)>(.*)< \/\\1>@iU', $value[2], $SubMatches, PREG_SET_ORDER );
        foreach( $SubMatches as $value2 )
        {
          if ( !isset( $xmlalanlari[$value2[1]] ) ) continue;
          $this->kurlar[$value[1]][$xmlalanlari[$value2[1]]] = $value2[2];
        }
      } 

      if ( $CacheEnabled )
        $this->wFile( $this->cache_dosyasi . ".db", base64_encode( serialize( $this->kurlar ) ) );
    }
  } 

  /**
   * Aktif kuru belirler.
   *
   * Geçerli Kurlar:
   * USD, EUR, CAD, DKK, SEK, CHF, NOK, JPY, SAR, KWD, AUD, GBP
   *
   * @param string $Currency
   * @return string
   */
  function KurBelirle( $Currency )
  {
    $Currency = strtoupper( $Currency );
    if ( isset( $this->kurlar[$Currency] ) )
    {
      $this->aktif_kur = $Currency;
      return true;
    } 

    return false;
  } 

  /**
   * Aktif Kur için Alış değerini getirir.
   *
   * @param string $Currency
   * @return string
   */
  function AlisGetir( $Currency = null )
  {
    if ( is_null( $Currency ) )
      $Currency = $this->aktif_kur;
    else
      $Currency = strtoupper( $Currency );

    return $this->kurlar[$Currency]["Alis"];
  } 

  /**
   * Aktif Kur için Satış değerini getirir.
   *
   * @param string $Currency
   * @return string
   */
  function SatisGetir( $Currency = null )
  {
    if ( is_null( $Currency ) )
      $Currency = $this->aktif_kur;
    else
      $Currency = strtoupper( $Currency );

    return $this->kurlar[$Currency]["Satis"];
  } 

  /**
   * Belirtilen yoldaki dosya içeriğini getirir.
   *
   * @param string $FileName
   * @param string $Mode
   * @return string
   */
  function rFile( $FileName )
  {
    $Content = "";
    if ( !preg_match( '@^https?:@', $FileName )
        OR ini_get( 'allow_url_fopen' ) == '1' )
    {
      if ( function_exists( 'file_get_contents' ) )
      {
        $Content = file_get_contents( $FileName );
      }
      else
      {
        if ( !$Handle = fopen( $FileName, "r" ) )
          return "Dosya Açılamıyor ($FileName)";

        $Content = fread( $Handle, filesize( $FileName ) );
        fclose( $Handle );
      }
    }
    else if ( function_exists( 'curl_init' ) )
    {
      $ch = curl_init();
      curl_setopt( $ch, CURLOPT_URL, $FileName );
      curl_setopt( $ch, CURLOPT_HEADER, 0 );
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
      $Content = curl_exec( $ch );
      curl_close( $ch );
    }
    else
      return "";

    return $Content;
  } 

  /**
   * Verilen içeriği dosyaya yazar.
   *
   * @param string $FileName
   * @param string $Content
   * @param int $Chmod
   * @param string $Mode
   * @return bool
   */
  function wFile( $FileName, $Content )
  {
    if ( function_exists( 'file_put_contents' ) )
    {
      if ( !file_put_contents( $FileName, $Content ) )
        return false;
    }
    else
    {
      if ( !$Handle = @fopen( $FileName, "w" ) )
        return false;

      if ( @fwrite( $Handle, $Content ) === false )
        return false;

      @fclose( $Handle );
    }
    @chmod( $FileName, 0644 );
    return true;
  }
} 

?>

test.php dosyasının içeriği

<?php
include ( 'doviz.php' );
/**
 * DovizKuru Sınıfı
 *
 * Eğer önbellek dosyası kullanmak istemiyorsanız 1. paremetre olarak false verebilirsiniz.
 * Örnek: $TCMB = new DovizKuru( false );
 *
 * Varsayılan olarak önbellek süresi 60 dakikadır. Bunu değiştirmek için 2. paremetre olarak dakika cinsinden bir değer girebilirsiniz.
 * Örnekler:
 * $TCMB = new DovizKuru( true, 1440 ); // 1 günlük önbellek
 * $TCMB = new DovizKuru( true, 0 ); // Her çağrıldığında yenile
 */
$TCMB = new DovizKuru();
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="tr-TR">
<head>
 <title>Test</title>
</head>
<body>
<table style="border: 1px solid #999; border-collapse: collapse;" border="1" cellspacing="0" cellpadding="4">
<tbody>
<tr>
<td> </td>
<td>ALIŞ</td>
<td>SATIŞ</td>
</tr>
<tr>
<td>USD</td>
<td><?=$TCMB->AlisGetir( "USD" )?></td>
<td><?=$TCMB->SatisGetir( "USD" )?></td>
</tr>
<?php
/** Kur Belirle fonksiyonu ile sayfa içinde kullanacağınız kuru tek seferde belirleyebilirsiniz.
*  Bu sayede daha sonra kullanacağınız AlisGetir() ve SatisGetir() fonksiyonlarında ayrıca kur belirtmeniz gerekmez. */
$TCMB->KurBelirle( "EUR" );

?><tr>
 <td>EURO</td>
 <td><?=$TCMB->AlisGetir()?></td>
 <td><?=$TCMB->AlisGetir()?></td>
 </tr>
</tbody>
</table>
</body>
</html>

Bu yazıyı beğendiniz mi?

RSS Kaynağımıza abone olun!

Kategori: PHP Yorum gönder.
Yorumlar (0) Geri izlemeler (0)

Yorum yapılmadı.


Yorum gönder.


Geri izleme yok.