1

enter image description herei wanted to read the csv file i load from yahoo that includes some stock data. 2 days before i used this code:

$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);

But now it doesn't work anymore. There is no problem with $url works fine. but reading the lines with fgetcsv hust loops 6 times and got no elemnts in it...

pls help.

sami_analyst
  • 1,751
  • 5
  • 24
  • 43

2 Answers2

3
$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);
}

Outputs:

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
)

Alternative method since above does not seem to work on your server:

$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);
}
kittycat
  • 14,983
  • 9
  • 55
  • 80
  • it's working by setting the variable $ticker_url like that. so where is the mistake in the loop that sets $ticker_url ? – sami_analyst Jan 02 '13 at 15:15
  • @user1791283 $ticker_url should not have the quotes around it as you have it, also I think the reason the above loop is working is because I am specifying the delimiter and enclosure characters. I'll redo your ticker symbol creation loop as well. – kittycat Jan 02 '13 at 15:22
  • @user1791283, try the above and see if it prints out the data for you. `$data` will be an array of all fields for the line in the file, so access each field like so `$data[0]` `$data[3]` etc... – kittycat Jan 02 '13 at 15:31
  • hmmm even not working.... there is sth wron with my ticker... – sami_analyst Jan 02 '13 at 15:36
  • just cant explain myself – sami_analyst Jan 02 '13 at 15:37
  • @user1791283 I tested and it works for me. I made temp $ticker array with stock symbols. Are you using my code in its entirety? – kittycat Jan 02 '13 at 15:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22067/discussion-between-cryptic-and-user1791283) – kittycat Jan 02 '13 at 15:37
1

Add an echo $url; , look at the output and then ... look over the other parameters of fgetcsv : http://php.net/manual/en/function.fgetcsv.php (delimiter and enclosure).

Sorin Trimbitas
  • 1,467
  • 18
  • 35