RomelTorres / alpha_vantage

A python wrapper for Alpha Vantage API for financial data.
MIT License
4.3k stars 741 forks source link

PHP Stock Ticker on WP Not Fetching Crypto Data #365

Closed f100001e closed 5 months ago

f100001e commented 6 months ago

`<?php $apiKey = 'API Key';

function callStockdataAPI($symbol, $apiKey) { $endpoint = 'https://www.alphavantage.co/query'; $params = [ 'function' => 'TIME_SERIES_DAILY_ADJUSTED', 'symbol' => $symbol, 'apikey' => $apiKey ];

$url = $endpoint . '?' . http_build_query($params);

try {
    $response = file_get_contents($url);
    if ($response === false) {
        return "API call failed for {$symbol}";
    }
    $data = json_decode($response, true);
    if (json_last_error() !== JSON_ERROR_NONE) {
        return "JSON decoding error for {$symbol}";
    }
    if (!isset($data['Time Series (Daily)'])) {
        return "No data available for {$symbol}";
    }

    $latestData = reset($data['Time Series (Daily)']);
    $stockPrice = $latestData['5. adjusted close'];

    return "{$symbol}: \${$stockPrice}";
} catch (\Throwable $e) {
    return "Error fetching stock price for {$symbol}: " . $e->getMessage();
}

}

function callCryptodataAPI($symbol, $apiKey) { $endpoint = 'https://www.alphavantage.co/query'; $params = [ 'function' => 'DIGITAL_CURRENCY_DAILY', 'symbol' => $symbol, 'market' => 'USD', 'apikey' => $apiKey ];

$url = $endpoint . '?' . http_build_query($params);

try {
    $response = file_get_contents($url);
    if ($response === false) {
        return "API call failed for {$symbol}";
    }
    $data = json_decode($response, true);
    if (json_last_error() !== JSON_ERROR_NONE) {
        return "JSON decoding error for {$symbol}";
    }
    if (!isset($data['Time Series (Digital Currency Daily)'])) {
        return "No data available for {$symbol}";
    }

    $latestData = reset($data['Time Series (Digital Currency Daily)']);
    $price = $latestData['4a. close (USD)'];

    return "{$symbol}: \${$price}";
} catch (\Throwable $e) {
    return "Error fetching cryptocurrency data for {$symbol}: " . $e->getMessage();
}

}

$data = []; $symbols = [ 'GOOG', 'AAPL', 'TSLA', 'META', 'AMZN', 'NFLX', 'GME', 'AMC', 'NOK', 'TSM', 'BLK', 'BTC', 'ETH', 'XRP' ];

foreach ($symbols as $symbol) { if (strpos($symbol, 'BTC') !== false || strpos($symbol, 'ETH') !== false || strpos($symbol, 'XRP') !== false) { $data[] = callCryptodataAPI($symbol, $apiKey); } else { $data[] = callStockdataAPI($symbol, $apiKey); } }

if (!empty($data)) { $stockTickerText = implode(" ", $data); } else { $stockTickerText = "No data available"; } ?>

<!DOCTYPE html>

`

AlphaVantageSupport commented 5 months ago

Closing this due to lack of response. This may also be a wordpress issue instead of the plugin issue.

f100001e commented 5 months ago

Problem solved. It was a PHP issue.

On Jun 7, 2024, at 11:58 PM, AlphaVantageSupport @.***> wrote:

Closing this due to lack of response. This may also be a wordpress issue instead of the plugin issue.

— Reply to this email directly, view it on GitHub https://github.com/RomelTorres/alpha_vantage/issues/365#issuecomment-2155844771, or unsubscribe https://github.com/notifications/unsubscribe-auth/BE3ZZMIZPOTU6DXVRNIL7NLZGKTTBAVCNFSM6AAAAABH5VRZESVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNJVHA2DINZXGE. You are receiving this because you authored the thread.

f100001e commented 3 months ago

Trying to pay my bill

On Jun 7, 2024, at 11:58 PM, AlphaVantageSupport @.***> wrote:

Closing this due to lack of response. This may also be a wordpress issue instead of the plugin issue.

— Reply to this email directly, view it on GitHub https://github.com/RomelTorres/alpha_vantage/issues/365#issuecomment-2155844771, or unsubscribe https://github.com/notifications/unsubscribe-auth/BE3ZZMIZPOTU6DXVRNIL7NLZGKTTBAVCNFSM6AAAAABH5VRZESVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNJVHA2DINZXGE. You are receiving this because you authored the thread.