[英]How can I send GET data to multiple URLs at the same time using cURL?
我道歉,我实际上多次问过这个问题,但从来没有完全理解答案。
这是我目前的代码:
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? 请解释一下,而不仅仅是给我代码。 我想知道每个函数究竟做了什么。 curl_multi_exec是否会在while循环中工作,因为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.
在while循环中,您需要为每个URL执行以下操作:
然后你需要使用curl_multi_init()创建一个多个curl句柄,并使用curl_multi_add_handle(...)添加所有以前的单个curl资源
最后你可以做curl_multi_exec(...)。
这里有一个很好的例子: http : //us.php.net/manual/en/function.curl-multi-exec.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.