Download cURL to Google Drive
I can create a file in Google Drive via json in the 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 a PDF file to it, because the json attribute is not specified in the documentation on Google Drive.
Where should I add the contents of the file? Is it possible?
Do I need to upload a file for upload / write, and then metadata with a folder id? I do not find sense in this.
You want to put the file in the body (i.e. in CURLOPT_POSTFIELDS). You will also want to specify a Content-Type that matches the file type and Content-Length.
This is well documented .
File metadata should be in / drive / v 2 / files, not / upload / drive / v2 / files.
So you will need to make two requests.
If you want to do a file and a meta at the same time, you can use the API and file insertion:
https://developers.google.com/drive/v2/reference/files/insert
Please note that there is a PHP library to make it easier.