Integrate Real-Time and Historical Stock Market Data API with PHP - Tutorialswebsite (2024)

Table of Contents

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 providersavailable, 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 onmarketstack. After creating a successful account, log in to your account. In the dashboard, you will get the API key under theYour API Access Key. Please refer below screenshot image.

Integrate Real-Time and Historical Stock Market Data API with PHP - Tutorialswebsite (1)

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

Integrate Real-Time and Historical Stock Market Data API with PHP - Tutorialswebsite (2)

Pradeep Maurya

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 likeDzone,e27.co

Integrate Real-Time and Historical Stock Market Data API with PHP - Tutorialswebsite (2024)
Top Articles
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 5444

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.