从命令行使用 curl 或 wget 获取文件不起作用(在 php 它正在工作....)

Getting a file using curl or wget from command line doesn't work (in php it's working ....)

我在 Ubuntu 15 ....

我需要下载这个开放数据 CSV http://www.sviluppoeconomico.gov.it/images/exportCSV/prezzo_alle_8.csv

来自此页面

http://www.sviluppoeconomico.gov.it/index.php/it/open-data/elenco-dataset/2032336-carburanti-prezzi-praticati-e-anagrafica-degli-impianti.

我想使用一个简单的 wget 或 curl 命令行,但是如果我尝试,例如

curl http://www.sviluppoeconomico.gov.it/images/exportCSV/prezzo_alle_8.csv pippo.csv

结果是

<html><head><title>Richiesta Rifiutata</title></head><body>La URL Richiesta e' stata riufiuta. Contattare l'amministratore di sistema.<br><br>The requested URL was rejected. Please consult with your administrator.<br><br></body></html>    
<html>
 <head><title>302 Found</title></head>
  <body bgcolor="white">
   <center><h1>302 Found</h1></center>
   <hr><center>nginx/1.4.6 (Ubuntu)</center>
  </body>
</html>

如果我使用

结果相同
wget http://www.sviluppoeconomico.gov.it/images/exportCSV/prezzo_alle_8.csv 

我试过使用一个简单的 php 程序

<?php
 $url = 'http://www.sviluppoeconomico.gov.it/images/exportCSV/prezzo_alle_8.csv';

 print $url;
 echo '<br>';
 echo '<br>';

 //#Set CURL parameters: pay attention to the PROXY config !!!!
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
 $data = curl_exec($ch);
 curl_close($ch);

 echo $data;
?>

在这种情况下,CSV 文件的数据打印在我的浏览器页面上(我一直在等待,但它已打印出来....)。

所以,我认为应该可以从命令行使用 curl 或 wget 下载数据,并且可能有一些我应该设置的参数,但我现在没有解决方案...

有什么建议/例子吗?

提前致谢!!

切萨雷

您需要在 curl 命令中使用 -L 开关,因为您正在使用 php 代码中的 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 来跟随重定向。

有关此 -L 开关的更多信息可在此处找到:http://curl.haxx.se/docs/manpage.html#-L

还使用以下选项覆盖默认的 curl 用户代理字符串(即 User-Agent: curl/7.40.0

 -A "Opera"

对于 wget 命令,为用户代理字符串添加以下开关。

-U "Opera"