Market Data API is extremely useful for obtaining real-time and historical data from stock exchanges. The Stock Market Data API is typically used to retrieve stock information from exchanges and display real-time data on a web application. There are numerous providers available to provide Market Data API services, but the majority of them are paid. Only a select few are given free API access to real-time and historical stock market data. The marketstack API is a top Market Data API that offers real-time, intraday, and historical stock market data.
The marketstack API is a simple JSON API that delivers data from 72 global stock exchanges throughout the world. In marketstack, the following APIs are provided.
Get daily stock market data including end-of-day data.
Intraday and real-time market data are available.
Stock ticker symbols: Find out more about stock ticker symbols.
Exchanges: Find more about all of the exchanges that are supported.
Currencies: Learn about all of the currencies that are supported.
Timezones: Find out about all of the timezones that are supported.
Any programming language can access the marketstack Market Data API (PHP, Python, Ruby, jQuery, Nodejs, etc.). In this article, we’ll teach you how to use the marketstack REST API to access real-time stock market data from stock exchanges using PHP.
To use the marketstack Stock Market Data API in PHP, simply follow the steps below.
Obtain an API Access Key
Create an account on Marketstack before you begin.
The API key can be found in the dashboard’s Your API Access Key section.
Configuring the API
To authenticate and access the marketstack API, you’ll need your Access Key.
To pass needed params in the marketstack API, generate the query string using the http build query() function.
In the access key parameter, enter the API Access Key.
In the symbols parameter, provide one or more comma-separated stock symbols.
$queryString = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
'symbols' => 'AAPL'
]);
PHP Data at the End of the Day
Call marketstack API through HTTP GET request using cURL in PHP to get end-of-day data for one or more stock tickers.
// 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: Use HTTPS (SSL) encryption to make secure API queries by using API URLs that begin with a https.
https://api.marketstack.com/v1/eod
PHP for Intraday Data
To get intraday data with data intervals, use cURL in PHP to make an HTTP GET request to the marketstack API.
// 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: To get real-time updated market data, specify the time in the interval option.
$queryString = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
'symbols' => 'AAPL',
'interval' => '1min'
]);
PHP and Historical Data
Both end-of-day (eod) and intraday (intraday) APIs can be used to get historical stock prices.
In the query string, use the date from and date to arguments.
$queryString = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
'symbols' => 'AAPL',
'date_from' => '2020-01-01',
'date_to' => '2020-02-01'
]);
PHP and Tickers Data
To acquire information about one or more stock ticker symbols, use the tickers API. For single tickers, you can also get end-of-day, real-time, and intraday market data.
// 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)
PHP data exchanges
To learn more about any of the 72+ stock exchanges, use the exchanges API.
// 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);
Data on the Stock Market in PHP
The stock market data will be delivered in JSON format after a successful API request. To convert a JSON response to an array in PHP, use the json decode() function.
// 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...
}
Note:
The marketstack API is free to use, however paid plans are available for more complex applications. We’ve included an example of some of the most useful APIs in the example code. To work with stock market data, you can use a variety of marketstack APIs. See the marketstack API documentation for a detailed reference.