3

我正在编写一个脚本,其中需要通过 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在循环中添加,但它并没有解决问题。有任何想法吗?

4

2 回答 2

4

放入timeout_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);
}
于 2013-05-04T06:42:42.480 回答
-1

通过调用如下函数将 curl 与循环分开

foreach ($paths as $path) {
call_curl($path);
}


function call_curl($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);
}
于 2013-05-04T06:30:22.453 回答