0

I am trying to perform multiple POST REST Call. The catch: doing multiple POST calls at the same time. I am fully aware and have worked with the library guzzle but I haven't figured away to do this properly. I can perform GET calls asynchronously but nothing at the same level for POST calls. Then I came across pthreads and I read through the documentation and was a bit confused on how to even start it off. I have compiled php with the pthreads extension.

Could someone advise how to perform multiple POST calls at the same time and be able to gather the responses for later manipulation?

The below is a basic implementation that loops and waits. Very slow overall.

$postDatas = [
    ['field' => 'test'],
    ['field' => 'test1'],
    ['field' => 'test2'],
];

foreach ($postDatas as $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;
    }
}
MaryCoding
  • 624
  • 1
  • 9
  • 31

1 Answers1

1

That if the task is reduced to working with the API then you probably need to use http://php.net/manual/ru/function.curl-multi-exec.php

public function getMultiUrl() {
    //If the connections are very much split the queue into 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 as $urls) {
        $mh = curl_multi_init();
        $active = null;
        $connects = [];
        foreach ($urls as $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]);
        }

        do {
            $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 as $i => $conn) {
            $content = curl_multi_getcontent($conn);
            file_put_contents($this->dir . $i, $content);
            curl_close($conn);
        }
    }
}
Redr01d
  • 392
  • 2
  • 12