views:

507

answers:

2

Here is my current code:

    $SQL = mysql_query("SELECT url FROM urls") or die(mysql_error()); //Query the urls table
while($resultSet = mysql_fetch_array($SQL)){ //Put all the urls into one variable

                // Now for some cURL to run it.
            $ch = curl_init($resultSet['url']); //load the urls
            curl_setopt($ch, CURLOPT_TIMEOUT, 2); //No need to wait for it to load. Execute it and go.
            curl_exec($ch); //Execute
            curl_close($ch); //Close it off 
        } //While loop

I'm relatively new to cURL. By relatively new, I mean this is my first time using cURL. Currently it loads one for two seconds, then loads the next one for 2 seconds, then the next. however, I want to make it load ALL of them at the same time. I'm sure its possible, I'm just unsure as to how. If someone could point me in the right direction, I'd appreciate it.

+3  A: 
Isaac
Well all I'm going to have it do is load each url (and the urls it will be loading are blank pages, accessing the urls only start a script and make it run for a preset amount of time) and not save or output any data. Do you think it will cause any problems in this case?
Rob
My guess is that it won't be a problem in that case, but I don't know for sure--if it fails to run or errors out when you try to load all of them at once, you could put a counter in your `while` loop and whenever `counter % batch_size == 0` inside the loop, run the batch and clear it.
Isaac
Woah. hate to bother you with this, but could you please comment some stuff in that code so I can see what everything does exactly?
Rob
No problem (and my fault for not documenting the code when I originally wrote it). If any of it is still unclear, please let me know (and tell me which parts).
Isaac
Very nice, thank you. Just one more question if you don't mind helping some more, how could I implement my current while loop into this?
Rob
Your current `while` loop should be an almost-direct replacement for the first `foreach` loop--that's where each `curl_handle` is created and has its options set. (My code was used on an array of URLs `$urls` generated from an XML file, but pulling it from a database and using the while loop you have ought to work pretty much the same way.)
Isaac
Thank you, you've been a lot of help.
Rob
You're welcome.
Isaac
A: 

It's quite easy, see: http://blog.notahat.com/posts/28