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.

stockquotes

<?
function getQuote($symbol)
{
$symbol = urlencode( trim( substr(strip_tags($symbol),0,7) ) );
$yahooCSV = “http://finance.yahoo.com/d/quotes.csv?s=$symbol&f=sl1d1t1c1ohgvpnbaejkr&o=t”;
$csv = fopen($yahooCSV,”r”);
if($csv)
{
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, ‘,’);
fclose($csv);
return $quote;
}else{
return false;
}}
$RBSQuote = getQuote(“MSFT”); //rocktheclick.com returns stock last price
echo “Microsoft Corp: MSFT (“.$RBSQuote[‘last’].”)”;
?>

The end result will look like a basic last price:

last-quote

 

>Download PHP File

There is also another really cool company called TradingView.com 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;}}
</style>
<div class=”mobileHide”>
<br/><br/>
<center>
<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=”https://d33t3vvu2t2yu5.cloudfront.net/tv.js”></script>
<script type=”text/javascript”>

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

Leave a Reply