如何使用cURL将GET数据同时发送到多个URL? [英] How can I send GET data to multiple URLs at the same time using cURL?

查看:326
本文介绍了如何使用cURL将GET数据同时发送到多个URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我当前的代码:

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),然后只是请求页面。这将启动在这些页面上运行的脚本,而该脚本所需要做的就是启动这些脚本。不需要返回任何输出。

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.

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

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.

所以我的问题是:

如何更改此代码以同时加载所有URL?请解释一下,而不仅仅是给我代码。我想知道每个函数的功能。 curl_multi_exec甚至可以在while循环中工作,因为while循环只是一次发送每一行?

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?

当然,有关cURL的任何参考资料,指南和教程功能也很好。最好不要从php.net那里下载很多东西,因为它虽然很好地为我提供了语法,但它只是一个干的,而且解释也不是很好。

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.

编辑::扎夫,这是我目前的当前代码:

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.


推荐答案

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

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


  • 通过使用curl_init()创建curl资源

  • 为资源设置选项通过curl_setopt(..)

然后,您需要通过使用curl_multi_init()并添加所有先前的单个卷曲来创建多重卷曲手柄通过使用curl_multi_add_handle(...)

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(...)

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

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

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

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

这篇关于如何使用cURL将GET数据同时发送到多个URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆