I am creating a command line application. I need to send several POST requests via cURL at the same time after I have completed the login procedures - this means that outgoing requests must send a session identifier, etc.
The chain of events is as follows:
- I open a cURL connection with curl_init
- I log in to the remote site by sending a POST request with curl_exec and getting the HTML back as a response
- I am sending multiple POST requests to the same site at the same time.
I was thinking about using something like this:
// Init connection $ch = curl_init(); // Set curl options curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_POST, 1); // Perform login curl_setopt($ch, CURLOPT_URL, "http://www.mysite/login.php"); $post = array('username' => 'username' , 'password' => 'password'); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post)); $result = curl_exec($ch); // Send multiple requests after being logged on curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1); for($i = 0 ; $i < 10 ; $i++){ $post = array('myvar' => 'changing_value'); curl_setopt($ch, CURLOPT_URL, 'www.myweb.ee/changing_url'); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post)); curl_exec($ch); }
But this does not seem to work, since only the first request in the loop is sent.
Using curl_multi_init may be one of the solutions, but I donβt know if I can pass it the same cURL handle several times with changed parameters for each.
I do not need any response from the server for these simultaneous requests, but it would be great if it could also be done in some way.
It would be ideal if someone could push me in the right direction on how to do this.