Search
Syndication
Sponsors

Get Dow Jones stock market data

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)


This example retrieves the latest Dow Jones data from Yahoo finance when a page is loaded

Part 1 : The all important form

<html>
<head>
<script type=”text/javascript” src=”getdow.js”></script>
</head>
<body onLoad=”showDow(this.value)”>

Latest Dow Jones data:

<div id=”dowOutput”>
<b>
</b>
</div>
</p>
</body>
</html>

Part 2 : The JavaScript which in this case is in a file called getdow.js

var xmlHttp;
function showDow(str)
 {
 xmlHttp=GetXmlHttpObject();
 if (xmlHttp==null)
  {
  alert (”Browser does not support HTTP Request”);
  return;
  }
 var url=”getdow.php”;
 url=url+”?q=”+str;
 url=url+”&sid=”+Math.random();
 xmlHttp.onreadystatechange=stateChanged;
 xmlHttp.open(”GET”,url,true);
 xmlHttp.send(null);
 }

function stateChanged()
 {
 if (xmlHttp.readyState==4 || xmlHttp.readyState==”complete”)
  {
  document.getElementById(”dowOutput”)
  .innerHTML=xmlHttp.responseText;
  }
 }function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject(”Msxml2.XMLHTTP”);
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject(”Microsoft.XMLHTTP”);
  }
 }
return xmlHttp;
}

Part 3 : Finally the PHP part which is in a file called getdow.php

<?php
//get the q parameter from URL
$q=$_GET["q"];//find out which feed was selected

$fp = fopen (”http://download.finance.yahoo.com/d/quotes.csv?s=%5EDJI&f=sl1d1t1c1ohgv&e=.csv”,”r“);
$data = fgetcsv ($fp, 1000, “,”)
?>
<!– this is our table which displays the stock info –>
<!– we access the individual items by using $data[0]–>

<table>
<br/>
<tr><td>symbol</td><td><?php echo $data[0] ?></td></tr>
<tr><td>last price</td><td><?php echo $data[1] ?></td></tr>
<tr><td>date</td><td><?php echo $data[2] ?></td></tr>
<tr><td>time</td><td><?php echo $data[3] ?></td></tr>
<tr><td>change</td><td><?php echo $data[4] ?></td></tr>
<tr><td>open</td><td><?php echo $data[5] ?></td></tr>
<tr><td>high</td><td><?php echo $data[6] ?></td></tr>
<tr><td>low</td><td><?php echo $data[7] ?></td></tr>
<tr><td>volume</td><td><?php echo $data[8] ?></td></tr>
</table>
<br/>
Data courtesy of <a href=”http://finance.yahoo.com”>Yahoo finance</a>
<?php
//close the filehandle $fp
fclose ($fp);
?>

Related posts:

  1. Ajax and PHP Stock Quote example This is  a new source code section so we thought...
  2. User chooses stock quotes This example lets the user choose what stock quotes they...
  3. MySQL Tutorial - Sorting Retrieved Data leanjhuls asked: MySQL Tutorial - Sorting Retrieved Data Bookmark...

Related posts brought to you by Yet Another Related Posts Plugin.



Tags: , , ,

Leave a Reply