在Mac上使用curl下载文件

时间:2017-07-07 15:40:23

标签: macos shell curl wget

我想在Mac上使用curl在shell脚本中下载文件:

http://dumps.wikimedia.org/barwiki/latest/barwiki-latest-pages-articles.xml.bz2

然而,我总是得到:

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.11.6</center>
</body>
</html>

为什么以及我该怎么办?

1 个答案:

答案 0 :(得分:0)

用php我首先检查url rediect然后尝试获取

    <?
$url='http://xxxx/';    
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE); // We'll parse redirect url from header.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); // We want to just get redirect url but not to follow it.
$response = curl_exec($ch);
preg_match_all('/^Location:(.*)$/mi', $response, $matches);
$data = curl_close($ch);
$turl= !empty($matches[1]) ? trim($matches[1][0]) : 'No redirect found';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $turl);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$homepage=$data = curl_exec($ch);

我无法帮助你使用ssh,但如果poosible使用它与PHP