adfasd
This commit is contained in:
@@ -400,3 +400,139 @@ $mm->registerFunction($moduleName, 'alpha_vantage_fetch_quote', static function
|
||||
'raw' => $quote,
|
||||
];
|
||||
});
|
||||
|
||||
$mm->registerFunction($moduleName, 'alpha_vantage_search_symbols', static function (string $keywords): array {
|
||||
$settings = modules()->settings('boersenchecker');
|
||||
$apiKey = trim((string) ($settings['alpha_vantage_api_key'] ?? ''));
|
||||
$timeout = (int) ($settings['alpha_vantage_timeout_sec'] ?? 12);
|
||||
$timeout = $timeout > 0 ? $timeout : 12;
|
||||
$keywords = trim($keywords);
|
||||
|
||||
if ($keywords === '') {
|
||||
return [
|
||||
'ok' => false,
|
||||
'message' => 'Bitte Suchbegriff angeben.',
|
||||
'results' => [],
|
||||
];
|
||||
}
|
||||
|
||||
if ($apiKey === '') {
|
||||
return [
|
||||
'ok' => false,
|
||||
'message' => 'Alpha-Vantage-API-Key fehlt. Bitte im Modul-Setup hinterlegen.',
|
||||
'results' => [],
|
||||
];
|
||||
}
|
||||
|
||||
$url = 'https://www.alphavantage.co/query?' . http_build_query([
|
||||
'function' => 'SYMBOL_SEARCH',
|
||||
'keywords' => $keywords,
|
||||
'apikey' => $apiKey,
|
||||
]);
|
||||
|
||||
$responseBody = null;
|
||||
|
||||
if (function_exists('curl_init')) {
|
||||
$ch = curl_init($url);
|
||||
if ($ch !== false) {
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_TIMEOUT => $timeout,
|
||||
CURLOPT_CONNECTTIMEOUT => min(5, $timeout),
|
||||
CURLOPT_HTTPHEADER => ['Accept: application/json'],
|
||||
]);
|
||||
$responseBody = curl_exec($ch);
|
||||
$curlError = curl_error($ch);
|
||||
$httpCode = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if (!is_string($responseBody) || $responseBody === '') {
|
||||
return [
|
||||
'ok' => false,
|
||||
'message' => 'Alpha Vantage Suche fehlgeschlagen.'
|
||||
. ($curlError !== '' ? ' ' . $curlError : '')
|
||||
. ($httpCode > 0 ? ' HTTP ' . $httpCode : ''),
|
||||
'results' => [],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_string($responseBody) || $responseBody === '') {
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'GET',
|
||||
'timeout' => $timeout,
|
||||
'header' => "Accept: application/json\r\n",
|
||||
],
|
||||
]);
|
||||
$responseBody = @file_get_contents($url, false, $context);
|
||||
if (!is_string($responseBody) || $responseBody === '') {
|
||||
return [
|
||||
'ok' => false,
|
||||
'message' => 'Alpha Vantage Suche lieferte keine Daten.',
|
||||
'results' => [],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$decoded = json_decode($responseBody, true);
|
||||
if (!is_array($decoded)) {
|
||||
return [
|
||||
'ok' => false,
|
||||
'message' => 'Alpha Vantage Suchantwort ist kein gueltiges JSON.',
|
||||
'results' => [],
|
||||
];
|
||||
}
|
||||
|
||||
if (!empty($decoded['Note'])) {
|
||||
return [
|
||||
'ok' => false,
|
||||
'message' => 'Alpha Vantage Limit-Hinweis: ' . trim((string) $decoded['Note']),
|
||||
'results' => [],
|
||||
];
|
||||
}
|
||||
|
||||
if (!empty($decoded['Information'])) {
|
||||
return [
|
||||
'ok' => false,
|
||||
'message' => trim((string) $decoded['Information']),
|
||||
'results' => [],
|
||||
];
|
||||
}
|
||||
|
||||
if (!empty($decoded['Error Message'])) {
|
||||
return [
|
||||
'ok' => false,
|
||||
'message' => trim((string) $decoded['Error Message']),
|
||||
'results' => [],
|
||||
];
|
||||
}
|
||||
|
||||
$matches = is_array($decoded['bestMatches'] ?? null) ? $decoded['bestMatches'] : [];
|
||||
$results = [];
|
||||
foreach ($matches as $match) {
|
||||
if (!is_array($match)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$results[] = [
|
||||
'symbol' => trim((string) ($match['1. symbol'] ?? '')),
|
||||
'name' => trim((string) ($match['2. name'] ?? '')),
|
||||
'type' => trim((string) ($match['3. type'] ?? '')),
|
||||
'region' => trim((string) ($match['4. region'] ?? '')),
|
||||
'market_open' => trim((string) ($match['5. marketOpen'] ?? '')),
|
||||
'market_close' => trim((string) ($match['6. marketClose'] ?? '')),
|
||||
'timezone' => trim((string) ($match['7. timezone'] ?? '')),
|
||||
'currency' => trim((string) ($match['8. currency'] ?? '')),
|
||||
'match_score' => trim((string) ($match['9. matchScore'] ?? '')),
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'ok' => true,
|
||||
'message' => count($results) . ' Treffer gefunden.',
|
||||
'results' => $results,
|
||||
];
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user