如何从php中的下载链接下载csv文件?

时间:2019-02-17 03:47:26

标签: php

我要从此链接下载csv文件:

https://www.nasdaq.com/investing/etfs/etf-finder-results.aspx?download=Yes

但是这两种代码也不起作用。

$url = 'https://www.nasdaq.com/investing/etfs/etf-finder-results.aspx?download=Yes';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$str = curl_exec($curl);
echo $str;

$table = file_get_contents('https://www.nasdaq.com/investing/etfs/etf-finder-results.aspx?download=Yes');

请告诉我为什么此代码在此链接中不起作用。 谢谢!

3 个答案:

答案 0 :(得分:1)

$file_name = 'ETFList.csv';
$url = 'https://www.nasdaq.com/investing/etfs/etf-finder-results.aspx?download=Yes';

function downloadFile($url, $filename)
{

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible ; Googlebot/2.1 ; +http://www.google.com/bot.html)');
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $data = curl_exec($ch);
    if (curl_errno($ch)) die(curl_error($ch));
    curl_close($ch);

    $file = fopen($filename, 'wb');
    fwrite($file, $data);
    fclose($file);

    if (file_exists($filename)) {

        header('Content-Description: File Transfer');
        header('Content-Type: text/csv; charset=utf-8');
        header('Content-Transfer-Encoding: binary');
        header("Content-Disposition: attachment; filename=\"$filename\"");
        header('Content-Length: ' . filesize($filename));
        readfile($filename);
    }

}

downloadFile($url, $file_name);

答案 1 :(得分:0)

$file = 'ETFList.csv';
$url = 'https://www.nasdaq.com/investing/etfs/etf-finder-results.aspx?download=Yes';

function downloadFile($url, $filename) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $data = curl_exec($ch);
    curl_close($ch);

    header('Content-Description: File Transfer');
    header('Content-Type: text/csv');
    header("Content-Disposition: attachment; filename=\"$filename\"");
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . filesize($data));
    readfile($data);
}

downloadFile($url,$file);

答案 2 :(得分:0)

您的php.ini文件中有一行可以取消注释;

;user_agent="PHP"

或者您可以在代码中临时设置它。

ini_set('user_agent', "PHP");
$table = file_get_contents('https://www.nasdaq.com/investing/etfs/etf-finder-results.aspx?download=Yes');

这是因为您使用的是https。参见How to download a file over https using php