对同一个 API 端点同时执行多个 POST 调用
Perform multiple simultaneous POST calls to the same API endpoint
我正在尝试执行多个 POST
REST 调用。 The catch:同时进行多个POST调用。我完全了解并与图书馆 guzzle
一起工作过,但我还没有想出办法正确地做到这一点。我可以异步执行 GET
调用,但在 POST
调用的同一级别上没有任何东西。然后我遇到了 pthreads
并且通读了文档并且对如何开始它感到有点困惑。我用 pthreads
扩展编译了 php
。
有人可以建议如何同时执行多个 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;
}
}
如果任务减少到使用 API 那么您可能需要使用 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);
}
}
}
我正在尝试执行多个 POST
REST 调用。 The catch:同时进行多个POST调用。我完全了解并与图书馆 guzzle
一起工作过,但我还没有想出办法正确地做到这一点。我可以异步执行 GET
调用,但在 POST
调用的同一级别上没有任何东西。然后我遇到了 pthreads
并且通读了文档并且对如何开始它感到有点困惑。我用 pthreads
扩展编译了 php
。
有人可以建议如何同时执行多个 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;
}
}
如果任务减少到使用 API 那么您可能需要使用 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);
}
}
}