php cURL循环下载一次,然后停止

时间:2018-10-01 20:26:01

标签: php curl ziparchive

我要做的基本操作是多次访问API以下载许多音频文件。

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

值得注意的是,这是一个安全的服务器,要求我在访问令牌之前先获取令牌,这就是“ 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;
    }
}

1 个答案:

答案 0 :(得分:0)

我终于设法使它工作了,我只向该函数发送了一个ID数组,该ID数组是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);
    }
}