{"id":4460,"date":"2020-07-13T12:32:06","date_gmt":"2020-07-13T12:32:06","guid":{"rendered":"https:\/\/www.codexworld.com\/?p=4460"},"modified":"2020-07-13T12:32:06","modified_gmt":"2020-07-13T12:32:06","slug":"stock-market-data-api-real-time-historical-with-php","status":"publish","type":"post","link":"https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/","title":{"rendered":"Real-Time and Historical Stock Market Data API with PHP"},"content":{"rendered":"<p><b>Market Data API<\/b> 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.<\/p>\n<p>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.<\/p>\n<ul class=\"bullet_disk_list\">\n<li><b>End-of-Day Data:<\/b> Get daily stock market data.<\/li>\n<li><b>Intraday Data:<\/b> Get intraday and real-time market data.<\/li>\n<li><b>Tickers:<\/b> Get information about stock ticker symbols.<\/li>\n<li><b>Exchanges:<\/b> Get information about all supported exchanges.<\/li>\n<li><b>Currencies:<\/b> Get information about all supported currencies.<\/li>\n<li><b>Timezones:<\/b> Get information about all supported timezones.<\/li>\n<\/ul>\n<p>The <b>marketstack Market Data API<\/b> 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.<\/p>\n<p>Follow the below simple steps to integrate marketstack <b>Stock Market Data API in PHP<\/b>.<\/p>\n<h2>Get API Access Key<\/h2>\n<ul class=\"step_list\">\n<li>Before getting started, create an account on <a href=\"https:\/\/marketstack.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">marketstack<\/a>.<\/li>\n<li>In the dashboard, you will get the API key under the <b>Your API Access Key<\/b>.\n<div class=\"img_center\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/07\/marketstack-api-access-key-codexworld-1024x411.png\" alt=\"\" width=\"960\" height=\"385\" class=\"alignnone size-large wp-image-4462\" srcset=\"https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/07\/marketstack-api-access-key-codexworld-1024x411.png 1024w, https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/07\/marketstack-api-access-key-codexworld-300x121.png 300w, https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/07\/marketstack-api-access-key-codexworld-768x309.png 768w, https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/07\/marketstack-api-access-key-codexworld-200x80.png 200w, https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/07\/marketstack-api-access-key-codexworld-346x139.png 346w, https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/07\/marketstack-api-access-key-codexworld.png 1222w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/div>\n<\/li>\n<\/ul>\n<h2>API Configuration<\/h2>\n<p>The Access Key is required to authenticate and access the marketstack API.<\/p>\n<ul class=\"bullet_disk_list\">\n<li>Build the query string using <code>http_build_query()<\/code> function to pass required params in the marketstack API.<\/li>\n<li>Specify the API Access Key in the <code>access_key<\/code> parameter.<\/li>\n<li>Specify one or multiple comma-separated stock symbols in the <code>symbols<\/code> parameter.<\/li>\n<\/ul>\n<pre><span style=\"color: #0000BB\">$queryString&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">http_build_query<\/span><span style=\"color: #007700\">([ <br \/>&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'access_key'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'YOUR_ACCESS_KEY'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'symbols'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'AAPL' <br \/><\/span><span style=\"color: #007700\">]);<\/span><\/pre>\n<h2>End-of-Day Data with PHP<\/h2>\n<p>To fetch the end-of-day data for one or multiple stock tickers, call marketstack API via HTTP GET request using <a href=\"https:\/\/www.codexworld.com\/post-receive-json-data-using-php-curl\/\">cURL in PHP<\/a>.<\/p>\n<pre><span style=\"color: #FF8000\">\/\/&nbsp;Set&nbsp;API&nbsp;access&nbsp;key <br \/><\/span><span style=\"color: #0000BB\">$queryString&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">http_build_query<\/span><span style=\"color: #007700\">([ <br \/>&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'access_key'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'YOUR_ACCESS_KEY'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'symbols'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'AAPL' <br \/><\/span><span style=\"color: #007700\">]); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;API&nbsp;URL&nbsp;with&nbsp;query&nbsp;string <br \/><\/span><span style=\"color: #0000BB\">$apiURL&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">sprintf<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'%s?%s'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'http:\/\/api.marketstack.com\/v1\/eod'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$queryString<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Initialize&nbsp;cURL <br \/><\/span><span style=\"color: #0000BB\">$ch&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">curl_init<\/span><span style=\"color: #007700\">(); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Set&nbsp;URL&nbsp;and&nbsp;other&nbsp;appropriate&nbsp;options <br \/><\/span><span style=\"color: #0000BB\">curl_setopt<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">CURLOPT_URL<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$apiURL<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">curl_setopt<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">CURLOPT_RETURNTRANSFER<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Execute&nbsp;and&nbsp;get&nbsp;response&nbsp;from&nbsp;API <br \/><\/span><span style=\"color: #0000BB\">$api_response&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">curl_exec<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Close&nbsp;cURL <br \/><\/span><span style=\"color: #0000BB\">curl_close<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">);<\/span><\/pre>\n<p><b>HTTPS Encryption:<\/b><br \/>\nTo make secure API requests use HTTPS (SSL) encryption by calling API URL begins with an https.<\/p>\n<pre>https:\/\/api.marketstack.com\/v1\/eod<\/pre>\n<h2>Intraday Data with PHP<\/h2>\n<p>To fetch the intraday data with data intervals, call marketstack API via HTTP GET request using cURL in PHP.<\/p>\n<pre><span style=\"color: #FF8000\">\/\/&nbsp;Set&nbsp;API&nbsp;access&nbsp;key <br \/><\/span><span style=\"color: #0000BB\">$queryString&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">http_build_query<\/span><span style=\"color: #007700\">([ <br \/>&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'access_key'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'YOUR_ACCESS_KEY'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'symbols'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'AAPL' <br \/><\/span><span style=\"color: #007700\">]); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;API&nbsp;URL&nbsp;with&nbsp;query&nbsp;string <br \/><\/span><span style=\"color: #0000BB\">$apiURL&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">sprintf<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'%s?%s'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'http:\/\/api.marketstack.com\/v1\/intraday'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$queryString<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Initialize&nbsp;cURL <br \/><\/span><span style=\"color: #0000BB\">$ch&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">curl_init<\/span><span style=\"color: #007700\">(); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Set&nbsp;URL&nbsp;and&nbsp;other&nbsp;appropriate&nbsp;options <br \/><\/span><span style=\"color: #0000BB\">curl_setopt<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">CURLOPT_URL<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$apiURL<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">curl_setopt<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">CURLOPT_RETURNTRANSFER<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Execute&nbsp;and&nbsp;get&nbsp;response&nbsp;from&nbsp;API <br \/><\/span><span style=\"color: #0000BB\">$api_response&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">curl_exec<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Close&nbsp;cURL <br \/><\/span><span style=\"color: #0000BB\">curl_close<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">);<\/span><\/pre>\n<p><b>Real-Time Updates:<\/b><br \/>\nSpecify the time in the interval parameter to get real-time updated market data.<\/p>\n<pre><span style=\"color: #0000BB\">$queryString&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">http_build_query<\/span><span style=\"color: #007700\">([ <br \/>&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'access_key'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'YOUR_ACCESS_KEY'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'symbols'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'AAPL'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'interval'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'1min' <br \/><\/span><span style=\"color: #007700\">]);<\/span><\/pre>\n<h2>Historical Data with PHP<\/h2>\n<p>You can obtain the historical stock prices from both, end-of-day (eod) and intraday (intraday) APIs.<\/p>\n<ul class=\"bullet_disk_list\">\n<li>Use date_from and date_to parameters in the query string.<\/li>\n<\/ul>\n<pre><span style=\"color: #0000BB\">$queryString&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">http_build_query<\/span><span style=\"color: #007700\">([ <br \/>&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'access_key'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'YOUR_ACCESS_KEY'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'symbols'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'AAPL'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'date_from'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'2020-01-01'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'date_to'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'2020-02-01' <br \/><\/span><span style=\"color: #007700\">]);<\/span><\/pre>\n<h2>Tickers Data with PHP<\/h2>\n<p>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.<\/p>\n<pre><span style=\"color: #FF8000\">\/\/&nbsp;Set&nbsp;API&nbsp;access&nbsp;key <br \/><\/span><span style=\"color: #0000BB\">$queryString&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">http_build_query<\/span><span style=\"color: #007700\">([ <br \/>&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'access_key'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'YOUR_ACCESS_KEY' <br \/><\/span><span style=\"color: #007700\">]); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;API&nbsp;URL&nbsp;with&nbsp;query&nbsp;string <br \/><\/span><span style=\"color: #0000BB\">$apiURL&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">sprintf<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'%s?%s'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'http:\/\/api.marketstack.com\/v1\/tickers'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$queryString<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Initialize&nbsp;cURL <br \/><\/span><span style=\"color: #0000BB\">$ch&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">curl_init<\/span><span style=\"color: #007700\">(); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Set&nbsp;URL&nbsp;and&nbsp;other&nbsp;appropriate&nbsp;options <br \/><\/span><span style=\"color: #0000BB\">curl_setopt<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">CURLOPT_URL<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$apiURL<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">curl_setopt<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">CURLOPT_RETURNTRANSFER<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Execute&nbsp;and&nbsp;get&nbsp;response&nbsp;from&nbsp;API <br \/><\/span><span style=\"color: #0000BB\">$api_response&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">curl_exec<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Close&nbsp;cURL <br \/><\/span><span style=\"color: #0000BB\">curl_close<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">);<\/span><\/pre>\n<h2>Exchanges Data with PHP<\/h2>\n<p>Use exchanges API to get the information about any of the 72+ stock exchanges.<\/p>\n<pre><span style=\"color: #FF8000\">\/\/&nbsp;Set&nbsp;API&nbsp;access&nbsp;key <br \/><\/span><span style=\"color: #0000BB\">$queryString&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">http_build_query<\/span><span style=\"color: #007700\">([ <br \/>&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'access_key'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'YOUR_ACCESS_KEY' <br \/><\/span><span style=\"color: #007700\">]); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;API&nbsp;URL&nbsp;with&nbsp;query&nbsp;string <br \/><\/span><span style=\"color: #0000BB\">$apiURL&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">sprintf<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'%s?%s'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'http:\/\/api.marketstack.com\/v1\/exchanges'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$queryString<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Initialize&nbsp;cURL <br \/><\/span><span style=\"color: #0000BB\">$ch&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">curl_init<\/span><span style=\"color: #007700\">(); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Set&nbsp;URL&nbsp;and&nbsp;other&nbsp;appropriate&nbsp;options <br \/><\/span><span style=\"color: #0000BB\">curl_setopt<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">CURLOPT_URL<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$apiURL<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">curl_setopt<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">CURLOPT_RETURNTRANSFER<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Execute&nbsp;and&nbsp;get&nbsp;response&nbsp;from&nbsp;API <br \/><\/span><span style=\"color: #0000BB\">$api_response&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">curl_exec<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Close&nbsp;cURL <br \/><\/span><span style=\"color: #0000BB\">curl_close<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">);<\/span><\/pre>\n<h2>Stock Market Data with PHP<\/h2>\n<p>After a successful API request, the stock market data will be returned in JSON format. Use <code>json_decode()<\/code> function to convert the JSON response to array in PHP.<\/p>\n<pre><span style=\"color: #FF8000\">\/\/&nbsp;Convert&nbsp;API&nbsp;json&nbsp;response&nbsp;to&nbsp;array <br \/><\/span><span style=\"color: #0000BB\">$api_result&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">json_decode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$api_response<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Output&nbsp;of&nbsp;the&nbsp;API&nbsp;data <br \/><\/span><span style=\"color: #007700\">foreach&nbsp;(<\/span><span style=\"color: #0000BB\">$api_result<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'data'<\/span><span style=\"color: #007700\">]&nbsp;as&nbsp;<\/span><span style=\"color: #0000BB\">$data<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Execution&nbsp;code&nbsp;goes&nbsp;here... <br \/><\/span><span style=\"color: #007700\">}<\/span><\/pre>\n<h2>Conclusion<\/h2>\n<p>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 <a href=\"https:\/\/marketstack.com\/documentation\" target=\"_blank\" rel=\"noopener noreferrer\">marketstack API<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 <\/p>\n","protected":false},"author":1,"featured_media":4465,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4],"tags":[250,14],"class_list":["post-4460","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-api","tag-php","cat-4-id","has_thumb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Real-Time and Historical Stock Market Data API with PHP - CodexWorld<\/title>\n<meta name=\"description\" content=\"Free Stock Market Data API - Get the real-time market data from stock exchanges via REST API using PHP. Example code to obtain real-time, intraday, and historical stock market data using PHP.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Real-Time and Historical Stock Market Data API with PHP - CodexWorld\" \/>\n<meta property=\"og:description\" content=\"Free Stock Market Data API - Get the real-time market data from stock exchanges via REST API using PHP. Example code to obtain real-time, intraday, and historical stock market data using PHP.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/\" \/>\n<meta property=\"og:site_name\" content=\"CodexWorld\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-13T12:32:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/07\/stock-market-data-api-real-time-historical-with-php-codexworld.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1366\" \/>\n\t<meta property=\"og:image:height\" content=\"768\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"CodexWorld\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codexworldblog\" \/>\n<meta name=\"twitter:site\" content=\"@codexworldweb\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"CodexWorld\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/stock-market-data-api-real-time-historical-with-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/stock-market-data-api-real-time-historical-with-php\\\/\"},\"author\":{\"name\":\"CodexWorld\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\"},\"headline\":\"Real-Time and Historical Stock Market Data API with PHP\",\"datePublished\":\"2020-07-13T12:32:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/stock-market-data-api-real-time-historical-with-php\\\/\"},\"wordCount\":545,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/stock-market-data-api-real-time-historical-with-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/stock-market-data-api-real-time-historical-with-php-codexworld.png\",\"keywords\":[\"API\",\"PHP\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/stock-market-data-api-real-time-historical-with-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/stock-market-data-api-real-time-historical-with-php\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/stock-market-data-api-real-time-historical-with-php\\\/\",\"name\":\"Real-Time and Historical Stock Market Data API with PHP - CodexWorld\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/stock-market-data-api-real-time-historical-with-php\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/stock-market-data-api-real-time-historical-with-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/stock-market-data-api-real-time-historical-with-php-codexworld.png\",\"datePublished\":\"2020-07-13T12:32:06+00:00\",\"description\":\"Free Stock Market Data API - Get the real-time market data from stock exchanges via REST API using PHP. Example code to obtain real-time, intraday, and historical stock market data using PHP.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/stock-market-data-api-real-time-historical-with-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/stock-market-data-api-real-time-historical-with-php\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/stock-market-data-api-real-time-historical-with-php\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/stock-market-data-api-real-time-historical-with-php-codexworld.png\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/stock-market-data-api-real-time-historical-with-php-codexworld.png\",\"width\":1366,\"height\":768,\"caption\":\"stock-market-data-api-real-time-historical-with-php-codexworld\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/stock-market-data-api-real-time-historical-with-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.codexworld.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Real-Time and Historical Stock Market Data API with PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"name\":\"CodexWorld\",\"description\":\"Web &amp; Mobile App Development Company\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.codexworld.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\",\"name\":\"CodexWorld\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"width\":200,\"height\":19,\"caption\":\"CodexWorld\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldweb\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/codexworld\",\"https:\\\/\\\/www.youtube.com\\\/codexworld\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\",\"name\":\"CodexWorld\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"caption\":\"CodexWorld\"},\"description\":\"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.\",\"sameAs\":[\"http:\\\/\\\/www.codexworld.com\",\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldblog\"],\"url\":\"https:\\\/\\\/www.codexworld.com\\\/author\\\/nitya192265\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Real-Time and Historical Stock Market Data API with PHP - CodexWorld","description":"Free Stock Market Data API - Get the real-time market data from stock exchanges via REST API using PHP. Example code to obtain real-time, intraday, and historical stock market data using PHP.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/","og_locale":"en_US","og_type":"article","og_title":"Real-Time and Historical Stock Market Data API with PHP - CodexWorld","og_description":"Free Stock Market Data API - Get the real-time market data from stock exchanges via REST API using PHP. Example code to obtain real-time, intraday, and historical stock market data using PHP.","og_url":"https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/","og_site_name":"CodexWorld","article_publisher":"https:\/\/www.facebook.com\/codexworld","article_author":"https:\/\/www.facebook.com\/codexworld","article_published_time":"2020-07-13T12:32:06+00:00","og_image":[{"width":1366,"height":768,"url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/07\/stock-market-data-api-real-time-historical-with-php-codexworld.png","type":"image\/png"}],"author":"CodexWorld","twitter_card":"summary_large_image","twitter_creator":"@codexworldblog","twitter_site":"@codexworldweb","twitter_misc":{"Written by":"CodexWorld","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/#article","isPartOf":{"@id":"https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/"},"author":{"name":"CodexWorld","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0"},"headline":"Real-Time and Historical Stock Market Data API with PHP","datePublished":"2020-07-13T12:32:06+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/"},"wordCount":545,"commentCount":0,"publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"image":{"@id":"https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/07\/stock-market-data-api-real-time-historical-with-php-codexworld.png","keywords":["API","PHP"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/","url":"https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/","name":"Real-Time and Historical Stock Market Data API with PHP - CodexWorld","isPartOf":{"@id":"https:\/\/www.codexworld.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/#primaryimage"},"image":{"@id":"https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/07\/stock-market-data-api-real-time-historical-with-php-codexworld.png","datePublished":"2020-07-13T12:32:06+00:00","description":"Free Stock Market Data API - Get the real-time market data from stock exchanges via REST API using PHP. Example code to obtain real-time, intraday, and historical stock market data using PHP.","breadcrumb":{"@id":"https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/#primaryimage","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/07\/stock-market-data-api-real-time-historical-with-php-codexworld.png","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/07\/stock-market-data-api-real-time-historical-with-php-codexworld.png","width":1366,"height":768,"caption":"stock-market-data-api-real-time-historical-with-php-codexworld"},{"@type":"BreadcrumbList","@id":"https:\/\/www.codexworld.com\/stock-market-data-api-real-time-historical-with-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codexworld.com\/"},{"@type":"ListItem","position":2,"name":"Real-Time and Historical Stock Market Data API with PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.codexworld.com\/#website","url":"https:\/\/www.codexworld.com\/","name":"CodexWorld","description":"Web &amp; Mobile App Development Company","publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.codexworld.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.codexworld.com\/#organization","name":"CodexWorld","url":"https:\/\/www.codexworld.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","width":200,"height":19,"caption":"CodexWorld"},"image":{"@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldweb","https:\/\/www.linkedin.com\/company\/codexworld","https:\/\/www.youtube.com\/codexworld"]},{"@type":"Person","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0","name":"CodexWorld","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","caption":"CodexWorld"},"description":"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.","sameAs":["http:\/\/www.codexworld.com","https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldblog"],"url":"https:\/\/www.codexworld.com\/author\/nitya192265\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/07\/stock-market-data-api-real-time-historical-with-php-codexworld.png","jetpack_shortlink":"https:\/\/wp.me\/p6bxIh-19W","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/4460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/comments?post=4460"}],"version-history":[{"count":3,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/4460\/revisions"}],"predecessor-version":[{"id":4464,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/4460\/revisions\/4464"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media\/4465"}],"wp:attachment":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media?parent=4460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/categories?post=4460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/tags?post=4460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}