PHP code example of denizgolbas / laravel-tcmb-evds
1. Go to this page and download the library: Download denizgolbas/laravel-tcmb-evds 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-evds example snippets
use Denizgolbas\LaravelTcmbEvds\Facades\Evds;
// Tek döviz kuru çekme (otomatik olarak hem alış hem satış, hem forex hem banknote)
$data = Evds::currency('USD')
->today()
->get();
// Çoklu döviz kuru çekme
$data = Evds::currency(['USD', 'EUR'])
->startDate('2024-01-01')
->endDate('2024-01-10')
->get();
// Sadece satış kurları (Forex Selling - Döviz Satış)
$data = Evds::currency('USD')
->startDate('2024-01-01')
->endDate('2024-01-10')
->type('sell')
->get();
// Sadece alış kurları (Forex Buying - Döviz Alış)
$data = Evds::currency('USD')
->startDate('2024-01-01')
->endDate('2024-01-10')
->type('buy')
->get();
// Hem alış hem satış (varsayılan)
$data = Evds::currency('USD')
->startDate('2024-01-01')
->endDate('2024-01-10')
->get();
// Sadece Forex kurları (Döviz)
$data = Evds::currency('USD')
->startDate('2024-01-01')
->endDate('2024-01-10')
->marketType('forex')
->get();
// Sadece Banknote kurları (Efektif)
$data = Evds::currency('USD')
->startDate('2024-01-01')
->endDate('2024-01-10')
->marketType('banknote')
->get();
// Hem forex hem banknote (varsayılan)
$data = Evds::currency('USD')
->startDate('2024-01-01')
->endDate('2024-01-10')
->get();
// Bugün
$data = Evds::currency('USD')->today()->get();
// Dün
$data = Evds::currency('USD')->yesterday()->get();
// Son 7 gün
$data = Evds::currency('USD')->lastDays(7)->get();
// Bu hafta
$data = Evds::currency('USD')->thisWeek()->get();
// Geçen hafta
$data = Evds::currency('USD')->lastWeek()->get();
// Bu ay
$data = Evds::currency('USD')->thisMonth()->get();
// Geçen ay
$data = Evds::currency('USD')->lastMonth()->get();
// Bir önceki günün değerini kullan (varsayılan)
$data = Evds::currency('USD')
->startDate('2024-01-01')
->endDate('2024-01-10')
->nullValueHandling('previous_day')
->get();
// Geçen haftanın ortalamasını kullan
$data = Evds::currency('USD')
->startDate('2024-01-01')
->endDate('2024-01-10')
->nullValueHandling('last_week_avg')
->get();
// Null değerleri atla (kaydetme)
$data = Evds::currency('USD')
->startDate('2024-01-01')
->endDate('2024-01-10')
->nullValueHandling('skip')
->get();
// URL'i görmek için
$url = Evds::currency(['USD', 'EUR'])
->startDate('2024-01-01')
->endDate('2024-01-10')
->type('sell')
->marketType('forex')
->build();
echo $url;
// Çıktı: https://evds2.tcmb.gov.tr/service/evds/series=TP.DK.USD.S.YTL-TP.DK.EUR.S.YTL&startDate=01-01-2024&endDate=10-01-2024&type=json
use Denizgolbas\LaravelTcmbEvds\Models\EvdsCurrencyRate;
use Carbon\Carbon;
// Bugünün USD kuru
$rate = EvdsCurrencyRate::where('code', 'USD')
->where('date', Carbon::today())
->first();
// Belirli bir tarih aralığındaki kurlar
$rates = EvdsCurrencyRate::where('code', 'USD')
->whereBetween('date', ['2024-01-01', '2024-01-10'])
->get();
// Sadece satış kurları
$sellRates = EvdsCurrencyRate::where('code', 'USD')
->sell()
->get();
// Sadece forex kurları
$forexRates = EvdsCurrencyRate::where('code', 'USD')
->forex()
->get();
// Kombinasyon: USD Forex Satış
$usdForexSell = EvdsCurrencyRate::where('code', 'USD')
->sell()
->forex()
->whereBetween('date', ['2024-01-01', '2024-01-10'])
->orderBy('date', 'asc')
->get();
// En son kaydedilen USD kuru
$latestUsd = EvdsCurrencyRate::where('code', 'USD')
->latest('date')
->first();
// Her döviz için en son kurlar
$latestRates = EvdsCurrencyRate::whereIn('code', ['USD', 'EUR'])
->latest('date')
->get()
->groupBy('code');
use Denizgolbas\LaravelTcmbEvds\Facades\Evds;
// Her gün çalışacak bir scheduled task
public function handle()
{
// USD, EUR, GBP için bugünün kurlarını çek ve kaydet
$saved = Evds::currency(['USD', 'EUR', 'GBP'])
->today()
->type('sell')
->marketType('forex')
->nullValueHandling('previous_day')
->save();
return "{$saved->count()} adet kur kaydedildi.";
}
use Denizgolbas\LaravelTcmbEvds\Models\EvdsCurrencyRate;
// Son 30 günün ortalama USD kuru
$avgRate = EvdsCurrencyRate::where('code', 'USD')
->sell()
->forex()
->where('date', '>=', Carbon::now()->subDays(30))
->avg('rate');
// En yüksek ve en düşük kurlar
$maxRate = EvdsCurrencyRate::where('code', 'USD')
->sell()
->forex()
->max('rate');
$minRate = EvdsCurrencyRate::where('code', 'USD')
->sell()
->forex()
->min('rate');
use Denizgolbas\LaravelTcmbEvds\Facades\Evds;
use Denizgolbas\LaravelTcmbEvds\Exceptions\InvalidConfigurationException;
use Denizgolbas\LaravelTcmbEvds\Exceptions\ApiException;
try {
$data = Evds::currency('USD')
->today()
->get();
} catch (InvalidConfigurationException $e) {
// API key eksik veya yanlış yapılandırılmış
logger()->error('EVDS Config Error: ' . $e->getMessage());
return response()->json(['error' => 'API yapılandırma hatası'], 500);
} catch (ApiException $e) {
// API isteği başarısız
logger()->error('EVDS API Error: ' . $e->getMessage());
return response()->json(['error' => 'API isteği başarısız'], 500);
} catch (\Exception $e) {
// Genel hata
logger()->error('EVDS Error: ' . $e->getMessage());
return response()->json(['error' => 'Beklenmeyen bir hata oluştu'], 500);
}
namespace App\Models;
use Denizgolbas\LaravelTcmbEvds\Models\EvdsCurrencyRate as BaseModel;
class CustomCurrencyRate extends BaseModel
{
// Özelleştirmeleriniz
}