【问题标题】:Download file with curl on Mac在 Mac 上使用 curl 下载文件
【发布时间】:2017-07-07 15:40:22
【问题描述】:

我想在我的 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>

为什么以及我能做什么?

【问题讨论】:

  • curl -L -L 将跟随重定向。
  • 太棒了,这项工作 - 谢谢!

标签: macos shell curl wget


【解决方案1】:

用php我先检查url重定向,然后尝试获取

    <?
$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,但如果可以的话,请使用 php

【讨论】:

    猜你喜欢
    • 2014-04-06
    • 2016-05-02
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 2019-10-02
    • 2014-05-13
    • 1970-01-01
    相关资源
    最近更新 更多