如何自动将我的数组与cURL一起使用?

时间:2010-04-26 20:38:44

标签: php curl

我有一个包含MySQL表内容的数组。我需要将每个内容放入curl_multi_handles中,以便我可以同时执行它们

以下是数组的代码,以防它有用:

$SQL = mysql_query("SELECT url FROM urls") or die(mysql_error()); 
                    while($resultSet = mysql_fetch_array($SQL)){    
                        $urls[]=$resultSet 
                        }

所以我需要能够同时向每个网址发送数据。我不需要取回任何数据,实际上我会让他们在两秒后超时。它只需要发送数据然后关闭。

我之前的代码,一次只执行一个。这是代码:

    $SQL = mysql_query("SELECT url FROM shells") or die(mysql_error());                         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);
            curl_close($ch);

所以我的问题是:如何将数组的内容加载到curl_multi_handle中,执行它,然后删除每个句柄并关闭curl_multi_handle?

2 个答案:

答案 0 :(得分:0)

你仍然会调用curl_init和curl_setopt。然后将其加载到multi_handle中,并继续调用execute直到它完成。这是基于curl_multi_init的文档。由于您在两秒内超时,而不是处理响应,我认为您一次只能睡两秒钟。如果您确实需要处理回复,curl_multi_select可能会更好。

    $SQL = mysql_query("SELECT url FROM shells") ;
    $mh = curl_multi_init();
    $handles = array();
    while($resultSet = mysql_fetch_array($SQL)){       
            //load the urls and send GET data                     
            $ch = curl_init($resultSet['url'] . $fullcurl); 
            //Only load it for two seconds (Long enough to send the data)
            curl_setopt($ch, CURLOPT_TIMEOUT, 2);           
            curl_multi_add_handle($mh, $ch);
            $handles[] = $ch;
    }

    // Create a status variable so we know when exec is done.
    $running = null;
    //execute the handles
    do {
      // Call exec.  This call is non-blocking, meaning it works in the background.
      curl_multi_exec($mh,$running);
      // Sleep while it's executing.  You could do other work here, if you have any.
      sleep(2);
    // Keep going until it's done.
    } while ($running > 0);

    // For loop to remove (close) the regular handles.
    foreach($handles as $ch)
    {
      // Remove the current array handle.
      curl_multi_remove_handle($mh, $ch);
    } 
    // Close the multi handle
    curl_multi_close($mh);

答案 1 :(得分:-1)

如果我是你,我会编写类mysql和类curl。 它非常好。 首先,我将创建一个方法,该方法将从传递的mysql结果返回所有URL。

这样的东西
public function getUrls($mysql_fetch_array)
{
    foreach($mysql_fetch_array as $result)
    {
       $urls[] = $result["url"];
    }                         
}


then you could write a method like curlSend($url,$param)

//remember you have to edit i dont know your full code so its just
// a way you could do it

public function curlSend($url,$param="")
{
$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);
            curl_close($ch); 

}

public function send()
{
  $urls = getUrls($this->mysql->result($sql));

  foreach($urls as $url)
  {
     $this->curlSend($url);
  }
}

现在你可以这样做了。