我正在尝试执行多个POST
REST 调用。问题:同时进行多个 POST 调用。我完全意识到并与图书馆合作过,guzzle
但我还没有想办法正确地做到这一点。我可以GET
异步执行调用,但没有任何相同级别的POST
调用。然后我偶然发现pthreads
并阅读了文档,并且对如何启动它感到有些困惑。我已经编译php
了pthreads
扩展名。
有人可以建议如何同时执行多个POST
呼叫并能够收集响应以供以后操作吗?
下面是一个循环和等待的基本实现。整体非常慢。
$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;
}
}