cURL上传到Google云端硬盘

mrq*_*mrq 5 php upload curl google-drive-api

我可以通过json在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);
Run Code Online (Sandbox Code Playgroud)

但是我找不到在其上添加PDF文件的方法,因为在Google云端硬盘文档中没有指定json属性.

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

我是否必须将文件上传到上传/驱动器,然后上传文件夹ID的元数据?我没有找到感觉.

ern*_*nie 2

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

这是有据可查的

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

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

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

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

请注意,有一个PHP 库可以使这变得更容易。