我正在编写一个脚本,其中需要通过 cURL 请求将未指定数量的文件上传到远程 API。但是,此脚本挂起并最终超时。奇怪的是,所有的请求都成功了(文件上传成功),但是脚本却无法继续。这是循环:
foreach ($paths as $path) {
$ch = curl_init($path);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Auth-Token: '.$token, 'Content-Length: '.filesize($path));
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, fopen($path, 'r'));
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($path));
echo curl_exec($ch);
}
我相信这与循环有关。我试过在循环中添加 curl_close
,但它并没有解决问题。有什么想法吗?
最佳答案
将超时
放入CURL
foreach ($paths as $path) {
$ch = curl_init($path);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Auth-Token: '.$token, 'Content-Length: '.filesize($path));
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, fopen($path, 'r'));
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($path));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
curl_setopt($ch, CURLOPT_TIMEOUT, 400); //timeout in seconds
echo curl_exec($ch);
}
关于php - 在循环中使用 cURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16371239/