function download_stock_data_history_by_symbol($stock_symbol) { $year = date('Y'); $day = intval(date('d')); $month = intval(date('m')) - 1; $historical_stock_data_link_format = 'http://real-chart.finance.yahoo.com/table.csv?s=%s&a=%s&b=%s&c=2013&d=%s&e=%s&f=%s&g=d&ignore=.csv'; $link = sprintf($historical_stock_data_link_format, $stock_symbol, $day, $month, $day, $month, $year); $historical_stock_data = file_get_contents($link); // Header row of file // Date,Open,High,Low,Close,Volume,Adj Close,Symbol $symbol_included_historical_stock_data = str_replace("\n", ",{$stock_symbol}\n", $historical_stock_data); $historical_stock_data_directory = get_data_subdirectory('historical_data'); ensure_directory_exists($historical_stock_data_directory); $historical_stock_data_filename = $stock_symbol . '.csv'; $filepath = $historical_stock_data_directory . $historical_stock_data_filename; file_put_contents($filepath, $symbol_included_historical_stock_data); }
<?php include './utility.php'; ensure_data_directory_exists(); $nyse_download_link = "http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NYSE&render=download"; $nasdaq_download_link = "http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NASDAQ&render=download"; $listings_dir = get_data_subdirectory('listings'); ensure_directory_exists($listings_dir); $data = file_get_contents($nyse_download_link); file_put_contents($listings_dir . "nyse.csv", $data); $data = file_get_contents($nasdaq_download_link); file_put_contents($listings_dir . "nasdaq.csv", $data);
function ensure_data_directory_exists() { $data_directory = get_data_directory(); ensure_directory_exists($data_directory); }
function ensure_directory_exists($dirpath, $mode = 0775) { if (is_dir($dirpath)) { return TRUE; } if (ensure_directory_exists(dirname($dirpath), $mode)) { if (mkdir($dirpath)) { chmod($dirpath, $mode); return TRUE; } } // Dunno why we'd fail, but fail anyway return FALSE; }