开发者

How would I automate my array to be used with cURL?

I have an array containing the contents of a MySQL table. I need to put each of these contents into curl_multi_handles so that I can execute them all simultaneously

Here is the code for the array, in case it helps:

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

So I need to put be able to send data to each url at the same time. I don't need to get any data back, and in fact I'll be having them time out after two seconds. It only needs to send the data and then close.

My code prior to this, was executing them one at a time. here is that code:

开发者_如何学编程
    $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);

So my question is: How can I load the contents of the array into curl_multi_handle, execute it, and then remove each handle and close the curl_multi_handle?


You still call curl_init and curl_setopt. Then you load it into a multi_handle, and keep calling execute until it's done. This is based on the documentation at curl_multi_init. Since you're timing out in two seconds, and not processing responses, I think you can just sleep for two seconds at a time. curl_multi_select might be better if you actually need to process the responses.

    $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);


If i were you, i would write class mysql and a class curl. Its very good at all. First i would create a method witch would return all urls from a passed mysql result.

Something like

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);
  }
}

Now this is how you could do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜