PHP Stock Quote Web Service


Yahoo finance provides webservice. Basically when you access the url http://finance.yahoo.com/d/quotes.csv?s=AMZN&f=l1, you are accessing the yahoo webservice which pulls the data you requested. In the above url I am making an inquiry about AMZN (amazon.com). The sl1c1, tells the web service that you want the stock symbol (s) along with the last close (l1) and the net change (c1). For a full list of codes see http://www.gummy-stuff.org/Yahoo-data.htm.
<?php
    //Dow    is ^DJI
    //Nasdaq is ^IXIC
    //S&P    is ^GSPC
    $arr = array('AAPL','AMZN','DELL','EBAY','GOOG','NFLX','YHOO');
 
    //codes like sl1c1d1 defined in http://www.gummy-stuff.org/Yahoo-data.htm
    $url= "http://finance.yahoo.com/d/quotes.csv?s=".implode("+", $arr)."&f="."sl1c1d1";
 
    echo "<table>";
    $handle = fopen($url, "r");
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
    {
        echo "<tr>";
        foreach($data as $d)
            echo "<td>$d</td>";
        echo "</tr>";
    }
    fclose($handle);
    echo "</table>";
 
?>
code snippets are licensed under Creative Commons CC-By-SA 3.0 (unless otherwise specified)

happy_coder on 2010-04-09 21:11:44
Thanks for this - I found a really nice (similar) version of this at the useful php blog http://usefulphp.blogspot.com/2010/04/stock-quotes-with-php.html

Wraps up all the info into a nice array for you!

Cheers

chamika on 2011-03-02 12:00:33
This is really good, short and sweet. Thank you very much for this.