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.
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.
The Access Key is required to authenticate and access the marketstack API.
http_build_query()
function to pass required params in the marketstack API.access_key
parameter.symbols
parameter.$queryString = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
'symbols' => 'AAPL'
]);
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($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 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
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($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 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'
]);
You can obtain the historical stock prices from both, end-of-day (eod) and intraday (intraday) APIs.
$queryString = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
'symbols' => 'AAPL',
'date_from' => '2020-01-01',
'date_to' => '2020-02-01'
]);
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($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute and get response from API
$api_response = curl_exec($ch);
// Close cURL
curl_close($ch);
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($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute and get response from API
$api_response = curl_exec($ch);
// Close cURL
curl_close($ch);
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_response, true);
// Output of the API data
foreach ($api_result['data'] as $data) {
// Execution code goes here...
}
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