【问题标题】:php cURL loop downloads once, then stopsphp cURL 循环下载一次,然后停止
【发布时间】:2018-10-01 20:26:01
【问题描述】:

我尝试做的基本工作是多次访问一个 API 以下载许多音频文件。

我的想法是我有一个名为“download_audio”的函数。这个函数是一个 cURL,适用于我给它的任何单数 ID。但是,当我将相同的函数放入循环 ID 的 foreach 循环中时,它将工作一次,然后停止。我很好奇它为什么会停止,以及如何解决这个问题。

值得注意的是,这是一个安全服务器,它要求我在访问它之前获得一个令牌,这就是“JWTtoken”的含义。否则我的代码如下所示:

function download_audio($JWTtoken, $contactId)
{
    $format = "wav";
    $userArray = array();
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://MYURL/" . $contactId . "/" . $format,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_CAINFO => "../cacert.pem",
      CURLOPT_TIMEOUT => 15,
      CURLOPT_SSL_VERIFYPEER => 1,
      CURLOPT_SSL_VERIFYHOST => 2,
      CURLOPT_SSLVERSION => 6,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_HTTPHEADER => array(
        "content-type: audio/" . $format, $JWTtoken,
        "Content-Description: File Transfer",
        "Content-Type: audio/" . $format,
        "Content-Disposition: attachment; filename=" . $contactId . "." . $format,
        "Content-Transfer-Encoding: binary",
        "Expires: 0",
        "Cache-Control: must-revalidate",
        "Pragma: public"
        )));
    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

    if ($err) 
    {
          return "cURL Error #:" . $err;
    }
    else
    {
        header("Content-type: application/octet-stream"); 
        header("Content-Disposition: attachment; filename=" . $contactId . "." . $format); 
        echo $response;
        return 1;
    }
}

【问题讨论】:

  • 你把循环放在哪里 - 在上面的函数中还是在调用这个函数的东西中?
  • 我在循环中调用这个函数。类似于: foreach( $myarray as $key => $item) { 一些从数组 $DLaudio = download_audio($JWTtoken, $id) } 中获取我的 ID 的逻辑
  • 您发送的所有标头都是响应标头,而不是请求标头。
  • 您将响应作为下载发送到客户端。但您一次只能发送一个下载。
  • 您是否受到 api 的速率限制?

标签: php curl ziparchive


【解决方案1】:

我终于成功地完成了这项工作,我只发送了一个“IDarray”,它只是一个 ID 数组,以及安全令牌。实际上,原始答案是每个请求只能下载一个文件。我原本以为因为我多次调用该函数,它会请求多次。但事实并非如此,所以我能够将所有文件压缩成一个文件,然后下载该文件。

以下功能对我有用:

function download_multiaudio_zip($JWTtoken, $IDArray)
{
    //turn ID array into URL array
    foreach ($IDArray as $key => $id)
    {
        $URLArray[$key] = "https://MYURL/downloadaudio/" . $id . "/wav";
    }
    $zip = new ZipArchive();
    $tmp_file = tempnam('.','');
    $zip->open($tmp_file, ZipArchive::CREATE);
    foreach ($URLArray as $key => $URL)
    {
        $curl = curl_init();
        curl_setopt_array($curl, array(
          CURLOPT_URL => $URL,
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_CAINFO => "../cacert.pem",
          CURLOPT_TIMEOUT => 15,
          CURLOPT_SSL_VERIFYPEER => 1,
          CURLOPT_SSL_VERIFYHOST => 2,
          CURLOPT_SSLVERSION => 6,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "GET",
          CURLOPT_HTTPHEADER => array(
            "content-type: audio/wav", $JWTtoken,
            "Content-Description: File Transfer"
            )));
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        $zip->addFromString($IDArray[$key] . ".wav", $response);
    }
    $zip->close();
    if ($err) 
    {
      return "cURL Error #:" . $err;
    }
    else
    {
        header('Content-disposition: attachment; filename=audiofiles.zip');
        header('Content-type: application/zip');
        readfile($tmp_file);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多