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 Alpha Vantage service which provides stock data for free. In the free plan, they provide 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 an endpoint to fetch the stock data. Using their APIs, we can fetch stock data of Intraday, Daily, Weekly, Monthly. They also provide fundamental data like a company overview, earnings, income statement, balance sheet, etc. 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

In order to fetch stock data, we need to send HTTP requests to the provided endpoint of Alpha Vantage. One can do it using cURL. Personally, I love Guzzle for this purpose. Here also 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 ‘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 am using ‘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. Here, 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 required 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 may 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

If you are looking to fetch real-time and historical forex(FX) rates then 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 as FX_DAILY which 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. I would like to hear 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 *