如何运行多个curl请求,按顺序处理?

时间:2010-06-24 13:38:45

标签: curl request

假设我是一名大型unix新秀, - 我每隔15分钟通过cron运行一次卷曲请求。 - Curl基本上用于加载一个给出一些参数的网页(php),就像一个脚本:

curl http://mysite.com/?update_=1

我想要实现的是使用这种卷曲技术运行另一个“脚本”, - 每次运行其他脚本时 - 在运行其他脚本之前

我已经读过curl在一个命令中接受多个url,但我不确定这是否会按顺序或“并行”处理ulrs。

7 个答案:

答案 0 :(得分:31)

它很可能会顺序处理它们(为什么不测试它)。但你也可以这样做:

1)创建一个名为curlrequests.sh的文件 2)把它放在这样的文件中:

curl http://mysite.com/?update_=1
curl http://mysite.com/?update_=3
curl http://mysite.com/?update_=234
curl http://mysite.com/?update_=65

3)保存文件并使用chmod:

使其可执行
chmod +x curlrequests.sh

4)运行你的文件:

./curlrequests.sh

/path/to/file/curlrequests.sh

作为旁注,您可以使用&&&链接请求,如下所示:

curl http://mysite.com/?update_=1 && curl http://mysite.com/?update_=2 && curl http://mysite.com/?update_=3

使用&:

并行执行
curl http://mysite.com/?update_=1 & curl http://mysite.com/?update_=2 & curl http://mysite.com/?update_=3

答案 1 :(得分:4)

这里没有提到的另一个关键方法是对多个 HTTP 请求使用相同的 TCP 连接,并且为此使用一个 curl 命令。

这对于节省网络带宽、客户端和服务器资源以及使用多个 curl 命令的整体需求非常有用,因为 curl 默认在到达命令结束时关闭连接。

保持连接打开并重复使用它对于运行网络应用程序的标准客户端来说是很常见的。

curl 版本 7.36.0 开始,--next-: 命令行选项允许链接多个请求,并可在命令行和脚本中使用。< /p>

例如:

  • 在同一个 TCP 连接上发送多个请求:
<块引用>

curl http://example.com/?update_=1 -: http://example.com/foo

  • 在同一连接上发送多个不同的 HTTP 请求:
<块引用>

curl http://example.com/?update_=1 -: -d "I am posting this string" http://example.com/?update_=2

  • 为每个请求发送多个具有不同 curl 选项的 HTTP 请求:
<块引用>

curl -o 'my_output_file' http://example.com/?update_=1 -: -d "my_data" -s -m 10 http://example.com/foo -: -o /dev/null http://example.com/random

来自curl manpage

<块引用>

-:, --next

告诉 curl 对以下 URL 使用单独的操作并 相关选项。这允许您发送多个 URL 请求,每个请求 具有自己的特定选项,例如,例如不同的用户 每个名称或自定义请求。

-:, --next 将重置所有本地选项,只有全局选项的值会保留到 -:, --next 之后的操作中 操作说明。全局选项包括 -v、--verbose、--trace、 --trace-ascii 和 --fail-early。

例如,您可以在单个命令中同时执行 GET 和 POST 行:

curl www1.example.com --next -d postthis www2.example.com

在 7.36.0 中添加。

答案 2 :(得分:2)

以所需顺序编写一个包含两个curl请求的脚本,然后由cron运行,如

#!/bin/bash
curl http://mysite.com/?update_=1
curl http://mysite.com/?the_other_thing

答案 3 :(得分:2)

根据curl man page

  

您可以在命令行上指定任意数量的URL。他们会   按指定顺序依次获取。

因此,最简单,最有效的方法(curl将通过一个TCP连接将它们全部发送出去)将它们全部放在一次curl调用上,例如:

curl http://example.com/?update_=1 http://example.com/?update_=2

答案 4 :(得分:0)

我认为这会使用更多本机功能

//printing the links to a file
$ echo "https://stackoverflow.com/questions/3110444/
https://stackoverflow.com/questions/8445445/
https://stackoverflow.com/questions/4875446/" > links_file.txt


$ xargs curl < links_file.txt

享受!

答案 5 :(得分:0)

这将做您想要的,使用输入文件并且超快

#!/bin/bash
IFS=$'\n'
file=/path/to/input.txt
lines=$(cat ${file})
for line in ${lines}; do
   curl "${line}"
done
IFS=""
exit ${?}

输入文件中每行一个条目,它将遵循输入文件的顺序

将其保存为what.sh并使其可执行

答案 6 :(得分:0)

您可以在命令行上指定任意数量的URL。它们将以指定顺序以顺序方式获取。 https://blog.zohear.com/su-kien/yen-sao-nguyen-chat-da-nang/