cURL 上传到 Google 云端硬盘

2024-03-20

我可以通过 json 到 Drive 文件夹中在 Google Drive 中创建一个文件:

$data = array(
    "title" => $_FILES['file']['name'],
    "parents" => array(array(
        "kind" => "drive#parentReference",
        "id" => $parent_id
    )),
    "mimeType" => "application/pdf"
);
$data = json_encode($data);

$url = "https://www.googleapis.com/drive/v2/files?oauth_token=".$accessToken;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

但我找不到在其上添加 PDF 文件的方法,因为 Google Drive 文档上没有指定 json 属性。

我必须在哪里添加文件内容? 这可能吗?

我是否必须先上传文件才能上传/驱动器,然后再上传带有文件夹 ID 的元数据?我觉得这没有什么意义。


您想将文件放入正文中(即 CURLOPT_POSTFIELDS 中)。您还需要指定与文件类型匹配的 Content-Type 和 Content-Length。

This is 有据可查 https://developers.google.com/drive/manage-uploads.

文件元数据应位于 /drive/v2/files,而不是 /upload/drive/v2/files。

这样,您就必须提出两个请求。

如果你想同时执行文件和元数据,可以使用 API 和文件插入:

https://developers.google.com/drive/v2/reference/files/insert https://developers.google.com/drive/v2/reference/files/insert

请注意,有一个PHP库 https://developers.google.com/drive/downloads让这变得更容易。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

cURL 上传到 Google 云端硬盘 的相关文章

随机推荐