我已经尝试了几天,以便能够调用远程服务器上的url,当您将url放入浏览器中时,该服务器会自动下载Excel文件。我在本地创建了一个自动下载文件以进行测试的页面,但我无法让cURL执行此操作。该脚本不返回任何错误,并表示成功,但它写入的文件已损坏,并且打开时为空。
这是代码。
$output_file = 'E:\Downloads\Export.xlsx';
$download_url = 'http://localhost/test/urlexport.php';
$ch = curl_init();
$out = fopen($output_file, 'w');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_URL, $download_url);
$output = curl_exec($ch);
fwrite($out, $output);
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';
}
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
我已经花了相当多的时间来研究这个问题,但还没有找到一个实际可行的答案。感谢您的帮助,谢谢。
发布于 2019-12-19 21:13:23
如果你不确定自己在做什么--保持简单。这是做你正在做的事情的最简单的方法,它是下载一个文件,然后保存到磁盘。
<?php
$output_file = 'E:\Downloads\Export.xlsx';
$download_url = 'http://localhost/test/urlexport.php';
$file = file_get_contents($download_url);
file_put_contents($output_file, $file);
如果这不起作用,那么你的下载地址就不是你想的那样。看看$output_file中的实际内容(在文本编辑器中打开它)--可能是一些html。
https://stackoverflow.com/questions/59416263
复制