php - 在循环中使用 cURL

标签 php loops curl

我正在编写一个脚本,其中需要通过 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/

相关文章:

bash - curl 在独立运行和 bash for 循环中的行为不同

php - 在 WordPress 中保存多个键/值对

php - 将子主题 css 添加到 <head>

java - 无法将数据发布到服务器,出现错误 java.io.IOException : Server returned HTTP response code: 415

excel - VBA循环要求用户输入新的工作表名称

r - 为随机抽样构建 R 循环

curl - Databricks:将 dbfs:/FileStore 文件下载到我的本地机器?

php - 我可以在使用 Zend_Db_Adapter_Pdo_Mssql 时设置查询超时吗?

php - 长 SQL 查询优化

java - 更改计算器程序java(循环)