I don't get all that data back... I get something a little less helpful.... (thank you yahoo for sending a header line for the csv file...)....
HTTP/1.1 200 OK
Date: Tue, 12 May 2009 01:26:34 GMT
P3P: policyref="http://p3p.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"
Cache-Control: private
Connection: close
Transfer-Encoding: chunked
Content-Type: application/octet-stream
4f
"^DJI",8418.77,"5/11/2009","4:02pm",-155.88,8569.23,8569.23,8410.33,332633152
0
Now, the trick here is to remember that all headers will be separated by a single line break (\r\n). The body is separated by a double line-break (\r\n\r\n). So using that, we can just read the entire string back from Yahoo. Then just grab the content you need ( a simple regex will suffice ) and then work your way down from there parsing the CSV.
Unfortunately at this time [man]str_getcsv/man isn't around, so we're forced to use regular expressions.
Below is a copy of what I came up with to quickly parse it out. Just remember, you'll have to test on your own since our outputs differ slightly.
<?php
$fp = fsockopen('download.finance.yahoo.com', 80, $errno, $errstr, 30);
if(!$fp)
{
echo 'Encountered Error (#'.$errno.'): '.$errstr;
exit;
}
$out = 'GET /d/quotes.csv?s=^DJI&f=sl1d1t1c1ohgv&e=.csv HTTP/1.1'."\r\n";
$out .= 'Host: download.finance.yahoo.com'."\r\n";
$out .= 'Content-Type: text/html'."\r\n";
$out .= 'Connection: Close'."\r\n\r\n";
fwrite($fp, $out);
$data = '';
while(!feof($fp))
{
$data .= fgets($fp, 8192);
}
echo '<pre>'.htmlspecialchars($data).'</pre>';
preg_match("~\r\n\r\n(.*)$~iUs", $data, $matches);
$lines = explode("\n", $matches[1]);
echo '<pre>'.print_r($lines, 1).'</pre>';
preg_match_all('~(?:\'|")?(.*)(?:\\1)?,+~iUs', $lines[1], $matches);
echo '<pre>'.print_r($matches, 1).'</pre>';
echo '<ul>';
foreach($matches[1] as $match)
{
echo '<li>'.$match.'</li>';
}
echo '</ul>';
That gives the following output:
HTTP/1.1 200 OK
Date: Tue, 12 May 2009 01:26:34 GMT
P3P: policyref="http://p3p.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"
Cache-Control: private
Connection: close
Transfer-Encoding: chunked
Content-Type: application/octet-stream
4f
"^DJI",8418.77,"5/11/2009","4:02pm",-155.88,8569.23,8569.23,8410.33,332633152
Array
(
[0] => 4f
[1] => "^DJI",8418.77,"5/11/2009","4:02pm",-155.88,8569.23,8569.23,8410.33,332633152
[2] =>
[3] => 0
[4] =>
)
Array
(
[0] => Array
(
[0] => "^DJI",
[1] => 8418.77,
[2] => "5/11/2009",
[3] => "4:02pm",
[4] => -155.88,
[5] => 8569.23,
[6] => 8569.23,
[7] => 8410.33,
)
[1] => Array
(
[0] => "^DJI"
[1] => 8418.77
[2] => "5/11/2009"
[3] => "4:02pm"
[4] => -155.88
[5] => 8569.23
[6] => 8569.23
[7] => 8410.33
)
)
* "^DJI"
* 8418.77
* "5/11/2009"
* "4:02pm"
* -155.88
* 8569.23
* 8569.23
* 8410.33