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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With