Rock The Click

Media, Advertising, Tech and Mumbo Jumbo

Stock quotes or charts on site using php & yahoo finance

Posted in Uncategorized
at 2015.10.13
With 0 Comments

The best way to display simple stock quotes for the price of a given stock on your website is by using PHP and parsing Yahoo Finances vast array of CSV files available for download.


function getQuote($symbol)
$symbol = urlencode( trim( substr(strip_tags($symbol),0,7) ) );
$yahooCSV = “$symbol&f=sl1d1t1c1ohgvpnbaejkr&o=t”;
$csv = fopen($yahooCSV,”r”);
list($quote[‘symbol’], $quote[‘last’], $quote[‘date’], $quote[‘timestamp’], $quote[‘change’], $quote[‘open’],
$quote[‘high’], $quote[‘low’], $quote[‘volume’], $quote[‘previousClose’], $quote[‘name’], $quote[‘bid’],
$quote[‘ask’], $quote[‘eps’], $quote[‘YearLow’], $quote[‘YearHigh’], $quote[‘PE’]) = fgetcsv($csv, ‘,’);
return $quote;
return false;
$RBSQuote = getQuote(“MSFT”); // returns stock last price
echo “Microsoft Corp: MSFT (“.$RBSQuote[‘last’].”)”;

The end result will look like a basic last price:



>Download PHP File

There is also another really cool company called that does full stock charts if you would like to display a stock price with a chart. It adds about a half a second to your page load time and looks beatiful. Since its’ not responsive I have created a simple script to hide the chart on smallers screens so it doesn’t push the browser window width out:


<!– <?php echo $data_result[0][“symbol”]?> Stock Chart –>
<style type=”text/css”>
.mobileHide { display: inline;}
/* Smartphone Portrait and Landscape */
@media only screen
and (min-device-width : 320px)
and (max-device-width : 480px){ .mobileHide { display: none;}}
<div class=”mobileHide”>
<h3> <?php echo $data_result[0][“symbol”]?>: Last Trade [<?php echo date(“m/d/y”,time())?>]</h3>
<div id=”tv-medium-widget-9f9da”></div>
<script type=”text/javascript” src=””></script>
<script type=”text/javascript”>

new TradingView.MediumWidget({
“container_id”: “tv-medium-widget-9f9da”,
“symbols”: [
“gridLineColor”: “#E9E9EA”,
“fontColor”: “#83888D”,
“underLineColor”: “#dbeffb”,
“trendLineColor”: “#4bafe9”,
“width”: “100%”,
“height”: “100%”,
“chartOnly”: false
<!– END Chart –>

Leave a Reply