What is Market Data API?
The marketstack Data API offers the easy-to-use JSON API that delivers worldwide stock markets from 72 global stock exchanges. If you want to Integrate and display real-time and historical data from stock exchanges on your website, the Market Data API is very useful.
The Stock Market Data API is typically used to collect stock information from exchanges and show on the web application real-time data. To provide the Market Data API service, there are several providers available, but most are paid. Only a few free APIs for real-time and historical stock market data is available. The Market stack API is one of the best real-time, intraday, and historical stock market data supplied by the Market Data API.
Market stack has the following APIs available.
- 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.
Any programming language (PHP, Python, Ruby, jQuery, Nodejs, etc.) can be used for the Marketstack Market Data API. We will teach you how to Integrate Real-Time and Historical Stock Market Data API with PHP in this tutorial.
Follow the simple steps below to implement the PHP Marketstack stock market data API:
Before start integration, you need to have Marketstack Data API Key.
How to Get API Access Key
You have to create an account on marketstack. After creating a successful account, log in to your account. In the dashboard, you will get the API key under the Your API Access Key. Please refer below screenshot image.
How to Configure API?
To authenticate and access the Marketstack API, an Access Key is required. Build the query string using http_build_query() function to pass required params in the marketstack API.
2 3 4 5 6 7 |
$queryString = http_build_query([ 'access_key' => 'YOUR_ACCESS_KEY', 'symbols' => 'AAPL' ]); |
End-of-Day Data with PHP
Request the marketstack API via HTTP GET request using cURL in PHP to get the end-of-day data for one or more stock tickers.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Set API access key $queryString = http_build_query([ 'access_key' => 'YOUR_ACCESS_KEY', 'symbols' => 'AAPL' ]); // API URL with query string $apiURL = sprintf('%s?%s', 'https://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); |
Intraday Data with PHP
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// 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:
2 3 4 5 6 7 8 |
$queryString = http_build_query([ 'access_key' => 'YOUR_ACCESS_KEY', 'symbols' => 'AAPL', 'interval' => '1min' ]); |
Historical Data with PHP
The historical stock prices can be collected from both intraday (intraday) and end-of-day (eod) APIs. In the query string, use date_from and date_to parameters.
2 3 4 5 6 7 8 9 |
$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
To get information about one or multiple stock ticker symbols, use the ticker API. The end-of-day, real-time, and intraday market data for single tickers can also be fetched.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// 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); |
Exchanges Data with PHP
To get information about any of the 72+ stock exchanges, use the Exchanges API
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// 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); |
Finally, Get the Stock Market Data with PHP
The stock market data will be returned in JSON format after a successful API request. To convert the JSON response to a PHP array, use the json_decode() function.
2 3 4 5 6 7 8 9 10 |
// 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... } |
You can also get this all code information on marketstack API documentation.
Conclusion:
This is the simple and easy steps to Integrate Real-Time and Historical Stock Market Data API with PHP. You can modify and customize code as per your requirement.
Are you want to get implementation help, or modify or extend the functionality of this script? Submit a paid service request
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co