Installation
composer require cheesaw/alpha-vantage-bundle
Add the bundle to config/bundles.php:
CheeSaW\AlphaVantageBundle\AlphaVantageBundle::class => ['all' => true],
Configuration Publish the default config:
php bin/console config:dump-reference CheeSaWAlphaVantageBundle
Update config/packages/cheesaw_alpha_vantage.yaml with your Alpha Vantage API key:
cheesaw_alpha_vantage:
api_key: '%env(ALPHA_VANTAGE_API_KEY)%'
base_url: 'https://www.alphavantage.co/query'
First Use Case Fetch real-time stock data (e.g., AAPL):
use CheeSaW\AlphaVantageBundle\Service\AlphaVantageService;
class StockController extends AbstractController
{
public function show(AlphaVantageService $alphaVantage): Response
{
$data = $alphaVantage->getStockQuote('AAPL');
return $this->json($data);
}
}
Service Injection
Inject AlphaVantageService into controllers/services for API calls:
public function __construct(private AlphaVantageService $api) {}
Common Endpoints
$quote = $this->api->getStockQuote('MSFT');
$timeSeries = $this->api->getTimeSeries('GOOG', 'DAILY');
$crypto = $this->api->getCryptoQuote('BTC', 'USD');
$news = $this->api->getNews('Apple');
Rate Limiting & Caching
# config/packages/cheesaw_alpha_vantage.yaml
cheesaw_alpha_vantage:
cache_enabled: true
cache_pool: 'app.cache.array'
cache_ttl: 600 # 10 minutes
Error Handling Wrap API calls in try-catch:
try {
$data = $this->api->getStockQuote('INVALID_TICKER');
} catch (\CheeSaW\AlphaVantageBundle\Exception\ApiException $e) {
$this->addFlash('error', $e->getMessage());
}
API Key Leaks
%env() in YAML or .env.php bin/console debug:config cheesaw_alpha_vantage
Rate Limits
$this->api->getRateLimitStatus();
Data Format Quirks
TIME_SERIES_DAILY) return nested structures. Flatten with:
$flattened = $this->api->flattenTimeSeries($rawData);
Deprecated Endpoints
GLOBAL_QUOTE) may be deprecated. Check Alpha Vantage’s docs for updates.Enable Verbose Logging:
cheesaw_alpha_vantage:
debug: true
Logs will appear in var/log/dev.log.
Validate Responses:
Use var_dump() or dd() to inspect raw API responses:
$raw = $this->api->rawRequest('FUNCTION=TIME_SERIES_DAILY', ['symbol' => 'AAPL']);
Custom Endpoints Extend the service by creating a decorator:
use CheeSaW\AlphaVantageBundle\Service\AlphaVantageServiceInterface;
class CustomAlphaVantageService implements AlphaVantageServiceInterface
{
public function __construct(private AlphaVantageService $decorated) {}
public function getCustomData(string $symbol): array
{
$data = $this->decorated->rawRequest('FUNCTION=CUSTOM_FUNCTION', ['symbol' => $symbol]);
// Transform data here
return $data;
}
}
Bind it in services.yaml:
services:
CheeSaW\AlphaVantageBundle\Service\AlphaVantageServiceInterface: '@custom_alpha_vantage'
Override Base URL For testing/staging, override the base URL:
cheesaw_alpha_vantage:
base_url: '%env(ALPHA_VANTAGE_TEST_URL)%'
How can I help you explore Laravel packages today?