首页 > 解决方案 > php cURL 循环下载一次,然后停止

问题描述

我正在尝试做的基本操作是多次访问 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;
    }
}

标签: phpcurlziparchive

解决方案


我终于设法让它工作了,我只发送一个“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);
    }
}

推荐阅读