本文翻译自:Is there a way to follow redirects with command line cURL?
I know that in a php script: 我知道在php脚本中:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
will follow redirects. 将遵循重定向。 Is there a way to follow redirects with command line cURL? 有没有办法使用命令行cURL跟踪重定向?
#1楼
参考:https://stackoom.com/question/1FW6s/有没有办法使用命令行cURL跟踪重定向
#2楼
使用位置标头标志:
curl -L <URL>
#3楼
I had a similar problem. 我遇到了类似的问题。 I am posting my solution here because I believe it might help one of the commenters. 我在这里发布我的解决方案,因为我相信它可能会对评论者之一有所帮助。
For me, the obstacle was that the page required a login and then gave me a new URL through javascript. 对我来说,障碍是该页面需要登录,然后通过javascript给我一个新的URL。 Here is what I had to do: 这是我必须做的:
curl -c cookiejar -g -O -J -L -F "j_username=yourusename" -F "j_password=yourpassword" <URL>
Note that j_username and j_password is the name of the fields for my website's login form. 请注意,j_username和j_password是我网站登录表单的字段名称。 You will have to open the source of the webpage to see what the 'name' of the username field and the 'name' of the password field is in your case. 您必须打开网页的来源,以查看用户名字段的“名称”和密码字段的“名称”是什么。 After that I go an html file with java script in which the new URL was embedded. 之后我去了一个带有java脚本的html文件,其中嵌入了新的URL。 After parsing this out just resubmit with the new URL: 解析完后,只需使用新URL重新提交:
curl -c cookiejar -g -O -J -L -F "j_username=yourusename" -F "j_password=yourpassword" <NEWURL>
#4楼
As said, to follow redirects you can use the flag -L
or --location
: 如上所述, 要遵循重定向,您可以使用标志-L
或--location
:
curl -L http://www.example.com
But, if you want limit the number of redirects , add the parameter --max-redirs
但是, 如果要限制重定向的数量 ,请添加参数--max-redirs
--max-redirs <num>
Set maximum number of redirection-followings allowed. 设置允许的最大重定向次数。 If
-L
,--location
is used, this option can be used to prevent curl from following redirections "in absurdum". 如果使用-L
, ---location
,则此选项可用于防止curl跟随“在荒谬中”重定向。 By default, the limit is set to 50 redirections. 默认情况下,限制设置为50次重定向。 Set this option to -1 to make it limitless. 将此选项设置为-1可使其无限制。 If this option is used several times, the last one will be used. 如果多次使用此选项,将使用最后一个选项。