php - Perform multiple simultaneous POST calls to the same API endpoint -


i trying perform multiple post rest call. the catch: doing multiple post calls @ same time. aware , have worked library guzzle haven't figured away properly. can perform get calls asynchronously nothing @ same level post calls. came across pthreads , read through documentation , bit confused on how start off. have compiled php pthreads extension.

could advise how perform multiple post calls @ same time , able gather responses later manipulation?

the below basic implementation loops , waits. slow overall.

$postdatas = [     ['field' => 'test'],     ['field' => 'test1'],     ['field' => 'test2'], ];  foreach ($postdatas $postdata) {     $curl = curl_init();      curl_setopt_array($curl, array(         curlopt_url            => "https://www.apisite.com",         curlopt_returntransfer => true,         curlopt_encoding       => "",         curlopt_maxredirs      => 10,         curlopt_timeout        => 30,         curlopt_http_version   => curl_http_version_1_1,         curlopt_customrequest  => "post",         curlopt_postfields     => json_encode($postdata),         curlopt_httpheader     => [             "cache-control: no-cache",             "connection: keep-alive",             "content-type: application/json",             "host: some.apisite.com",         ],     ));      $response = curl_exec($curl);     $err      = curl_error($curl);      curl_close($curl);      if ($err) {         echo "curl error #:" . $err;     } else {         echo $response;     } } 

that if task reduced working api need use http://php.net/manual/ru/function.curl-multi-exec.php

public function getmultiurl() {     //if connections split queue parts     $parts = array_chunk($this->urlstack, self::url_iteration_size , true);      //base options     $options = [         curlopt_useragent => 'myapp',         curlopt_header => false,         curlopt_returntransfer => true,         curlopt_post => true,     ];      foreach ($parts $urls) {         $mh = curl_multi_init();         $active = null;         $connects = [];         foreach ($urls $i => $url) {             $options[curlopt_postfields] = $url['postdata'];              $connects[$i] = curl_init($url['queryurl']);              curl_setopt_array($connects[$i], $options);             curl_multi_add_handle($mh, $connects[$i]);         }          {             $status = curl_multi_exec($mh, $active);             $info = curl_multi_info_read($mh);             if (false !== $info) {                 var_dump($info);             }         } while ($status === curlm_call_multi_perform || $active);          foreach ($connects $i => $conn) {             $content = curl_multi_getcontent($conn);             file_put_contents($this->dir . $i, $content);             curl_close($conn);         }     } } 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -