How can I send GET data to multiple URLs simultaneously using cURL?

My apologies, I did ask this question several times, but still did not understand the answers.

Here is my current code:

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

What I'm doing here takes URLs from a MySQL database ($ resultSet ['url']), adding extra variables to it, only some GET data ($ fullcurl) and just querying the pages. This will run the script running on these pages, and that all that this script has to do is run these scripts. He does not need to return any result. Just load the page long enough to run the script.

However, it currently loads each URL (currently 11) one at a time. I need to download them all at the same time. I understand that I need to use curl_multi_, but I have no idea how the cURL functions work, so I don’t know how to change my code to use curl_multi_ in a while loop.

So my questions are:

How can I change this code to download all urls at once? Please explain this and do not just give me the code. I want to know what each individual function does. Will curl_multi_exec even work in a while loop, since the while loop just sends each line in turn?

, , , , 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 finished.
+3
1

while URL-:

  • curl curl_init()
  • curl_setopt (..)

curl curl_multi_init() curl_multi_add_handle (...)

, , curl_multi_exec (...).

: http://us.php.net/manual/en/function.curl-multi-exec.php

+3

Source: https://habr.com/ru/post/1742828/


All Articles