Display real-time stock market data with Laravel
If you want to build an exchange or just want to get real-time world market data, try marketstack. This is an easy-to-use REST API interface delivering worldwide stock market data in JSON format. And it's FREE to use!
Features
Market data, supporting real-time, intraday and historical data from 72 global stock exchanges.
Real-Time Market Data
Intraday Market Data
30+ Years Historical Data
125,000+ Stock Tickers
72 Stock Exchanges
Documentation
You can read the official documentation on the marketstack website.
How to use
Register to get a free API key
You need to go to marketstack and register to get a free API key.
Using Raw PHP
You can use raw PHP to grab the market data. Here is the sample code (which can be found at marketstack documentation):
$queryString = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY'
]);
$ch = curl_init(sprintf('%s?%s', 'https://api.marketstack.com/v1/tickers/aapl/eod', $queryString));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close($ch);
$apiResult = json_decode($json, true);
foreach ($apiResult['data'] as $stocksData) {
echo sprintf('Ticker %s has a day high of %s on %s', $stockData['symbol'], $stockData['high'], $stockData['date'] )
}
Using Node.js
const axios = require('axios');
const params = {
access_key: 'YOUR_ACCESS_KEY'
}
axios.get('https://api.marketstack.com/v1/tickers/aapl/eod', {params})
.then(response => {
const apiResponse = response.data;
if (Array.isArray(apiResponse['data'])) {
apiResponse['data'].forEach(stockData => {
console.log(`Ticker ${stockData['symbol']}`,
`has a day high of ${stockData['high']},
`on ${stockData['date']}`);
});
}
}).catch(error => {
console.log(error);
});
Using jQuey
$.ajax({
url: 'https://api.marketstack.com/v1/tickers/aapl/eod',
data: {
access_key: 'YOUR_ACCESS_KEY'
},
dataType: 'json',
success: function(apiResponse) {
if (Array.isArray(apiResponse['data'])) {
apiResponse['data'].forEach(stockData => {
console.log(`Ticker ${stockData['symbol']}`,
`has a day high of ${stockData['high']},
`on ${stockData['date']}`);
});
}
}
});