Alpha Vantage – Integrate Free Stock APIs in PHP

Recently I have worked on a project where we wanted to integrate free stock APIs. During the research, we came across the Alpha Vantage service which provides free APIs to get stock data. In the free plan, they allow 5 API requests per minute and 500 requests per day. This quota is sufficient for small applications. However, if you need more resources then check out their premium plans.

The Alpha Vantage gives different API endpoints to fetch the stock data. Using their APIs, we can fetch Intraday, Daily, Weekly, Monthly stock data. In addition to this, they provide fundamental data like a company overview, earnings, income statement, balance sheet, etc. Using this service, one can also get foreign exchange rates between different currencies.

In this article, we study how to get stock data using Alpha Vantage in PHP. To get started, grab your API key from the Alpha Vantage website.

Integrate Free Stock APIs in PHP

To fetch the stock data, we need to send HTTP requests to the provided endpoint of Alpha Vantage. The user can do it using cURL. I personally preferred to use Guzzle for this purpose. Here, I am going to send HTTP requests and handle responses using Guzzle.

Install the Guzzle library using the command:

composer require guzzlehttp/guzzle

While interacting with Alpha Vantage APIs, we need to send GET requests to their API endpoint. The API endpoint https://www.alphavantage.co/query is the same for all requests. The only change will be in the ‘GET’ parameters. Let’s start with fetching stock data practically.

Get Intraday Data

On the API documentation of Alpha Vantage, you will find the details about the endpoint and their parameters in detail.

You can get intraday data of any stock as follows. As an example, I take ‘IBM’ company.

<?php
require_once "vendor/autoload.php";

use GuzzleHttp\Client;
 
$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'https://www.alphavantage.co',
]);
 
$response = $client->request('GET', '/query', [
    'query' => [
        'function' => 'TIME_SERIES_INTRADAY',
        'symbol' => 'IBM',
        'interval' => '5min',
        'apikey' => 'YOUR_API_KEY',
    ]
]);
 
$body = $response->getBody();
$arr_body = json_decode($body);
print_r($arr_body);

Replace the placeholder YOUR_API_KEY with your actual API key. In the above code, I am using a function parameter as TIME_SERIES_INTRADAY and interval as 5min.

Daily Data

You can get the daily data of stock using the same endpoint but with different parameters as follows.

$response = $client->request('GET', '/query', [
    'query' => [
        'function' => 'TIME_SERIES_DAILY',
        'symbol' => 'IBM',
        'apikey' => 'YOUR_API_KEY',
    ]
]);

In the above API request, the function parameter used is TIME_SERIES_DAILY.

Fundamental Data

While picking the right stocks, you probably want to have a look over the fundamental details of a company. You will get these details easily with Alpha Vantage.

Company Overview
$response = $client->request('GET', '/query', [
    'query' => [
        'function' => 'OVERVIEW',
        'symbol' => 'IBM',
        'apikey' => 'YOUR_API_KEY',
    ]
]);
Company Earnings
$response = $client->request('GET', '/query', [
    'query' => [
        'function' => 'EARNINGS',
        'symbol' => 'IBM',
        'apikey' => 'YOUR_API_KEY',
    ]
]);
Income Statements
$response = $client->request('GET', '/query', [
    'query' => [
        'function' => 'INCOME_STATEMENT',
        'symbol' => 'IBM',
        'apikey' => 'YOUR_API_KEY',
    ]
]);
Balance Sheet
$response = $client->request('GET', '/query', [
    'query' => [
        'function' => 'BALANCE_SHEET',
        'symbol' => 'IBM',
        'apikey' => 'YOUR_API_KEY',
    ]
]);

Please note you will get details of these endpoints and their parameters on their API documentation.

Foreign Exchange Rates

You may want to fetch real-time and historical forex(FX) rates in your application. You can do it using the code below. In this code, I am getting the USD rate of INR currency.

$response = $client->request('GET', '/query', [
    'query' => [
        'function' => 'CURRENCY_EXCHANGE_RATE',
        'from_currency' => 'USD',
        'to_currency' => 'INR',
        'apikey' => 'YOUR_API_KEY',
    ]
]);

In the same way users can fetch historical forex(FX) rates between 2 currencies.

$response = $client->request('GET', '/query', [
    'query' => [
        'function' => 'FX_DAILY',
        'from_symbol' => 'USD',
        'to_symbol' => 'INR',
        'apikey' => 'YOUR_API_KEY',
    ]
]);

The above code has a function parameter FX_DAILY. It will give you daily forex rates. You can get weekly data with parameter FX_WEEKLY, monthly data with FX_MONTHLY.

It’s all about integrating free stock APIs in PHP with Alpha Vantage. Give it a try and let me know your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *