How to determine when curl_multi_exec is executed _sending_ data

I need to call a web service from a PHP script. The web service is slow, and I'm not interested in his answer, I just want to send him the data.

I'm trying to use curl_multi_exec (following the example here: http://www.jaisenmathai.com/articles/php-curl-asynchronous.html ), and the second parameter ($ still_running) lets you know when it was sent and received. But, again, I'm only interested in when my script will be sent. Of course, if I exit the script before sending the data, the web service never registers the receipt of the request.

Another way to look at this is to detect when PHP is idle waiting for a response from the server.

What I would like to achieve is a dialogue:

  • PHP: Hi, save this data.
  • WS: Okay, noisy, think about it.
  • PHP: Cya! (to do something more important)
  • WS: Ok, I did the processing, here is your answer ... PHP? Where have you gone? I feel used: (
+4
source share
1 answer

You can try

$url = "http://localhost/server.php"; $nodes = array(); $nodes["A"] = array("data" => mt_rand()); <-------- Random Data $nodes["B"] = array("data" => mt_rand()); $nodes["C"] = array("data" => mt_rand()); $nodes["D"] = array("data" => mt_rand()); echo "<pre>"; $mh = curl_multi_init(); $curl_array = array(); foreach ( $nodes as $i => $data ) { $curl_array[$i] = curl_init($url); curl_setopt($curl_array[$i], CURLOPT_RETURNTRANSFER, true); curl_setopt($curl_array[$i], CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)'); curl_setopt($curl_array[$i], CURLOPT_POST, true); curl_setopt($curl_array[$i], CURLOPT_POSTFIELDS, $data); curl_setopt($curl_array[$i], CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($curl_array[$i], CURLOPT_TIMEOUT, 15); curl_multi_add_handle($mh, $curl_array[$i]); echo "Please save this data No : $i ", $data['data'], PHP_EOL; } echo PHP_EOL ,PHP_EOL; $running = NULL; do { usleep(10000); curl_multi_exec($mh, $running); } while ( $running > 0 ); $res = array(); foreach ( $nodes as $i => $url ) { $curlErrorCode = curl_errno($curl_array[$i]); if ($curlErrorCode === 0) { $info = curl_getinfo($curl_array[$i]); if ($info['http_code'] == 200) { <------- Connection OK echo "Cya! (off to do something more important No : $i Done", PHP_EOL; echo curl_multi_getcontent($curl_array[$i]) , PHP_EOL ; } } curl_multi_remove_handle($mh, $curl_array[$i]); curl_close($curl_array[$i]); } curl_multi_close($mh); 

Output

 Please save this data No : A 1130087324 Please save this data No : B 1780371600 Please save this data No : C 764866719 Please save this data No : D 2042666801 Cya! (off to do something more important No : A Done Ok, Im done processing, here is your response... {"data":"1130087324"} PHP? Where did you go? I feel used :( 113 Cya! (off to do something more important No : B Done Ok, Im done processing, here is your response... {"data":"1780371600"} PHP? Where did you go? I feel used :( 113 Cya! (off to do something more important No : C Done Ok, Im done processing, here is your response... {"data":"764866719"} PHP? Where did you go? I feel used :( 112 Cya! (off to do something more important No : D Done Ok, Im done processing, here is your response... {"data":"2042666801"} PHP? Where did you go? I feel used :( 113 

Simple test server server.php

 echo printf("Ok, Im done processing, here is your response... \n\t%s PHP? Where did you go? \n\tI feel used :(\n", json_encode($_REQUEST)); 
+1
source

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


All Articles