Search code examples
phpweb-scrapingyahooyahoo-financestock

Yahoo Stock Feed


I'm trying to get a chart of stock opening prices.

I want to use yahoo's data feed for stock data: http://www.gummy-stuff.org/Yahoo-data.htm

Has anyone ever used this? What I want is basically a 1 column table, each row represents one day, and the first column represents the opening price of a particular stock.

How can I query more than just one day?


Solution

  • This is how you could retrieve data and parse it:

    $sourceURL = 'http://finance.yahoo.com/d/quotes.csv?s=AAPL&f=snd1lyr';
    
    $sourceData  = file_get_contents( $sourceURL );
    
    // separate into lines
    $sourceLines = str_getcsv($sourceData, "\n"); 
    
    foreach( $sourceLines as $line ) {
    
        $contents = str_getcsv( $line );
    
        // Now, is an array of the comma-separated contents of a line
    }
    

    Update:

    Yahoo provides historial data e.g. for AAPL on this page:

    http://de.finance.yahoo.com/q/hp?s=AAPL
    

    At the bottom of the page, you may download the table using this link:

    http://ichart.finance.yahoo.com/table.csv?s=AAPL&d=3&e=22&f=2011&g=d&a=8&b=7&c=1984&ignore=.csv
    

    I'd propose to play with the webpage to learn which parameters represent what.