Asynchronous cURL using POST

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.

+4
source share
2 answers

You will need to create a new curl handle for each request, and then register it using http://www.php.net/manual/en/function.curl-multi-add-handle.php

here is some code that i pulled out and adapted from my code base, keep in mind that you should add error checking there.

 function CreateHandle($url , $data) { $curlHandle = curl_init($url); $defaultOptions = array ( CURLOPT_COOKIEJAR => 'cookies.txt' , CURLOPT_COOKIEFILE => 'cookies.txt' , CURLOPT_ENCODING => "gzip" , CURLOPT_FOLLOWLOCATION => true , CURLOPT_RETURNTRANSFER => true , CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $data ); curl_setopt_array($curlHandle , $defaultOptions); return $curlHandle; } function MultiRequests($urls , $data) { $curlMultiHandle = curl_multi_init(); $curlHandles = array(); $responses = array(); foreach($urls as $id => $url) { $curlHandles[$id] = CreateHandle($url , $data[$id]); curl_multi_add_handle($curlMultiHandle, $curlHandles[$id]); } $running = null; do { curl_multi_exec($curlMultiHandle, $running); } while($running > 0); foreach($curlHandles as $id => $handle) { $responses[$id] = curl_multi_getcontent($handle); curl_multi_remove_handle($curlMultiHandle, $handle); } curl_multi_close($curlMultiHandle); return $responses; } 
+5
source

There's a faster, more efficient option ... that doesn't require you to use any curl at all ...

http://uk3.php.net/manual/en/book.pthreads.php http://pthreads.org

See github for the latest source, key releases ....

I will say this, file_get_contents may seem attractive, but PHP was never intended to start streaming in this way, its socket levels, etc. do not think about consumption, you may find that it is better to open and sleep between small readings to save CPU usage ... however, you will do it, it will be much better ... and how you do it depends on what resources you want to devote task ...

+2
source

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


All Articles