![](/img/trans.png)
[英]Working with ChannelAdvisor REST API in PHP - requests not working in CURL
[英]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,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/', array(
'q' => 'hello world',
));
$multi_curl->addGet('https://www.bing.com/search', array(
'q' => 'hello world',
));
$multi_curl->start();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.