fgetcsv不适用于雅虎股票csv?

时间:2013-01-02 14:42:19

标签: php csv yahoo-finance

enter image description here我想阅读我从yahoo加载的csv文件,其中包含一些股票数据。我使用此代码前2天:

$ticker_url="";
for($i=0;$i<200;$i++){
    if($i==199){
            // $ticker=array with all 200 stock ticker....
        $ticker_url=$ticker_url.$ticker[$i];
    }else{
        $ticker_url=$ticker_url.$ticker[$i]."+";
    }
}

$url="http://finance.yahoo.com/d/quotes.csv?s='$ticker_url'&f=snxab2l1va2p2opm3m4ghd1t1=.csv";
$filehandle=fopen($url,"r");
while(!feof($filehandle)){

    $line=fgetcsv($filehandle,1024);
    echo $line[0]."<br/>";

}
fclose($filehandle);

但现在它不再起作用了。 $ url工作正常没有问题。但是用fgetcsv hust循环读取线路6次并且没有任何元素......

请帮助。

2 个答案:

答案 0 :(得分:2)

$ticker = array('GOOG', 'ACE', 'FDX'); // test tickers

$ticker_url = '';

$count = count($ticker);
for ($i = 0; $i < $count; $i++)
{
    $ticker_url .= (0 === $i)
        ? $ticker[$i]
        : '+' . $ticker[$i];
}

$url = 'http://finance.yahoo.com/d/quotes.csv?s=' . $ticker_url . '&f=snxab2l1va2p2opm3m4ghd1t1=.csv';

ini_set('auto_detect_line_endings', TRUE);
if (($handle = fopen($url, 'r')) !== FALSE) 
{
    while (($data = fgetcsv($handle, 1024, ',', '"')) !== FALSE) 
    {
        print_r($data);
    }
    fclose($handle);
}

输出:

Array
(
    [0] => GOOG
    [1] => Google Inc.
    [2] => NasdaqNM
    [3] => 719.55
    [4] => 719.55
    [5] => 719.49
    [6] => 912426
    [7] => 2583440
    [8] => +1.71%
    [9] => 720.03
    [10] => 707.38
    [11] => 688.697
    [12] => 669.546
    [13] => 718.00
    [14] => 727.00
    [15] => 1/2/2013
    [16] => 10:27am
    [17] => GOOG
    [18] => GOOG
    [19] => +12.11 - +1.71%
    [20] => GOOG
    [21] => 912426
)
Array
(
    [0] => ACE
    [1] => Ace Limited Commo
    [2] => NYSE
    [3] => N/A
    [4] => 80.91
    [5] => 80.91
    [6] => 279438
    [7] => 1476060
    [8] => +1.39%
    [9] => 81.20
    [10] => 79.80
    [11] => 79.4015
    [12] => 76.014
    [13] => 80.56
    [14] => 81.58
    [15] => 1/2/2013
    [16] => 10:28am
    [17] => ACE
    [18] => ACE
    [19] => +1.11 - +1.39%
    [20] => ACE
    [21] => 279438
)
Array
(
    [0] => FDX
    [1] => FedEx Corporation
    [2] => NYSE
    [3] => N/A
    [4] => 94.59
    [5] => 94.59
    [6] => 594530
    [7] => 2092040
    [8] => +3.13%
    [9] => 93.46
    [10] => 91.72
    [11] => 89.7738
    [12] => 89.4001
    [13] => 93.37
    [14] => 95.19
    [15] => 1/2/2013
    [16] => 10:28am
    [17] => FDX
    [18] => FDX
    [19] => +2.87 - +3.13%
    [20] => FDX
    [21] => 594530
)

上面的替代方法似乎不适用于您的服务器:

$ticker_url = '';

$count = count($ticker);
for ($i = 0; $i < $count; $i++)
{
    $ticker_url .= (0 === $i)
        ? $ticker[$i]
        : '+' . $ticker[$i];
}

$url = 'http://finance.yahoo.com/d/quotes.csv?s=' . $ticker_url . '&f=snxab2l1va2p2opm3m4ghd1t1=.csv';
$csv = explode("\n", file_get_contents($url));
unset($csv[count($csv) - 1]); // remove empty row

foreach ($csv as $line)
{
    $data = str_getcsv($line);
    print_r($data);
}

答案 1 :(得分:1)

添加echo $url;,查看输出,然后...查看fgetcsv的其他参数:http://php.net/manual/en/function.fgetcsv.php(分隔符和附件)。