home   articles   tags   browse code   

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.

The format it returns is as a csv. Now you can open a csv in excel, or openoffice if you choose, however it is simple to have a php parse the csv and display the results on your webpage.

<?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>";

?>
 

Tags: php
 
happy_coder on Apr 9th, 2010 3:11 pm said:
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 Mar 2nd, 2011 5:00 am said:
This is really good, short and sweet. Thank you very much for this.
 



 

 



Related Articles
 


home  |  privacy policy  |  terms of use  |  contact  


©2012, Zedwood.com

zedwood.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com