使用CURL从Yahoo Quotes下载多个CSV

时间:2017-10-11 13:34:35

标签: curl fopen fwrite

创建的CSV只是空白,在浏览器中我收到通知:未定义的偏移量:第29行的0 & 警告:curl_multi_remove_handle()期望参数2为资源。在第30行,我得到注意:未定义的偏移量: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);
?>

0 个答案:

没有答案