【问题标题】:How to download csv file from the download link in php?如何从 php 中的下载链接下载 csv 文件?
【发布时间】:2019-02-17 03:47:25
【问题描述】:

我想从这个链接下载 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');

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

【问题讨论】:

  • 定义“不工作”。它做什么?错误信息?超时?

标签: php


【解决方案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);

【讨论】:

    【解决方案2】:
    $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);
    
    

    【讨论】:

    • 可以下载了。但是csv文件中有错误内容,像这样:
      ( !) 警告:readfile(): Filename cannot be empty in C:\wamp64\www\test1.php on line 20 th>
      调用堆栈
    【解决方案3】:

    您的 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      相关资源
      最近更新 更多