Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对RESTAPI暂停的PHP多个cURL请求_Php_Rest_Cakephp_Curl - Fatal编程技术网

对RESTAPI暂停的PHP多个cURL请求

对RESTAPI暂停的PHP多个cURL请求,php,rest,cakephp,curl,Php,Rest,Cakephp,Curl,目前我有一个向RESTAPI发送多个请求的系统。它的结构如下: 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); $respo

目前我有一个向RESTAPI发送多个请求的系统。它的结构如下:

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)
}
foreach($d数据)
{
$ch=curl\u init($url);
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,(此处的数据数组));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$response=curl\u exec($ch);
$retry=0;
而((curl_errno($ch)==7 | | curl_errno($ch)==52)和&$retry<3)
{
$response=curl\u exec($ch);
$retry++;
}
卷曲关闭($ch);
(解码XML响应和循环)
}
(我不能公开整个代码,所以我已经填写了在括号中发生的操作)

然而,在几百次请求之后,FastCGI脚本暂停。如果我以另一种方式查询RESTAPI,RESTAPI在此期间仍将响应,但此批处理客户端将不再发送请求。几分钟后,它将再次开始响应。我不确定为什么会出现这种情况,我可以通过htop看到,在发生这种情况时,两端的线程上都没有CPU活动


cURL/PHP脚本在这里暂停有什么原因吗?

如果允许使用外部PHP库; 我想建议这种方法:

你可以用
    // 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();