0

I am writing a program to automatically download data from a url. The data is in csv and I'm trying to convert it to usable information. The downloading part works fine, however, I'm using var_export to then convert the data into an array, then harvest the data from the array. The problem is that the data is not being taken from the array and I have no Idea why. Thanks in advance.

<?php ///without data harvesting
$loop = 1;
while ($loop < 401)
    {
    ///data file harvesting csv
    $symbol = array("A","AA","AAN","AAP","AAT","AAV","AB","ABB","ABBV","ABC","ABEV","ABG","ABM","ABR","ABR-A","ABR-B","ABT","ABW-B","ABX","ACC","ACCO","ACE","ACG","ACH","ACI","ACM","ACMP","ACN","ACO","ACP","ACRE","ACT","ACW","ADC","ADM","ADS","ADT","ADX","AEB","AEC","AED","AEE","AEF","AEG","AEH","AEK","AEL","AEM","AEO","AEP","AER","AES","AES-C","AET","AEV","AF","AF-C","AFA","AFB","AFC","AFG","AFL","AFM","AFQ","AFSD","AFT","AFW","AG","AGC","AGCO","AGD","AGI","AGM","AGM-A","AGM.A","AGN","AGO","AGO-B","AGO-E","AGO-F","AGRO","AGU","AGX","AH","AHC","AHH","AHL","AHL-A","AHL-B","AHL-C","AHP","AHS","AHT","AHT-A","AHT-D","AHT-E","AI","AIB","AIF","AIG","AIG.W","AIN","AIR","AIT","AIV","AIV-Z","AIW","AIY","AIZ","AJG","AKO.A","AKO.B","AKP","AKR","AKS","AL","ALB","ALDW","ALE","ALEX","ALG","ALJ","ALK","ALL","ALL-A","ALL-B","ALL-C","ALLE","ALP-N","ALP-O","ALP-P","ALR","ALR-B","ALSN","ALU","ALV","ALX","AMC","AMD","AME","AMG","AMH","AMH-A","AMID","AMP","AMP-A","AMRC","AMRE","AMT","AMT-A","AMTD","AMTG","AMX","AN","ANF","ANFI","ANH","ANH-A","ANH-B","ANN","ANR","ANW","AOD","AOI","AOL","AON","AOS","AP","APA","APAM","APB","APC","APD","APF","APH","APL","APO","APU","AR","ARC","ARCO","ARCX","ARDC","ARE","ARE-E","ARG","ARH-C","ARI","ARI-A","ARK","ARL","ARMF","ARMK","ARN","ARO","ARP","ARPI","ARR","ARR-A","ARR-B");
        $symboluse = $symbol[$loop];
        $historical_data_1 = file_get_contents('http://ichart.finance.yahoo.com/table.csv?s='.$symboluse.'&a=05&b=06&c=2013&d=06&e=05&f=2014&g=d&ignore=.csv');
        $dividend_data_1 = file_get_contents('http://ichart.finance.yahoo.com/table.csv?s='.$symboluse.'&a=05&b=06&c=2013&d=06&e=05&f=2014&g=v&ignore=.csv');
        $general_data_1 = file_get_contents('http://finance.yahoo.com/d/quotes.csv?s='.$symboluse.'&f=e7e8rr5');
    ///converting csv to usable data
    ///historical
    $h_array = 
    $historical_array = array (1447, explode(',', $historical_data_1), true );
    $dividend_array = array ( explode(',', $dividend_data_1, true) );
    $general_array = array ( explode(',', $general_data_1, true) );
    var_dump($historical_array);
    var_dump($dividend_array);
    var_dump($general_array);
///making it usable data
    $symbol_1 = $symboluse;
    $price = intval($historical_array[11]); /// have to call the array inside an array because the data doesn't collect
    $dividend1 = intval($dividend_array[2]);
    $dividend2 = intval($dividend_array[3]);
    $dividend3 = intval($dividend_array[4]);
    $dividend4 = intval($dividend_array[5]);
    $PE_Ratio = intval($general_array[2]);
    $EPS = intval($general_array[0]);
    $EPS_next_year = intval($general_array[1]);
    $PEG_Ratio = intval($general_array[3]);
    echo $symbol_1."\n";
    echo $price."\n";
    echo $dividend1."\n";
    echo $dividend2."\n";
    echo $dividend3."\n";
    echo $dividend4."\n";
    echo $PE_Ratio."\n";
    echo $EPS."\n";
    echo $EPS_next_year."\n";
    echo $PEG_Ratio."\n";
    $loop++;
    }
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
    Where's the part with var_export? And if you put here some var_dumps as well then we can compare those with your code. – Stefan May 21 '14 at 23:43
  • 1
    `fgetcsv()` or `str_getcsv()` most likely would be easier. Maybe post an example of the file data and the output you want. – AbraCadaver May 21 '14 at 23:48

1 Answers1

0

There's lots wrong here. Using explode() to split up a CSV file will only work in certain circumstances. Here, you have files with multiple lines that need to be separated first. Using str_getcsv() will be more reliable.

Further, you're taking the data from the wrong elements of the resulting arrays

Lastly, you're converting floating point data to integer with intval(), so you'll get a zero result.

Try this version, cut down for testing. You can rebuild the rest yourself.

$symbol = array("A","AA");
foreach ($symbol as $symboluse) {
    $dividend_data_1 = file_get_contents('http://ichart.finance.yahoo.com/table.csv?s='.$symboluse.'&a=05&b=06&c=2013&d=06&e=05&f=2014&g=v&ignore=.csv');

    ///converting csv to usable data
    ///historical
    $Data = str_getcsv($dividend_data_1,"\n");   // separate lines
    $dividend_array = [];
    foreach($Data as $row) {
        $dividend_array[] = str_getcsv($row);    // parse individual lines.
    }
    // var_dump($dividend_array);

///making it usable data
    $dividend1 = $dividend_array[1][1];
    $dividend2 = $dividend_array[2][1];
    $dividend3 = $dividend_array[3][1];
    $dividend4 = $dividend_array[4][1];
    echo $symboluse.' ';
    echo $dividend1.", ";
    echo $dividend2.", ";
    echo $dividend3.", ";
    echo $dividend4;
    echo '<br>';

}