Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 下载多个谷歌驱动器文件作为压缩文件_Php_Curl_Google Drive Api_Google Api Php Client - Fatal编程技术网

Php 下载多个谷歌驱动器文件作为压缩文件

Php 下载多个谷歌驱动器文件作为压缩文件,php,curl,google-drive-api,google-api-php-client,Php,Curl,Google Drive Api,Google Api Php Client,用户登录门户后,将显示PDF报告列表 为了下载所需的报告,用户可以选中/取消选中与每个报告关联的框 比如说, 列表中有10份报告。用户已选择7个报告。点击下载。此工作流应导致下载压缩的文件,该文件包含所有选定的报告(7),而不是单独下载每个文件 上述示例中的这10个报告存储在Google Drive中。我们将Google下载URL存储在数据库中。使用此下载URL,我们需要完成上述结果 尝试使用Google Drive API快速启动参考。错误:403在第二次尝试将文件保存到文件系统时命中 PHP

用户登录门户后,将显示PDF报告列表

为了下载所需的报告,用户可以选中/取消选中与每个报告关联的框

比如说,

列表中有10份报告。用户已选择7个报告。点击下载。此工作流应导致下载压缩的文件,该文件包含所有选定的报告(7),而不是单独下载每个文件

上述示例中的这10个报告存储在Google Drive中。我们将Google下载URL存储在数据库中。使用此下载URL,我们需要完成上述结果

尝试使用Google Drive API快速启动参考。错误:403在第二次尝试将文件保存到文件系统时命中

PHP cURL实现在第三轮运行脚本时失败,状态代码为403

基本上,计划是将每个选定的文件保存在文件系统的文件夹中。然后,压缩文件夹并下载压缩文件

这是我最近试过的

<?php

define('SAVE_REPORT_DIR', getcwd(). '/pathtosave/'. time());

function fs_report_save($fileUrl) 
{
    static $counter = 1;

    if (!file_exists(SAVE_REPORT_DIR)) {
        mkdir(SAVE_REPORT_DIR, 0777, true);
    }

    //The path & filename to save to.
    $saveTo = SAVE_REPORT_DIR. '/'. time(). '.pdf';

    //Open file handler.
    $fp = fopen($saveTo, 'w+');

    //If $fp is FALSE, something went wrong.
    if($fp === false){
        throw new Exception('Could not open: ' . $saveTo);
    }

    //Create a cURL handle.
    $ch = curl_init($fileUrl);

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    //Pass our file handle to cURL.
    curl_setopt($ch, CURLOPT_FILE, $fp);

    //Timeout if the file doesn't download after 20 seconds.
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);

    //Execute the request.
    curl_exec($ch);

    //If there was an error, throw an Exception
    if(curl_errno($ch)){
        throw new Exception(curl_error($ch));
    }

    //Get the HTTP status code.
    $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    //Close the cURL handler.
    curl_close($ch);

    //Close the file handler.
    fclose($fp);

    if($statusCode == 200){
        echo 'File: '. $saveTo .'. Downloaded!<br>';
    } else{
        echo "Status Code: " . $statusCode;
    }
}

$reports = array(
    'https://drive.google.com/uc?id=a&export=download',
    'https://drive.google.com/uc?id=b&export=download',
    'https://drive.google.com/uc?id=c&export=download'
);

foreach($reports as $report) {
    fs_report_save($report);
}

?>

正如@DalmTo所说,API不会让您以zip的形式大量使用,您可以做的是在驱动器内创建一个文件夹,并将该文件夹作为zip下载

在@Tanaike的回答中还有更多的信息:


您必须一次下载一个文件,然后在本地压缩它们。api或下载链接不允许您进行那样的批量下载。