AlphaVantage SDK is a lightweight PHP wrapper for the Alpha Vantage Stock Market API.
It allows you to quickly integrate real-time stock prices, trends, and historical data into your applications with minimal setup. Whether you're building a financial dashboard, tracking market trends, or simply exploring stock data, this SDK makes it easy to access all the Alpha Vantage endpoints in a clean, modern, and dependency-free way.
- Full Range Endpoints: Access Time Series, Technical Indicators, Forex, and Cryptocurrency data.
- JSON Responses: Designed to work with JSON only.
- Dependency-Free HTTP Client: Uses an inbuilt cURL-based HTTP client.
- Modern PHP Practices: Built for PHP 8.1+ with strict typing, PSR-4 autoloading, and PSR-12 coding standards.
- Robust Error Handling: Custom exceptions for handling API errors and invalid responses.
- Comprehensive Testing: Includes PHPUnit tests to ensure core functionalities remain stable.
- PHP: Version 8.1 or later.
- cURL: Enabled in PHP for HTTP requests.
Install the package via Composer:
composer require sukhrobnurali/alphavantage-sdk
The following examples show how to use the different endpoints provided by the SDK.
Begin by including Composer’s autoloader and initializing the client with your API key:
<?php
require 'vendor/autoload.php';
use AlphaVantage\Client;
$apiKey = 'YOUR_ALPHA_VANTAGE_API_KEY';
$client = new Client($apiKey);
Retrieve intraday stock data for Microsoft (MSFT) at a 5-minute interval:
<?php
$intradayData = $client->timeSeries()->intraday('MSFT', '5min');
print_r($intradayData);
Fetch Simple Moving Average (SMA) data for Microsoft (MSFT):
<?php
$smaData = $client->technicalIndicator()->get('SMA', [
'symbol' => 'MSFT',
'interval' => 'daily',
'time_period' => 10,
'series_type' => 'close'
]);
print_r($smaData);
Get the real-time exchange rate from USD to EUR:
<?php
$exchangeRate = $client->forex()->getExchangeRate('USD', 'EUR');
print_r($exchangeRate);
Retrieve daily cryptocurrency data for Bitcoin (BTC) in USD:
<?php
$cryptoData = $client->crypto()->daily('BTC', 'USD');
print_r($cryptoData);
-
Intraday Data
- Method:
TimeSeriesEndpoint::intraday(string $symbol, string $interval, string $outputSize = 'compact')
- Description: Retrieves intraday time series data for a specified stock symbol.
- Method:
-
Daily Data
- Method:
TimeSeriesEndpoint::daily(string $symbol, string $outputSize = 'compact')
- Description: Retrieves daily time series data for a specified stock symbol.
- Method:
-
Weekly Data
- Method:
TimeSeriesEndpoint::weekly(string $symbol)
- Description: Retrieves weekly time series data for a specified stock symbol.
- Method:
-
Monthly Data
- Method:
TimeSeriesEndpoint::monthly(string $symbol)
- Description: Retrieves monthly time series data for a specified stock symbol.
- Method:
- Technical Indicator Data
- Method:
TechnicalIndicatorEndpoint::get(string $indicator, array $params)
- Description: Retrieves technical indicator data based on the specified indicator (e.g., SMA, EMA) and parameters.
- Method:
-
Exchange Rate
- Method:
ForexEndpoint::getExchangeRate(string $fromCurrency, string $toCurrency)
- Description: Retrieves the real-time exchange rate between two currencies.
- Method:
-
Intraday Forex Data
- Method:
ForexEndpoint::intraday(string $fromCurrency, string $toCurrency, string $interval)
- Description: Retrieves intraday forex data for a given currency pair.
- Method:
-
Daily Data
- Method:
CryptoEndpoint::daily(string $symbol, string $market)
- Description: Retrieves daily digital currency data for the specified cryptocurrency and market.
- Method:
-
Weekly Data
- Method:
CryptoEndpoint::weekly(string $symbol, string $market)
- Description: Retrieves weekly digital currency data for the specified cryptocurrency and market.
- Method:
-
Monthly Data
- Method:
CryptoEndpoint::monthly(string $symbol, string $market)
- Description: Retrieves monthly digital currency data for the specified cryptocurrency and market.
- Method:
Run the test suite using Composer:
composer test
The package includes PHPUnit tests that ensure all core functionalities and error handling mechanisms work as expected.
Contributions are welcome! To contribute:
- Fork the repository.
- Create a branch for your feature or bug fix.
- Commit your changes with clear, descriptive messages.
- Submit a Pull Request detailing your changes.
Please adhere to the coding standards (PSR-12) and include tests for new features or bug fixes.
This project is licensed under the MIT License.
For any questions or further assistance, please open an issue in the repository.