PHP code example of denizgolbas / laravel-tcmb-gold

1. Go to this page and download the library: Download denizgolbas/laravel-tcmb-gold library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

denizgolbas / laravel-tcmb-gold example snippets


  $rates = TcmbGold::all();
  // XAU (24 Ayar) ve XAS (SAF/Has) altın fiyatlarını getirir
  

  // Paket otomatik olarak 12:00, 14:00, 16:00 saatlerinde XML'i kontrol eder
  // İlk bulduğu geçerli veriyi döner
  $rates = TcmbGold::all();
  

  // config/tcmb-gold.php
  'cache_duration' => 120, // 2 saat (dakika cinsinden)
  
  // Cache'i temizlemek için
  Cache::forget('tcmb_gold_2025-12-09_12:00');
  

  use DenizTezc\TcmbGold\Models\GoldRate;
  
  $rates = TcmbGold::all();
  foreach ($rates as $rate) {
      GoldRate::updateOrCreate(
          ['code' => $rate['code'], 'date' => $rate['date']],
          $rate
      );
  }
  

use DenizTezc\TcmbGold\Facades\TcmbGold;

// Bugünün tüm altın fiyatlarını al (XAU + XAS)
$rates = TcmbGold::all();

// Tüm altın türlerini listele
foreach ($rates as $gold) {
    echo "{$gold['name']}: {$gold['buying']} TL\n";
}
// Output:
// 24 Ayar Altın: 5734.7 TL
// SAF (Has) Altın: 5763.52 TL

// SAF (Has) Altın (gram fiyatı)
$hasAltin = $rates->firstWhere('code', 'XAS');
echo "1 gram saf altın: {$hasAltin['buying']} TL";

// 24 Ayar Altın
$xau = $rates->firstWhere('code', 'XAU');
echo "24 ayar altın: {$xau['buying']} TL";

use Illuminate\Support\Carbon;

$date = Carbon::parse('2025-12-01');
$rates = TcmbGold::all($date);

[
    [
        'code' => 'XAU',
        'name' => '24 Ayar Altın',
        'buying' => 5734.70,
        'unit' => 1,
        'date' => '2025-12-09',
        'timestamp' => Carbon::instance,
    ],
    [
        'code' => 'XAS',
        'name' => 'SAF (Has) Altın',
        'buying' => 5763.52,
        'unit' => 1,
        'date' => '2025-12-09',
        'timestamp' => Carbon::instance,
    ],
]

return [
    // TCMB Reeskont Kurları base URL
    'base_url' => env('TCMB_GOLD_BASE_URL', 'https://www.tcmb.gov.tr/reeskontkur'),
    
    // Kontrol edilecek saatler (TCMB bu saatlerde XML yayınlar)
    'check_hours' => ['12:00', '14:00', '16:00'],
    
    // Cache ayarları
    'cache_driver' => env('TCMB_GOLD_CACHE_DRIVER', 'file'),
    'cache_duration' => 120, // dakika (2 saat)
    'cache_prefix' => 'tcmb_gold_',
];
bash
  # GitHub Actions'da otomatik test edilir
  # 5 farklı kombinasyon: PHP 8.1 (L10), PHP 8.2/8.3 (L10+L11)
  # Not: Laravel 11 PHP 8.2+ gerektirir
  
bash
php artisan vendor:publish --provider="DenizTezc\TcmbGold\TcmbGoldServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="DenizTezc\TcmbGold\TcmbGoldServiceProvider" --tag="migrations"
php artisan migrate