简体   繁体   中英

How to download csv file from the download link in php?

I want to download csv file from this link:

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

but this two kinds of code is also not working.

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

and

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

Pls, tell me about why this code is not working in this link. Thank you!

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

There is a line in your php.ini file you can uncomment;

;user_agent="PHP"

Or you can temporarily set it in your code.

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

This is because you are using https. See How to download a file over https using php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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