PHP对REST API停顿的多个cURL请求

目前我有一个系统可以向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,REST API仍将在此期间响应,但此批处理客户端将不再发送请求.几分钟后,它将再次开始响应.我不知道为什么这会停滞不前,我可以通过htop看到两端的线程都没有cpu活动,而这种情况正在发生.

有没有理由说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/',));
$multi_curl->addGet('https://www.bing.com/search',));

$multi_curl->start();

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...