Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL upload to Google Drive

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);

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?

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.

like image 394
mrq Avatar asked Nov 04 '22 10:11

mrq


1 Answers

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.

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

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

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

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

like image 51
ernie Avatar answered Nov 09 '22 06:11

ernie