如何使用 cURL 同时向多个 URL 发送 GET 数据?

发布于 2024-08-30 10:41:51 字数 1813 浏览 4 评论 0原文

抱歉,我实际上多次问过这个问题,但从未完全理解答案。

这是我当前的代码:

while($resultSet = mysql_fetch_array($SQL)){            
$ch = curl_init($resultSet['url'] . $fullcurl); //load the urls and send GET data
            curl_setopt($ch, CURLOPT_TIMEOUT, 2);           //Only load it for two seconds (Long enough to send the data)
            curl_exec($ch);                                 //Execute the cURL
            curl_close($ch);                                //Close it off 
} //end while loop

我在这里所做的,是从 MySQL 数据库 ($resultSet['url']) 获取 URL,向其附加一些额外的变量,只是一些 GET 数据 ($fullcurl),然后简单地请求页。这将启动在这些页面上运行的脚本,该脚本所需要做的就是启动这些脚本。它不需要返回任何输出。只需加载页面足够长的时间即可启动脚本。

然而,目前它一次加载每个 URL(当前为 11 个)。我需要同时加载所有这些。我知道我需要使用curl_multi_,但我对cURL函数如何工作一无所知,所以我不知道如何更改我的代码以在while循环中使用curl_multi_

所以我的问题是:

如何更改此代码以同时加载所有 URL?请解释一下,而不仅仅是给我代码。我想知道每个单独的函数到底做什么。由于 while 循环只是一次发送每一行,curl_multi_exec 甚至可以在 while 循环中工作吗?

当然,任何有关 cURL 函数的参考、指南和教程也很好。最好不要太多来自 php.net,因为虽然它很好地为我提供了语法,但它只是有点,并且解释得不太好。

编辑:好的zaf,这是我目前的代码:

        $mh = curl_multi_init(); //set up a cURL multiple execution handle

$SQL = mysql_query("SELECT url FROM urls") or die(mysql_error()); //Query the shell table
                    while($resultSet = mysql_fetch_array($SQL)){   

        $ch = curl_init($resultSet['url'] . $fullcurl); //load the urls and send GET data
        curl_setopt($ch, CURLOPT_TIMEOUT, 2);           //Only load it for two seconds (Long enough to send the data)
        curl_multi_add_handle($mh, $ch);
    } //No more shells, close the while loop

        curl_multi_exec($mh);                           //Execute the multi execution
        curl_multi_close($mh);                          //Close it when it's finished.

My apologies, I've actually asked this question multiple times, but never quite understood the answers.

Here is my current code:

while($resultSet = mysql_fetch_array($SQL)){            
$ch = curl_init($resultSet['url'] . $fullcurl); //load the urls and send GET data
            curl_setopt($ch, CURLOPT_TIMEOUT, 2);           //Only load it for two seconds (Long enough to send the data)
            curl_exec($ch);                                 //Execute the cURL
            curl_close($ch);                                //Close it off 
} //end while loop

What I'm doing here, is taking URLs from a MySQL Database ($resultSet['url']), appending some extra variables to it, just some GET data ($fullcurl), and simply requesting the pages. This starts the script running on those pages, and that's all that this script needs to do, is start those scripts. It doesn't need to return any output. Just the load the page long enough for the script to start.

However, currently it's loading each URL (currently 11) one at a time. I need to load all of them simultaneously. I understand I need to use curl_multi_, but I haven't the slightest idea on how cURL functions work, so I don't know how to change my code to use curl_multi_ in a while loop.

So my questions are:

How can I change this code to load all of the URLs simultaneously? Please explain it and not just give me code. I want to know what each individual function does exactly. Will curl_multi_exec even work in a while loop, since the while loop is just sending each row one at a time?

And of course, any references, guides, tutorials about cURL functions would be nice, as well. Preferably not so much from php.net, as while it does a good job of giving me the syntax, its just a little dry and not so good with the explanations.

EDIT: Okay zaf, here is my current code as of now:

        $mh = curl_multi_init(); //set up a cURL multiple execution handle

$SQL = mysql_query("SELECT url FROM urls") or die(mysql_error()); //Query the shell table
                    while($resultSet = mysql_fetch_array($SQL)){   

        $ch = curl_init($resultSet['url'] . $fullcurl); //load the urls and send GET data
        curl_setopt($ch, CURLOPT_TIMEOUT, 2);           //Only load it for two seconds (Long enough to send the data)
        curl_multi_add_handle($mh, $ch);
    } //No more shells, close the while loop

        curl_multi_exec($mh);                           //Execute the multi execution
        curl_multi_close($mh);                          //Close it when it's finished.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所谓啦 2024-09-06 10:41:52

在 while 循环中,您需要对每个 URL 执行以下操作:

  • 使用curl_init() 创建一个curl 资源,
  • 通过curl_setopt(..) 设置资源选项

然后,您需要使用curl_multi_init() 并添加来创建多个curl 句柄使用curl_multi_add_handle(...) 之前的所有单独的curl 资源

然后最后您可以执行curl_multi_exec(...)。

一个很好的例子可以在这里找到:http://us。 php.net/manual/en/function.curl-multi-exec.php

In your while loop, you need to do the following for each URL:

  • create a curl resource by using curl_init()
  • set options for resource by curl_setopt(..)

Then you need to create a multiple curl handle by using curl_multi_init() and adding all the previous individual curl resources by using curl_multi_add_handle(...)

Then finally you can do curl_multi_exec(...).

A good example can be found here: http://us.php.net/manual/en/function.curl-multi-exec.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文