cURL上传到Google云端硬盘 [英] cURL upload to Google Drive

查看:847
本文介绍了cURL上传到Google云端硬盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过json将文件在Google云端硬盘中创建到云端硬盘文件夹中:

I can create a file in Google Drive through a json into a Drive folder:

$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文件,因为没有json属性在Google云端硬盘文档中指定。

But I can't find a way to add the PDF file on it, since no json attribute is specified on Google Drive Documentation.

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

Where would I have to add the file contents? Is this possible?

我必须上传文件以上传/驱动,然后上传文件夹ID的元数据?

Do I have to upload the file to upload/drive and then the metadata with the folder id? I don't find the sense on that.

推荐答案

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

You want to put the file in the body (i.e. in the CURLOPT_POSTFIELDS). You'll want to specify Content-Type matching the file type and Content-Length as well.

这是

This is well documented.

文件元数据应该转到/ drive / v2 /文件,而不是/ drive / / upload / drive / v2 / files。

File metadata should be going to /drive/v2/files, not /upload/drive/v2/files.

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

This way, you'd have to make two requests.

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

If you want to do both a file and meta simultaneously, you can use the API and file insert:

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

请注意,有一个 PHP库可以使此操作更简单。

Note that there's a PHP library to make this easier.

这篇关于cURL上传到Google云端硬盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆