为了账号安全,请及时绑定邮箱和手机立即绑定

在php中下载一个通过url触发的csv文件

在php中下载一个通过url触发的csv文件

PHP
莫回无 2021-11-13 15:59:00
我尝试下载通过 URL 触发的 csv 文件。我执行 get 请求,然后返回结果,但内容只是 HTML 代码。我没有取回 csv 文件。如果我print_r是获取请求的结果,则 csv 文件开始在浏览器中下载,但我需要在 PHP 中下载 csv 文件。有没有办法从 PHP 中下载 csv 文件?任何帮助将不胜感激。以下是我到目前为止的代码。获取请求...require_once('hhb_.inc.php');    $hc = new hhb_curl('', true);            $output_filename = "hotProduct_Laptop_Parts.csv";                $fp = fopen($output_filename, 'w+');                set_time_limit(0); // unlimited max execution time                $contents=$hc->setopt_array(array(                  CURLOPT_FILE    => $fp,                  CURLOPT_RETURNTRANSFER => 1,                  CURLOPT_FOLLOWLOCATION=> true,                  CURLOPT_TIMEOUT =>  28800, // set this to 8 hours so we dont timeout on big files                  CURLOPT_URL     => 'https://portals.aliexpress.com/adcenter/hot_product_download.do?categoryId=200001083&categoryName=Laptop%20Parts%20&%20Accessories',                  CURLOPT_HTTPHEADER => array(                      'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',                      'Referer: https://portals.aliexpress.com/adcenter/hotProductRecommend.htm?categoryId=7',                      'accept-language: en-US,en;q=0.5',                      'accept-encoding: gzip, deflate, br',                      'upgrade-insecure-requests:   1',                      'te:  trailers',                      'authority:   portals.aliexpress.com'                       ,                  )                )                )->exec()->getStdOut();        print_r($contents);            // the following lines write the contents to a file in the same directory (provided permissions etc)            fwrite($fp, $contents);            fclose($fp);
查看完整描述

2 回答

?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

您是否正确设置了标题?


header("Content-type: text/csv");

header("Content-Disposition: attachment; filename=file.csv");

header("Pragma: no-cache");

header("Expires: 0");

您需要在 php 文件的顶部指定它们



查看完整回答
反对 回复 2021-11-13
?
慕田峪7331174

TA贡献1828条经验 获得超13个赞

您需要先登录并拥有有效的登录 cookie,然后才能下载该文件。如果您尝试在未登录的浏览器中查看该 URL,您将首先看到登录页面!该页面基本上是说“先登录”。



查看完整回答
反对 回复 2021-11-13
  • 2 回答
  • 0 关注
  • 121 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信