如何使用PHP Curl获取Google Drive指定文件夹下的所有文件
Google Drive API获取指定文件夹下文件失败问题解决
已成功运行的基础代码
我已遵循Google Drive Search API文档,使用以下PHP Curl代码成功获取Google Drive中的所有文件和文件夹:
$access_token = 'xxxxxxxx'; $headers = array( "Authorization: Bearer $access_token", "Content-Type: application/json" ); $ch = curl_init(); // curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/drive/v3/files?trashed=false"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); $response = curl_exec($ch); var_dump($response);
问题描述
我需要获取指定文件夹(例如myvideo_folder)下的所有文件,尝试了以下代码后抛出404错误:
{ "error": { "code": 404, "message": "File not found: .", "errors": [ { "message": "File not found: .", "domain": "global", "reason": "notFound", "location": "fileId", "locationType": "parameter" } ] } }
尝试的代码:
$access_token = 'xxxxx'; $headers = array( "Authorization: Bearer $access_token", "Content-Type: application/json" ); $folder_name='myvideo_folder'; $ch = curl_init(); // curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/drive/v3/files?q='$folder_name' in parents"); curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/drive/v3/files?q=%27$folder_name%27%20in%20parents"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); $response = curl_exec($ch); var_dump($response);
问题原因
错误核心是:in parents查询条件需要传入文件夹的唯一ID,而非文件夹名称。直接用文件夹名称myvideo_folder作为参数,API无法识别对应资源,因此返回404错误。
解决方案
步骤1:根据文件夹名称获取对应ID
先调用API搜索目标文件夹,提取它的ID:
$access_token = 'xxxxx'; $headers = array( "Authorization: Bearer $access_token", "Content-Type: application/json" ); $folder_name = 'myvideo_folder'; // 搜索名称匹配的非删除文件夹 $search_url = "https://www.googleapis.com/drive/v3/files?q=name='$folder_name' and mimeType='application/vnd.google-apps.folder' and trashed=false"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $search_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); $folder_response = curl_exec($ch); $folder_data = json_decode($folder_response, true); // 提取文件夹ID(假设仅存在一个匹配文件夹) $folder_id = ''; if (!empty($folder_data['files']) && count($folder_data['files']) > 0) { $folder_id = $folder_data['files'][0]['id']; } else { die("未找到名称为 $folder_name 的文件夹"); }
步骤2:用文件夹ID查询子文件
拿到ID后,以此作为parents参数查询该文件夹下的所有非删除文件:
// 查询目标文件夹下的所有非删除文件 $files_url = "https://www.googleapis.com/drive/v3/files?q=%27$folder_id%27%20in%20parents%20and%20trashed=false"; curl_setopt($ch, CURLOPT_URL, $files_url); $files_response = curl_exec($ch); var_dump($files_response); // 关闭curl资源 curl_close($ch);
完整整合代码
$access_token = 'xxxxx'; $headers = array( "Authorization: Bearer $access_token", "Content-Type: application/json" ); $folder_name = 'myvideo_folder'; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); // 1. 获取目标文件夹ID $search_url = "https://www.googleapis.com/drive/v3/files?q=name='$folder_name' and mimeType='application/vnd.google-apps.folder' and trashed=false"; curl_setopt($ch, CURLOPT_URL, $search_url); $folder_response = curl_exec($ch); $folder_data = json_decode($folder_response, true); $folder_id = ''; if (!empty($folder_data['files']) && count($folder_data['files']) > 0) { $folder_id = $folder_data['files'][0]['id']; } else { die("未找到名称为 $folder_name 的文件夹"); } // 2. 查询该文件夹下的所有文件 $files_url = "https://www.googleapis.com/drive/v3/files?q=%27$folder_id%27%20in%20parents%20and%20trashed=false"; curl_setopt($ch, CURLOPT_URL, $files_url); $files_response = curl_exec($ch); var_dump($files_response); curl_close($ch);
内容的提问来源于stack exchange,提问作者Nancy Moore
相关产品推荐
相关产品推荐

