PHP get CSV from URL, load into array, format -
i trying write script download historic info yahoo finance csv. script loads info array i've run 2 problems. first despite creating date range pull yahoo, maintain getting entire historical info stock , i'm not sure why.i want lastly 6months calculated current day. second, able utilize str_getcsv load info array, have been unable create loop work set in table first row beingness headers of table columns , rest organized date in rows.
here code:
<?php $stock = "aapl"; $date = strtotime(date('y-m-d') . ' -1 month'); $date2 = strtotime(date('y-m-d') . ' -6 months'); $a = (date('m', $date)); $b = (date('d', $date)); $c =(date('y', $date)); $d = (date('m', $date2)); $e = (date('d', $date2)); $f =(date('y', $date2)); $s = str_getcsv(file_get_contents("http://ichart.yahoo.com/table.csv?s=$stock&a=$d&b=e&c=$f&d=$a&e=$b&f=$c&g=d")); stock:echo $stock; echo '<pre>'; print_r($s); echo '</pre>'; ?>
and here output:
aapl array ( [0] => date [1] => open [2] => high [3] => low [4] => close [5] => volume [6] => adj close 2014-10-10 [7] => 100.69 [8] => 102.03 [9] => 100.30 [10] => 100.73 [11] => 66270200 [12] => 100.73 2014-10-09 [13] => 101.54 [14] => 102.38 [15] => 100.61 [16] => 101.02 [17] => 77312200 [18] => 101.02 2014-10-08 [19] => 98.76 [20] => 101.11 [21] => 98.31 [22] => 100.80 [23] => 57364800 [24] => 100.80 2014-10-07 [25] => 99.43 [26] => 100.12 [27] => 98.73 [28] => 98.75 [29] => 42068200 [30] => 98.75
etc...
any help appreciated!
you missed dollar sign in url in b=e
. , here code in rows:
$data = file_get_contents("http://ichart.yahoo.com/table.csv?s=$stock&a=$d&b=$e&c=$f&d=$a&e=$b&f=$c&g=d"); $rows = explode("\n",$data); $s = array(); foreach($rows $row) { $s[] = str_getcsv($row); }
php arrays loops csv
No comments:
Post a Comment