繁体   English   中英

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

[英]How to download csv file from the download link in 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');

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

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

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。 请参阅如何使用php通过https下载文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM