Display latest stock market prices
In this example we display the latest stock market information . This example is getting information for the Dow Jones stock market . This is a similar idea to the stock quote script .
Code :
<?php
//this is the url of our csv file
$url = "http://finance.yahoo.com/d/quotes.csv?s=^DJI&f=sl1d1t1c1ohgv&e=.csv";
//open it for reading
$fp = fopen($url , "r");
//if no connection exists display error message
if (!fp)
{
echo "could not connect to the site";
}
else
{
//store the csv file info in the array $data
$data = fgetcsv($fp,1000,",");
//close the file
fclose($fp);
?>
<table>
<tr><td>description</td><td>latest figure</td><tr>
<tr><td>Market</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>
</table>
<?php
}
?>
Example :
| description |
latest figure |
| Market |
^DJI |
| last price |
12986.80 |
| date |
5/16/2008 |
| time |
4:02pm |
| change |
-5.86 |
| open |
12992.74 |
| high |
13001.94 |
| low |
12893.49 |