1

The created CSV are just blank and in browser I get Notices: Undefined offset: 0 on line 29 & Warning: curl_multi_remove_handle() expects parameter 2 to be resource. On line 30 I get Notice: Undefined offset: 0

 <?php

function multiple_download(array $urls, $save_path = '/stockcache/')
{
    $multi_handle = curl_multi_init();
    $file_pointers = [];
    $curl_handles = [];
    $stocks = "GC=F,PL=F,HG=F,PA=F,BZ=F,USDZAR=X,EURZAR=X,GBPZAR=X,AUDZAR=X";
    // Add curl multi handles, one per file we don't already have
    foreach ($urls as $key => $url) {
        foreach ( explode(",", $stocks) as $stock ) {
        $file = "stockcache/".$stock.".csv";
        if(!is_file($file)) {
            $curl_handles[$key] = curl_init($url);
            $file_pointers[$key] = fopen($file, "w");
            curl_setopt($curl_handles[$key], CURLOPT_FILE, $file_pointers[$key]);
            curl_setopt($curl_handles[$key], CURLOPT_HEADER, 0);
            curl_setopt($curl_handles[$key], CURLOPT_CONNECTTIMEOUT, 60);
            curl_multi_add_handle($multi_handle,$curl_handles[$key]);
        }
    }
      }
    // Download the files
    do {
        curl_multi_exec($multi_handle,$running);
    } while ($running > 0);
    // Free up objects
    foreach ($urls as $key => $url) {
        curl_multi_remove_handle($multi_handle, $curl_handles[$key]);
        curl_close($curl_handles[$key]);
        fclose ($file_pointers[$key]);
    }
    curl_multi_close($multi_handle);
}
// Files to download
$urls = [
 'https://finance.yahoo.com/d/quotes.csv?s=GC=F&f=sl1c1&e=.csv',
'https://finance.yahoo.com/d/quotes.csv?s=PL=F&f=sl1c1&e=.csv',
'https://finance.yahoo.com/d/quotes.csv?s=HG=F&f=sl1c1&e=.csv',
'https://finance.yahoo.com/d/quotes.csv?s=PA=F&f=sl1c1&e=.csv',
'https://finance.yahoo.com/d/quotes.csv?s=BZ=F&f=sl1c1&e=.csv',
'https://finance.yahoo.com/d/quotes.csv?s=EURZAR=X&f=sl1c1&e=.csv',
'https://finance.yahoo.com/d/quotes.csv?s=GBPZAR=X&f=sl1c1&e=.csv',
'https://finance.yahoo.com/d/quotes.csv?s=AUDZAR=X&f=sl1c1&e=.csv'
];
multiple_download($urls);
?>
PabloG
  • 11
  • 1

0 Answers0