Real-Time and Historical Stock Market Data API with PHP

Market Data API is very useful to get real-time and historical data from the Stock Exchanges. Generally, the Stock Market Data API is used to fetch the stock information from the exchanges and display real-time data on the web application. There are many provides are available to provide the Market Data API service, but most of them are paid. Only some few are provided free API for real-time and historical stock market data. The marketstack API is one of the best Market Data API that provides real-time, intraday, and historical stock market data.

The marketstack API provides the easy-to-use JSON API that delivering from 72 global stock exchanges worldwide stock markets. The following APIs are available in marketstack.

  • End-of-Day Data: Get daily stock market data.
  • Intraday Data: Get intraday and real-time market data.
  • Tickers: Get information about stock ticker symbols.
  • Exchanges: Get information about all supported exchanges.
  • Currencies: Get information about all supported currencies.
  • Timezones: Get information about all supported timezones.

The marketstack Market Data API can be used with any programming languages (PHP, Python, Ruby, jQuery, Nodejs, etc.). In this tutorial, we will show you how to get real-time stock market data from stock exchanges with marketstack REST API using PHP.

Follow the below simple steps to integrate marketstack Stock Market Data API in PHP.

Get API Access Key

  • Before getting started, create an account on marketstack.
  • In the dashboard, you will get the API key under the Your API Access Key.

API Configuration

The Access Key is required to authenticate and access the marketstack API.

  • Build the query string using http_build_query() function to pass required params in the marketstack API.
  • Specify the API Access Key in the access_key parameter.
  • Specify one or multiple comma-separated stock symbols in the symbols parameter.
$queryString http_build_query([ 
  
'access_key' => 'YOUR_ACCESS_KEY',
  
'symbols' => 'AAPL'
]);

End-of-Day Data with PHP

To fetch the end-of-day data for one or multiple stock tickers, call marketstack API via HTTP GET request using cURL in PHP.

// Set API access key 
$queryString http_build_query([
  
'access_key' => 'YOUR_ACCESS_KEY',
  
'symbols' => 'AAPL'
]);

// API URL with query string
$apiURL sprintf('%s?%s''http://api.marketstack.com/v1/eod'$queryString);

// Initialize cURL
$ch curl_init();

// Set URL and other appropriate options
curl_setopt($chCURLOPT_URL$apiURL);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

// Execute and get response from API
$api_response curl_exec($ch);

// Close cURL
curl_close($ch);

HTTPS Encryption:
To make secure API requests use HTTPS (SSL) encryption by calling API URL begins with an https.

https://api.marketstack.com/v1/eod

Intraday Data with PHP

To fetch the intraday data with data intervals, call marketstack API via HTTP GET request using cURL in PHP.

// Set API access key 
$queryString http_build_query([
  
'access_key' => 'YOUR_ACCESS_KEY',
  
'symbols' => 'AAPL'
]);

// API URL with query string
$apiURL sprintf('%s?%s''http://api.marketstack.com/v1/intraday'$queryString);

// Initialize cURL
$ch curl_init();

// Set URL and other appropriate options
curl_setopt($chCURLOPT_URL$apiURL);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

// Execute and get response from API
$api_response curl_exec($ch);

// Close cURL
curl_close($ch);

Real-Time Updates:
Specify the time in the interval parameter to get real-time updated market data.

$queryString http_build_query([ 
  
'access_key' => 'YOUR_ACCESS_KEY',
  
'symbols' => 'AAPL',
  
'interval' => '1min'
]);

Historical Data with PHP

You can obtain the historical stock prices from both, end-of-day (eod) and intraday (intraday) APIs.

  • Use date_from and date_to parameters in the query string.
$queryString http_build_query([ 
  
'access_key' => 'YOUR_ACCESS_KEY',
  
'symbols' => 'AAPL',
  
'date_from' => '2020-01-01',
  
'date_to' => '2020-02-01'
]);

Tickers Data with PHP

Use the tickers API to get the information about one or multiple stock ticker symbols. Also, you can fetch the end-of-day, real-time, and intraday market data for single tickers.

// Set API access key 
$queryString http_build_query([
  
'access_key' => 'YOUR_ACCESS_KEY'
]);

// API URL with query string
$apiURL sprintf('%s?%s''http://api.marketstack.com/v1/tickers'$queryString);

// Initialize cURL
$ch curl_init();

// Set URL and other appropriate options
curl_setopt($chCURLOPT_URL$apiURL);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

// Execute and get response from API
$api_response curl_exec($ch);

// Close cURL
curl_close($ch);

Exchanges Data with PHP

Use exchanges API to get the information about any of the 72+ stock exchanges.

// Set API access key 
$queryString http_build_query([
  
'access_key' => 'YOUR_ACCESS_KEY'
]);

// API URL with query string
$apiURL sprintf('%s?%s''http://api.marketstack.com/v1/exchanges'$queryString);

// Initialize cURL
$ch curl_init();

// Set URL and other appropriate options
curl_setopt($chCURLOPT_URL$apiURL);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

// Execute and get response from API
$api_response curl_exec($ch);

// Close cURL
curl_close($ch);

Stock Market Data with PHP

After a successful API request, the stock market data will be returned in JSON format. Use json_decode() function to convert the JSON response to array in PHP.

// Convert API json response to array 
$api_result json_decode($api_responsetrue);

// Output of the API data
foreach ($api_result['data'] as $data) {
    
// Execution code goes here...
}

Conclusion

The marketstack API is free to use, there also premium plans are available for advanced uses. In the example code, we have provided an example of some most useful APIs. You can find various marketstack APIs to work with the stock market data. For a complete reference, see the documentation of marketstack API.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

Leave a reply

keyboard_double_arrow_up