PHP 对 REST 的多个 cURL 请求 API 停止
PHP Multiple cURL requests to REST API stalls
目前我有一个向 REST 发送多个请求的系统 API。它的结构如下:
foreach ($data as $d)
{
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_HTTPHEADER, (array of data here));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
$retry = 0;
while((curl_errno($ch) == 7 || curl_errno($ch) == 52) && $retry < 3)
{
$response = curl_exec($ch);
$retry++;
}
curl_close($ch);
(decode XML Response and loop)
}
(我无法公开整个代码,所以我在括号中填写了发生的操作)
然而,在几百次请求之后,FastCGI 脚本停止了。如果我以其他方式查询,REST API 在此期间仍会响应,但此批处理客户端将不再发送请求。几分钟后,它将再次开始响应。我不确定为什么会停滞,我可以通过 htop 看到发生这种情况时两端的线程上都没有 CPU activity 。
cURL/PHP 脚本会在这里停滞的原因是什么?
如果您允许使用外部 PHP 库;
我想推荐这种方法:
https://github.com/php-curl-class/php-curl-class
// Requests in parallel with callback functions.
$multi_curl = new MultiCurl();
$multi_curl->success(function($instance) {
echo 'call to "' . $instance->url . '" was successful.' . "\n";
echo 'response: ' . $instance->response . "\n";
});
$multi_curl->error(function($instance) {
echo 'call to "' . $instance->url . '" was unsuccessful.' . "\n";
echo 'error code: ' . $instance->error_code . "\n";
echo 'error message: ' . $instance->error_message . "\n";
});
$multi_curl->complete(function($instance) {
echo 'call completed' . "\n";
});
$multi_curl->addGet('https://www.google.com/search', array(
'q' => 'hello world',
));
$multi_curl->addGet('https://duckduckgo.com/', array(
'q' => 'hello world',
));
$multi_curl->addGet('https://www.bing.com/search', array(
'q' => 'hello world',
));
$multi_curl->start();
目前我有一个向 REST 发送多个请求的系统 API。它的结构如下:
foreach ($data as $d)
{
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_HTTPHEADER, (array of data here));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
$retry = 0;
while((curl_errno($ch) == 7 || curl_errno($ch) == 52) && $retry < 3)
{
$response = curl_exec($ch);
$retry++;
}
curl_close($ch);
(decode XML Response and loop)
}
(我无法公开整个代码,所以我在括号中填写了发生的操作)
然而,在几百次请求之后,FastCGI 脚本停止了。如果我以其他方式查询,REST API 在此期间仍会响应,但此批处理客户端将不再发送请求。几分钟后,它将再次开始响应。我不确定为什么会停滞,我可以通过 htop 看到发生这种情况时两端的线程上都没有 CPU activity 。
cURL/PHP 脚本会在这里停滞的原因是什么?
如果您允许使用外部 PHP 库; 我想推荐这种方法: https://github.com/php-curl-class/php-curl-class
// Requests in parallel with callback functions.
$multi_curl = new MultiCurl();
$multi_curl->success(function($instance) {
echo 'call to "' . $instance->url . '" was successful.' . "\n";
echo 'response: ' . $instance->response . "\n";
});
$multi_curl->error(function($instance) {
echo 'call to "' . $instance->url . '" was unsuccessful.' . "\n";
echo 'error code: ' . $instance->error_code . "\n";
echo 'error message: ' . $instance->error_message . "\n";
});
$multi_curl->complete(function($instance) {
echo 'call completed' . "\n";
});
$multi_curl->addGet('https://www.google.com/search', array(
'q' => 'hello world',
));
$multi_curl->addGet('https://duckduckgo.com/', array(
'q' => 'hello world',
));
$multi_curl->addGet('https://www.bing.com/search', array(
'q' => 'hello world',
));
$multi_curl->start();