Files
finance/app/Jobs/FetchCurrencyRates.php
T
Ümit Tunç f6ef9fafdc first commit
2025-01-17 21:38:08 +03:00

79 lines
2.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
class FetchCurrencyRates implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle()
{
$data = [];
$data['Update_Date'] = now()->format('Y-m-d H:i:s');
// Döviz kurları
$response = Http::withHeaders([
'User-Agent' => 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10',
'Accept-Language' => 'en'
])->get('https://kur.doviz.com');
// DOM işlemleri için veri çekme
$dom = new \DOMDocument();
@$dom->loadHTML($response->body());
$xpath = new \DOMXPath($dom);
// Döviz kurlarını çekme
$elements = $xpath->query("//*[@data-socket-key]");
foreach ($elements as $element) {
$name = $element->getAttribute('data-socket-key');
$type = $element->getAttribute('data-socket-attr');
$value = $this->virgulToNokta(trim($element->nodeValue));
if (trim($name) !== '') {
if ($name == "JPY") $value = $value / 100;
if ($type == "bid") $data[$name]['Buying'] = $value;
if ($type == "ask") $data[$name]['Selling'] = $value;
if ($type == "c") $data[$name]['Change'] = $value;
$data[$name]['Type'] = "Currency";
}
}
// AZN kuru için
$aznResponse = Http::get('https://wise.com/tr/currency-converter/azn-to-try-rate?amount=1');
preg_match('/(\d+\.\d+)\s+TRY/', $aznResponse->body(), $matches);
if (isset($matches[1])) {
$data['AZN'] = [
'Buying' => $matches[1],
'Selling' => $matches[1],
'Change' => "0.00",
'Type' => "Currency"
];
}
// Altın kurları
$goldResponse = Http::get('https://altin.doviz.com');
// ... Altın kurları için benzer DOM işlemleri ...
// JSON dosyasını kaydet
Storage::put('currency/today.json', json_encode($data, JSON_UNESCAPED_UNICODE));
return $data;
}
private function virgulToNokta($text)
{
$text = str_replace(".", "", $text);
$text = str_replace(",", ".", $text);
$text = str_replace("%", "", $text);
return $text;
}
}