Yahoo stock quote example

Posted on February 12th, 2009 in PHP by admin

Yahoo stock quote example

<?php
//stock quote script
//this is the url for Microsoft’s stock quote , we are opening it for reading
$fp = fopen (”http://finance.yahoo.com/d/quotes.csv?s=msft&f=sl1d1t1c1ohgv&e=.csv”,”r”);
//this uses the fgetcsv function to store the quote info in the array $data
$data = fgetcsv ($fp, 1000, “,”)
?>

<!– this is our table which displays the stock info –>
<!– we access the individual items by using $data[0]–>

<table>
<tr><td>description</td><td>latest figure</td><tr>
<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>

<?php
//close the filehandle $fp
fclose ($fp);
?>

Related posts:

  1. Yahoo stock chart This example displays a stock chart from yahoo.com, it uses...
  2. Display a CSV file Display a CSV file CSV files are common formats to...
  3. Saving a CSV file to a database Create a database in access called stock.mdb . Now create...

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

4 Responses to 'Yahoo stock quote example'

Subscribe to comments with RSS or TrackBack to 'Yahoo stock quote example'.

  1. Mathi said,

    on March 2nd, 2009 at 10:09 am

    Warning: fopen(http://finance.yahoo.com/d/quotes.csv?s=msft&f=sl1d1t1c1ohgv&e=.csv) [function.fopen]: failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\xampp\htdocs\phpEX\feed.php on line 2

    Fatal error: Maximum execution time of 60 seconds exceeded in C:\xampp\htdocs\phpEX\feed.php on line 2

    I am getting this error while tried out the above to yahoo quotes. :(

  2. admin said,

    on March 2nd, 2009 at 6:58 pm

    I have tried this in my test PC which is using Xampp and I cannot see an issue I’m afraid. I have a reasonably fast connection and the quotes come in about 1 second maximum time.

    Example output

    description latest figure
    symbol MSFT
    last price 15.90
    date 3/2/2009
    time 1:43pm
    change -0.25
    open 15.95
    high 16.25
    low 15.82
    volume 46050552

  3. admin said,

    on March 2nd, 2009 at 7:00 pm

    I noticed you have an Indian email address , you could always try

    http://in.finance.yahoo.com/d/quotes.csv?s=MSFT&f=sl1d1t1c1ohgv&e=.csv

    instead of

    http://finance.yahoo.com/d/quotes.csv?s=msft&f=sl1d1t1c1ohgv&e=.csv

  4. Suj said,

    on November 4th, 2009 at 9:23 pm

    thanks mate. extremely useful. I just had one issue with the quotes, which I fixed it.

    thanks again.

Post a comment