63 lines
1.5 KiB
PHP
63 lines
1.5 KiB
PHP
<?php
|
|
|
|
function get2symbol(string $isin, string $openFigiApiKey): ?string
|
|
{
|
|
$url = "https://api.openfigi.com/v3/mapping";
|
|
|
|
$postData = json_encode([
|
|
[
|
|
"idType" => "ID_ISIN",
|
|
"idValue" => $isin
|
|
]
|
|
]);
|
|
|
|
$headers = [
|
|
"Content-Type: application/json",
|
|
"X-OPENFIGI-APIKEY: $openFigiApiKey"
|
|
];
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
$response = curl_exec($ch);
|
|
|
|
if (curl_errno($ch)) {
|
|
curl_close($ch);
|
|
return null;
|
|
}
|
|
|
|
curl_close($ch);
|
|
|
|
$data = json_decode($response, true);
|
|
|
|
if (!isset($data[0]['data']) || empty($data[0]['data'])) {
|
|
return null;
|
|
}
|
|
|
|
// Wir suchen zuerst nach einem Eintrag mit deutschem Handelsplatz (DE)
|
|
$symbolEntry = null;
|
|
foreach ($data[0]['data'] as $entry) {
|
|
if (isset($entry['exchCode']) && $entry['exchCode'] === 'XETR') { // XETR = Deutsche Börse Xetra
|
|
$symbolEntry = $entry;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Wenn kein DE/XETR Eintrag gefunden, nehmen wir einfach den ersten
|
|
if ($symbolEntry === null) {
|
|
$symbolEntry = $data[0]['data'][0];
|
|
}
|
|
|
|
if (isset($symbolEntry['ticker']) && !empty($symbolEntry['ticker'])) {
|
|
// Kombiniere Ticker und Exchange, um ein vollständiges Symbol zu liefern
|
|
$exchange = $symbolEntry['exchCode'] ?? '';
|
|
return $exchange ? "{$symbolEntry['ticker']}.{$exchange}" : $symbolEntry['ticker'];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|