PHP Stock Quote Web Service

Yahoo finance provides webservice. Basically when you access the url, you are accessing the yahoo webservice which pulls the data you requested. In the above url I am making an inquiry about AMZN ( 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
    //Dow    is ^DJI
    //Nasdaq is ^IXIC
    //S&P    is ^GSPC
    $arr = array('AAPL','AMZN','DELL','EBAY','GOOG','NFLX','YHOO');
    //codes like sl1c1d1 defined in
    $url= "".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>";
    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

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


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